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 =
'1.13.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.assume_from_symbolObject

Returns the value of attribute assume_from_symbol.



15
16
17
# File 'lib/monetize.rb', line 15

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.



21
22
23
# File 'lib/monetize.rb', line 21

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.



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

def expect_whole_subunits
  @expect_whole_subunits
end

Class Method Details

.extract_cents(input, currency = Money.default_currency) ⇒ Object



74
75
76
77
78
79
# File 'lib/monetize.rb', line 74

def extract_cents(input, currency = Money.default_currency)
  warn '[DEPRECATION] Monetize.extract_cents is deprecated. Use Monetize.parse().cents'

  money = parse(input, currency)
  money.cents if money
end

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



65
66
67
# File 'lib/monetize.rb', line 65

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



56
57
58
# File 'lib/monetize.rb', line 56

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

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



61
62
63
# File 'lib/monetize.rb', line 61

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

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



69
70
71
72
# File 'lib/monetize.rb', line 69

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



51
52
53
54
# File 'lib/monetize.rb', line 51

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



29
30
31
32
33
# File 'lib/monetize.rb', line 29

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

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



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

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



47
48
49
# File 'lib/monetize.rb', line 47

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