From 33b5be1a5aaa2a62f6801ea3b9c89f6deafa0e0b Mon Sep 17 00:00:00 2001 From: Pavel Pravosud Date: Fri, 17 Jun 2011 12:52:07 +0700 Subject: [PATCH] added Money#to_json method --- lib/money/money.rb | 7 +++++++ spec/money_spec.rb | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/lib/money/money.rb b/lib/money/money.rb index 095c8f8234..82dc065059 100644 --- a/lib/money/money.rb +++ b/lib/money/money.rb @@ -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 # diff --git a/spec/money_spec.rb b/spec/money_spec.rb index a796a63e09..f08a0737ea 100644 --- a/spec/money_spec.rb +++ b/spec/money_spec.rb @@ -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