Class: Money::Parser::Fuzzy

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

Direct Known Subclasses

Accounting

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/parser/fuzzy.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) ⇒ Money

Deprecated.

Parses an input string and attempts to find the decimal separator based on certain heuristics, like the amount decimals for the fractional part a currency has or the incorrect notion a currency has a defined decimal separator (this is a property of the locale). While these heuristics can lead to the expected result for some cases, the other cases can lead to surprising results such as parsed amounts being 1000x larger than intended.

Parameters:

Returns:

Raises:



74
75
76
77
78
# File 'lib/money/parser/fuzzy.rb', line 74

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