Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have to_json and as_json return amount and currency json #138

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 7 additions & 6 deletions lib/money/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def parse(*args)
parser.parse(*args)
end

def from_json(json)
data = JSON.parse(json)
Money.new(data['value'], data['currency'])
end

def from_cents(cents, currency = nil)
new(cents.round.to_f / 100, currency)
end
Expand Down Expand Up @@ -213,12 +218,8 @@ def to_liquid
cents
end

def to_json(options = {})
to_s
end

def as_json(*args)
to_s
def as_json(*_args)
{ value: to_s(:amount), currency: currency.to_s }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we prefer currency.iso_code here instead to be a bit more clear and future proof if we want to_s to be more human readable?

Copy link
Contributor Author

@elfassy elfassy Jun 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main difference is with a NullCurrency, to_s will return an empty string, vs iso_code will return XXX. I'd rather keep it the way it is for now since the generated JSON might be sent to a third party which does not understand the valid "no currency" iso code.

end

def abs
Expand Down
12 changes: 8 additions & 4 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
expect(non_fractional_money.to_s(:amount)).to eq("1")
end

it "as_json as a float with 2 decimal places" do
expect(money.as_json).to eq("1.00")
it "as_json as a json containing the value and currency" do
expect(money.as_json).to eq(value: "1.00", currency: "CAD")
end

it "is constructable with a BigDecimal" do
Expand Down Expand Up @@ -282,8 +282,8 @@
expect(Money.new(1.00).to_liquid).to eq(100)
end

it "returns cents in to_json" do
expect(Money.new(1.00).to_json).to eq("1.00")
it "returns in to_json" do
expect(Money.new(1.00).to_json).to eq('{"value":"1.00","currency":"CAD"}')
end

it "supports absolute value" do
Expand All @@ -302,6 +302,10 @@
expect(Money.new(1.50).to_f.to_s).to eq("1.5")
end

it "is creatable from a json string" do
expect(Money.from_json('{"value":"1.0","currency":"CAD"}')).to eq(Money.new(1, 'CAD'))
end

it "is creatable from an integer value in cents" do
expect(Money.from_cents(1950)).to eq(Money.new(19.50))
end
Expand Down