Method: Money::Parsing::ClassMethods#parse
- Defined in:
- lib/money/money/parsing.rb
#parse(input, currency = nil) ⇒ Money
Parses the current string and converts it to a Money object. Excess characters will be discarded.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/money/money/parsing.rb', line 36 def parse(input, currency = nil) i = input.to_s.strip # raise Money::Currency.table.collect{|c| c[1][:symbol]}.inspect # Check the first character for a currency symbol, alternatively get it # from the stated currency string c = if Money.assume_from_symbol && i =~ /^(\$|€|£)/ case i when /^$/ then "USD" when /^€/ then "EUR" when /^£/ then "GBP" end else i[/[A-Z]{2,3}/] end # check that currency passed and embedded currency are the same, # and negotiate the final currency if currency.nil? and c.nil? currency = Money.default_currency elsif currency.nil? currency = c elsif c.nil? currency = currency elsif currency != c # TODO: ParseError raise ArgumentError, "Mismatching Currencies" end currency = Money::Currency.wrap(currency) cents = extract_cents(i, currency) new(cents, currency) end |