-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathmonetize.rb
More file actions
84 lines (67 loc) · 2.74 KB
/
monetize.rb
File metadata and controls
84 lines (67 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'money'
require 'monetize/core_extensions'
require 'monetize/errors'
require 'monetize/version'
require 'monetize/parser'
require 'monetize/collection'
module Monetize
# Class methods
class << self
# @attr_accessor [true, false] assume_from_symbol Use this to enable the
# ability to assume the currency from a passed symbol
attr_accessor :assume_from_symbol
# 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.
attr_accessor :enforce_currency_delimiters
# 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.
attr_accessor :expect_whole_subunits
def parse(input, currency = Money.default_currency, options = {})
parse! input, currency, options
rescue Error
nil
end
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
def parse_collection(input, currency = Money.default_currency, options = {})
Collection.parse(input, currency, options)
end
def from_string(value, currency = Money.default_currency)
value = BigDecimal(value.to_s)
Money.from_amount(value, currency)
end
def from_fixnum(value, currency = Money.default_currency)
Money.from_amount(value, currency)
end
alias_method :from_integer, :from_fixnum
def from_float(value, currency = Money.default_currency)
Money.from_amount(value, currency)
end
def from_bigdecimal(value, currency = Money.default_currency)
Money.from_amount(value, currency)
end
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
def register_currency_symbol(symbol, iso_code)
Monetize::Parser.register_currency_symbol(symbol, iso_code)
end
def unregister_currency_symbol(symbol)
Monetize::Parser.unregister_currency_symbol(symbol)
end
def reset_currency_symbols!
Monetize::Parser.reset_currency_symbols!
end
end
end