Class: MoneyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/money/money_parser.rb

Overview

Parse an amount from a string

Direct Known Subclasses

AccountingMoneyParser

Defined Under Namespace

Classes: MoneyFormatError

Constant Summary collapse

MARKS =
%w[. , ·  ˙ '] + [' ']
ESCAPED_MARKS =
Regexp.escape(MARKS.join)
ESCAPED_NON_SPACE_MARKS =
Regexp.escape((MARKS - [' ']).join)
ESCAPED_NON_DOT_MARKS =
Regexp.escape((MARKS - ['.']).join)
ESCAPED_NON_COMMA_MARKS =
Regexp.escape((MARKS - [',']).join)
NUMERIC_REGEX =
/(
  [\+\-]?
  [\d#{ESCAPED_NON_SPACE_MARKS}][\d#{ESCAPED_MARKS}]*
)/ix
DOT_DECIMAL_REGEX =

1,234,567.89

/\A
  [\+\-]?
  (?:
    (?:\d+)
    (?:[#{ESCAPED_NON_DOT_MARKS}]\d{3})+
    (?:\.\d{2,})?
  )
\z/ix
COMMA_DECIMAL_REGEX =

1.234.567,89

/\A
  [\+\-]?
  (?:
    (?:\d+)
    (?:[#{ESCAPED_NON_COMMA_MARKS}]\d{3})+
    (?:\,\d{2,})?
  )
\z/ix
INDIAN_NUMERIC_REGEX =

12,34,567.89

/\A
  [\+\-]?
  (?:
    (?:\d+)
    (?:\,\d{2})+
    (?:\,\d{3})
    (?:\.\d{2})?
  )
\z/ix
CHINESE_NUMERIC_REGEX =

1,1123,4567.89

/\A
  [\+\-]?
  (?:
    (?:\d+)
    (?:\,\d{4})+
    (?:\.\d{2})?
  )
\z/ix

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(input, currency = nil, **options) ⇒ Object



60
61
62
# File 'lib/money/money_parser.rb', line 60

def self.parse(input, currency = nil, **options)
  new.parse(input, currency, **options)
end

Instance Method Details

#parse(input, currency = nil, strict: false) ⇒ Object



64
65
66
67
68
# File 'lib/money/money_parser.rb', line 64

def parse(input, currency = nil, strict: false)
  currency = Money::Helpers.value_to_currency(currency)
  amount = extract_amount_from_string(input, currency, strict)
  Money.new(amount, currency)
end