Skip to content

Commit

Permalink
Rename/-work find_by_iso_numeric and add specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ct-clearhaus committed Jan 8, 2013
1 parent c51f899 commit 4d0f9e1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/money/currency.rb
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions spec/currency_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 4d0f9e1

Please sign in to comment.