Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion lib/money/currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,22 @@ def hash
# @example
# Money::Currency.new(:usd) #=> #<Currency id: usd ...>
def inspect
"#<#{self.class.name} id: #{id}, priority: #{priority}, symbol_first: #{symbol_first}, thousands_separator: #{thousands_separator}, html_entity: #{html_entity}, decimal_mark: #{decimal_mark}, name: #{name}, symbol: #{symbol}, subunit_to_unit: #{subunit_to_unit}, exponent: #{exponent}, iso_code: #{iso_code}, iso_numeric: #{iso_numeric}, subunit: #{subunit}, smallest_denomination: #{smallest_denomination}, format: #{format}>"
"#<#{self.class.name} " \
"id: #{id}, " \
"priority: #{priority}, " \
"symbol_first: #{symbol_first}, " \
"thousands_separator: #{thousands_separator}, " \
"html_entity: #{html_entity}, " \
"decimal_mark: #{decimal_mark}, " \
"name: #{name}, " \
"symbol: #{symbol}, " \
"subunit_to_unit: #{subunit_to_unit}, " \
"exponent: #{exponent}, " \
"iso_code: #{iso_code}, " \
"iso_numeric: #{iso_numeric}, " \
"subunit: #{subunit}, " \
"smallest_denomination: #{smallest_denomination}, " \
"format: #{format}>"
end

# Returns a string representation corresponding to the upcase +id+
Expand Down
6 changes: 5 additions & 1 deletion lib/money/money/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ class Formatter
#
# @example
# Money.ca_dollar(570).format(html_wrap: true, with_currency: true)
# #=> "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span> <span class=\"money-currency\">CAD</span>"
# #=> "<span class=\"money-currency-symbol\">$</span>" \
# "<span class=\"money-whole\">5</span>" \
# "<span class=\"money-decimal-mark\">.</span>" \
# "<span class=\"money-decimal\">70</span> " \
# "<span class=\"money-currency\">CAD</span>"
#
# @option rules [Boolean] :sign_before_symbol (false) Whether the sign should be
# before the currency symbol.
Expand Down
37 changes: 35 additions & 2 deletions spec/currency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,41 @@ def to_s

describe "#inspect" do
it "works as documented" do
expect(described_class.new(:usd).inspect).to eq "#<Money::Currency id: usd, priority: 1, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: United States Dollar, symbol: $, subunit_to_unit: 100, exponent: 2, iso_code: USD, iso_numeric: 840, subunit: Cent, smallest_denomination: 1, format: >"
expect(described_class.new(:aed).inspect).to eq "#<Money::Currency id: aed, priority: 100, symbol_first: false, thousands_separator: ,, html_entity: , decimal_mark: ., name: United Arab Emirates Dirham, symbol: د.إ, subunit_to_unit: 100, exponent: 2, iso_code: AED, iso_numeric: 784, subunit: Fils, smallest_denomination: 25, format: %n %u>"
expect(described_class.new(:usd).inspect).to eq(
"#<Money::Currency " \
"id: usd, priority: 1, " \
"symbol_first: true, " \
"thousands_separator: ,, " \
"html_entity: $, " \
"decimal_mark: ., " \
"name: United States Dollar, " \
"symbol: $, " \
"subunit_to_unit: 100, " \
"exponent: 2, " \
"iso_code: USD, " \
"iso_numeric: 840, " \
"subunit: Cent, " \
"smallest_denomination: 1, " \
"format: >",
)
expect(described_class.new(:aed).inspect).to eq(
"#<Money::Currency " \
"id: aed, " \
"priority: 100, " \
"symbol_first: false, " \
"thousands_separator: ,, " \
"html_entity: , " \
"decimal_mark: ., " \
"name: United Arab Emirates Dirham, " \
"symbol: د.إ, " \
"subunit_to_unit: 100, " \
"exponent: 2, " \
"iso_code: AED, " \
"iso_numeric: 784, " \
"subunit: Fils, " \
"smallest_denomination: 25, " \
"format: %n %u>",
)
end
end

