Module: Monetize

Defined in:
lib/monetize.rb,
lib/monetize/errors.rb,
lib/monetize/parser.rb,
lib/monetize/version.rb,
lib/monetize/collection.rb

Defined Under Namespace

Classes: ArgumentError, Collection, Error, ParseError, Parser

Constant Summary collapse

VERSION =
'2.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.assume_from_symbolObject

Returns the value of attribute assume_from_symbol.



13
14
15
# File 'lib/monetize.rb', line 13

def assume_from_symbol
  @assume_from_symbol
end

.enforce_currency_delimitersObject

Monetize uses the delimiters set in the currency to separate integers from decimals, and to ignore thousands separators. In some corner cases, though, it will try to determine the correct separator by itself. Set this to true to enforce the delimiters set in the currency all the time.



19
20
21
# File 'lib/monetize.rb', line 19

def enforce_currency_delimiters
  @enforce_currency_delimiters
end

.expect_whole_subunitsObject

Where this set to true, the behavior for parsing thousands separators is changed to expect that eg. €10.000 is EUR 10 000 and not EUR 10.000 - it’s incredibly rare when parsing human text that we’re dealing with fractions of cents.



25
26
27
# File 'lib/monetize.rb', line 25

def expect_whole_subunits
  @expect_whole_subunits
end

Class Method Details

.from_bigdecimal(value, currency = Money.default_currency) ⇒ Object



63
64
65
# File 'lib/monetize.rb', line 63

def from_bigdecimal(value, currency = Money.default_currency)
  Money.from_amount(value, currency)
end

.from_fixnum(value, currency = Money.default_currency) ⇒ Object Also known as: from_integer



54
55
56
# File 'lib/monetize.rb', line 54

def from_fixnum(value, currency = Money.default_currency)
  Money.from_amount(value, currency)
end

.from_float(value, currency = Money.default_currency) ⇒ Object



59
60
61
# File 'lib/monetize.rb', line 59

def from_float(value, currency = Money.default_currency)
  Money.from_amount(value, currency)
end

.from_numeric(value, currency = Money.default_currency) ⇒ Object



67
68
69
70
# File 'lib/monetize.rb', line 67

def from_numeric(value, currency = Money.default_currency)
  fail ArgumentError, "'value' should be a type of Numeric" unless value.is_a?(Numeric)
  Money.from_amount(value, currency)
end

.from_string(value, currency = Money.default_currency) ⇒ Object



49
50
51
52
# File 'lib/monetize.rb', line 49

def from_string(value, currency = Money.default_currency)
  value = BigDecimal(value.to_s)
  Money.from_amount(value, currency)
end

.parse(input, currency = Money.default_currency, options = {}) ⇒ Object



27
28
29
30
31
# File 'lib/monetize.rb', line 27

def parse(input, currency = Money.default_currency, options = {})
  parse! input, currency, options
rescue Error
  nil
end

.parse!(input, currency = Money.default_currency, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/monetize.rb', line 33

def parse!(input, currency = Money.default_currency, options = {})
  return input if input.is_a?(Money)
  return from_numeric(input, currency) if input.is_a?(Numeric)

  parser = Monetize::Parser.new(input, currency, options)
  amount, currency = parser.parse

  Money.from_amount(amount, currency)
rescue Money::Currency::UnknownCurrency => e
  fail ParseError, e.message
end

.parse_collection(input, currency = Money.default_currency, options = {}) ⇒ Object



45
46
47
# File 'lib/monetize.rb', line 45

def parse_collection(input, currency = Money.default_currency, options = {})
  Collection.parse(input, currency, options)
end