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

Added Money#to_json method #88

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/money/money.rb
Expand Up @@ -343,6 +343,13 @@ def as_euro
def to_money
self
end

# Return the amount of money as a JSON string.
#
# @return [String]
def to_json
JSON.dump(:amount => to_f, :currency => currency.to_s)
end

# Common inspect function
#
Expand Down
5 changes: 5 additions & 0 deletions spec/money_spec.rb
Expand Up @@ -125,6 +125,11 @@
decimal.should be_instance_of(BigDecimal)
decimal.should == 10.0
end

specify "Money.to_json works" do
json = %({"amount":10.0,"currency":"USD"})
Money.new(10_00, 'USD').to_json.should == json
end

specify "Money.to_d should respect :subunit_to_unit currency property" do
decimal = Money.new(10_00, "BHD").to_d
Expand Down