Expand Down
64 changes: 60 additions & 4 deletions spec/money/allocation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,76 @@
context "with an allocation seen in the wild" do
it "allocates the full amount" do
amount = 700273
allocations = [1.1818583143661, 1.1818583143661, 1.1818583143661, 1.1818583143661, 1.1818583143661, 1.1818583143661, 1.1818583143661, 1.170126087450276, 1.0, 1.0, 1.0, 1.0]
allocations = [
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.170126087450276,
1.0,
1.0,
1.0,
1.0,
]

result = described_class.generate(amount, allocations)
expect(result.reduce(&:+)).to eq(amount)
expect(result).to eq([61566, 61565, 61565, 61565, 61565, 61565, 61565, 60953, 52091, 52091, 52091, 52091])
expect(result).to eq(
[
61566,
61565,
61565,
61565,
61565,
61565,
61565,
60953,
52091,
52091,
52091,
52091,
],
)
end

it "allocates the full -amount" do
amount = -700273
allocations = [1.1818583143661, 1.1818583143661, 1.1818583143661, 1.1818583143661, 1.1818583143661, 1.1818583143661, 1.1818583143661, 1.170126087450276, 1.0, 1.0, 1.0, 1.0]
allocations = [
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.1818583143661,
1.170126087450276,
1.0,
1.0,
1.0,
1.0,
]

result = described_class.generate(amount, allocations)
expect(result.reduce(&:+)).to eq(amount)
expect(result).to eq([-61566, -61565, -61565, -61565, -61565, -61565, -61565, -60953, -52091, -52091, -52091, -52091])
expect(result).to eq(
[
-61566,
-61565,
-61565,
-61565,
-61565,
-61565,
-61565,
-60953,
-52091,
-52091,
-52091,
-52091,
],
)
end

context "when specified precision" do
Expand Down
23 changes: 17 additions & 6 deletions spec/money/formatting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,19 @@
end

specify "(south_asian_number_formatting: true) works as documented" do
expect(Money.new(10000000, "INR").format(south_asian_number_formatting: true, symbol: false)).to eq "1,00,000.00"
expect(Money.new(1000000000, "INDIAN_BAR").format(south_asian_number_formatting: true, symbol: false)).to eq "1,00,000.0000"
expect(Money.new(10000000).format(south_asian_number_formatting: true)).to eq "$1,00,000.00"
expect(Money.new(10000000, "INR").format(south_asian_number_formatting: true, symbol: false))
.to eq "1,00,000.00"
expect(Money.new(1000000000, "INDIAN_BAR").format(south_asian_number_formatting: true, symbol: false))
.to eq "1,00,000.0000"
expect(Money.new(10000000).format(south_asian_number_formatting: true))
.to eq "$1,00,000.00"
end

specify "(south_asian_number_formatting: true and no_cents_if_whole => true) works as documented" do
expect(Money.new(10000000, "INR").format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true)).to eq "1,00,000"
expect(Money.new(1000000000, "INDIAN_BAR").format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true)).to eq "1,00,000"
expect(Money.new(10000000, "INR").format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true))
.to eq "1,00,000"
expect(Money.new(1000000000, "INDIAN_BAR").format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true))
.to eq "1,00,000"
end
end

Expand Down Expand Up @@ -488,7 +493,13 @@

specify "(html_wrap: true, with_currency: true)" do
string = Money.ca_dollar(570).format(html_wrap: true, with_currency: true)
expect(string).to eq "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span> <span class=\"money-currency\">CAD</span>"
expect(string).to eq(
"<span class=\"money-currency-symbol\">$</span>" \
"<span class=\"money-whole\">5</span>" \
"<span class=\"money-decimal-mark\">.</span>" \
"<span class=\"money-decimal\">70</span> " \
"<span class=\"money-currency\">CAD</span>",
)
end

specify "should fallback to symbol if entity is not available" do
Expand Down