Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rename/-work find_by_iso_numeric and add specs.

  • Loading branch information...
commit 4d0f9e18430a8632a6bd0d960212000f8d020b55 1 parent c51f899
@ct-clearhaus ct-clearhaus authored
Showing with 28 additions and 6 deletions.
  1. +7 −6 lib/money/currency.rb
  2. +21 −0 spec/currency_spec.rb
View
13 lib/money/currency.rb
@@ -38,16 +38,17 @@ def find(id)
# Lookup a currency with given +num+ as an ISO 4217 numeric and returns an
# +Currency+ instance on success, +nil+ otherwise.
#
- # @param [Integer, #to_i] num used to look into +table+ in +iso_numeric+
- # and find the right currency id.
+ # @param [#to_s] num used to look into +table+ in +iso_numeric+ and find
+ # the right currency id.
#
# @return [Money::Currency]
#
# @example
- # Money::Currency.find_numeric(978) #=> #<Money::Currency id: eur ...>
- # Money::Currency.find_numeric(1) #=> nil
- def find_numeric(num)
- id = (self.table.select { |cur| self.table[cur][:iso_numeric].to_i == num }).keys.first
+ # Money::Currency.find_by_iso_numeric(978) #=> #<Money::Currency id: eur ...>
+ # Money::Currency.find_by_iso_numeric('001') #=> nil
+ def find_by_iso_numeric(num)
+ num = num.to_s
+ id, garbage = self.table.find{|key, currency| currency[:iso_numeric] == num}
new(id) if self.table[id]
end
View
21 spec/currency_spec.rb
@@ -22,6 +22,27 @@
end
end
+ describe ".find_by_iso_numeric" do
+ it "returns currency matching given numeric code" do
+ Money::Currency.find_by_iso_numeric(978).should == Money::Currency.new(:eur)
+ Money::Currency.find_by_iso_numeric(208).should_not == Money::Currency.new(:eur)
+ Money::Currency.find_by_iso_numeric('840').should == Money::Currency.new(:usd)
+
+ class Mock
+ def to_s
+ '208'
+ end
+ end
+ Money::Currency.find_by_iso_numeric(Mock.new).should == Money::Currency.new(:dkk)
+ Money::Currency.find_by_iso_numeric(Mock.new).should_not == Money::Currency.new(:usd)
+ end
+
+ it "returns nil if no currency has the given numeric code" do
+ Money::Currency.find_by_iso_numeric('non iso 4217 numeric code').should == nil
+ Money::Currency.find_by_iso_numeric(0).should == nil
+ end
+ end
+
describe ".wrap" do
it "returns nil if object is nil" do
Money::Currency.wrap(nil).should == nil
Please sign in to comment.
Something went wrong with that request. Please try again.