Module: Monetize
- Defined in:
- lib/monetize.rb,
lib/collection.rb,
lib/monetize/version.rb
Defined Under Namespace
Classes: Collection
Constant Summary collapse
- CURRENCY_SYMBOLS =
{ "$" => "USD", "€" => "EUR", "£" => "GBP", "₤" => "GBP", "R$" => "BRL", "R" => "ZAR", "¥" => "JPY", "C$" => "CAD" }
- MULTIPLIER_SUFFIXES =
{ "K" => 3, "M" => 6, "B" => 9, "T" => 12 }
- MULTIPLIER_REGEXP =
Regexp.new('\d(%s)\b[^\d]*$' % MULTIPLIER_SUFFIXES.keys.join('|'), 'i')
- VERSION =
"1.3.1"
Class Attribute Summary collapse
-
.assume_from_symbol ⇒ Object
Returns the value of attribute assume_from_symbol.
Class Method Summary collapse
- .extract_cents(input, currency = Money.default_currency) ⇒ Object
- .from_bigdecimal(value, currency = Money.default_currency) ⇒ Object
- .from_fixnum(value, currency = Money.default_currency) ⇒ Object
- .from_float(value, currency = Money.default_currency) ⇒ Object
- .from_numeric(value, currency = Money.default_currency) ⇒ Object
- .from_string(value, currency = Money.default_currency) ⇒ Object
- .parse(input, currency = Money.default_currency, options = {}) ⇒ Object
- .parse_collection(input, currency = Money.default_currency, options = {}) ⇒ Object
Class Attribute Details
.assume_from_symbol ⇒ Object
Returns the value of attribute assume_from_symbol.
34 35 36 |
# File 'lib/monetize.rb', line 34 def assume_from_symbol @assume_from_symbol end |
Class Method Details
.extract_cents(input, currency = Money.default_currency) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/monetize.rb', line 92 def self.extract_cents(input, currency = Money.default_currency) multiplier_suffix = (matches = MULTIPLIER_REGEXP.match(input)) ? matches[1].upcase : nil multiplier_exp = MULTIPLIER_SUFFIXES[multiplier_suffix] num = input.gsub(/[^\d.,'-]/, '') negative = num =~ /^-|-$/ ? true : false decimal_char = currency.decimal_mark num = num.sub(/^-|-$/, '') if negative if num.include?('-') raise ArgumentError, "Invalid currency amount (hyphen)" end num.chop! if num.match(/[\.|,]$/) used_delimiters = num.scan(/[^\d]/) case used_delimiters.uniq.length when 0 major, minor = num, 0 when 2 thousands_separator, decimal_mark = used_delimiters.uniq major, minor = num.gsub(thousands_separator, '').split(decimal_mark) min = 0 unless min when 1 decimal_mark = used_delimiters.first if decimal_char == decimal_mark major, minor = num.split(decimal_char) else if num.scan(decimal_mark).length > 1 # multiple matches; treat as decimal_mark major, minor = num.gsub(decimal_mark, ''), 0 else possible_major, possible_minor = num.split(decimal_mark) possible_major ||= "0" possible_minor ||= "00" if possible_minor.length != 3 # thousands_separator major, minor = possible_major, possible_minor else if possible_major.length > 3 major, minor = possible_major, possible_minor else if decimal_mark == '.' major, minor = possible_major, possible_minor else major, minor = "#{possible_major}#{possible_minor}", 0 end end end end end else raise ArgumentError, "Invalid currency amount" end cents = major.to_i * currency.subunit_to_unit cents *= (10 ** multiplier_exp) minor = minor.to_s + ('0' * multiplier_exp) shift = minor[0 ... multiplier_exp].to_i * 100 cents += shift minor = (minor[multiplier_exp .. -1] || '') minor = if minor.size < currency.decimal_places (minor + ("0" * currency.decimal_places))[0,currency.decimal_places].to_i elsif minor.size > currency.decimal_places if minor[currency.decimal_places,1].to_i >= 5 minor[0,currency.decimal_places].to_i+1 else minor[0,currency.decimal_places].to_i end else minor.to_i end cents += minor negative ? cents * -1 : cents end |
.from_bigdecimal(value, currency = Money.default_currency) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/monetize.rb', line 73 def self.from_bigdecimal(value, currency = Money.default_currency) currency = Money::Currency.wrap(currency) value = value * currency.subunit_to_unit value = value.round unless Money.infinite_precision Money.new(value, currency) end |
.from_fixnum(value, currency = Money.default_currency) ⇒ Object
62 63 64 65 66 |
# File 'lib/monetize.rb', line 62 def self.from_fixnum(value, currency = Money.default_currency) currency = Money::Currency.wrap(currency) value = value * currency.subunit_to_unit Money.new(value, currency) end |
.from_float(value, currency = Money.default_currency) ⇒ Object
68 69 70 71 |
# File 'lib/monetize.rb', line 68 def self.from_float(value, currency = Money.default_currency) value = BigDecimal.new(value.to_s) from_bigdecimal(value, currency) end |
.from_numeric(value, currency = Money.default_currency) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/monetize.rb', line 80 def self.from_numeric(value, currency = Money.default_currency) case value when Fixnum from_fixnum(value, currency) when Numeric value = BigDecimal.new(value.to_s) from_bigdecimal(value, currency) else raise ArgumentError, "'value' should be a type of Numeric" end end |
.from_string(value, currency = Money.default_currency) ⇒ Object
57 58 59 60 |
# File 'lib/monetize.rb', line 57 def self.from_string(value, currency = Money.default_currency) value = BigDecimal.new(value.to_s) from_bigdecimal(value, currency) end |
.parse(input, currency = Money.default_currency, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/monetize.rb', line 37 def self.parse(input, currency = Money.default_currency, = {}) input = input.to_s.strip computed_currency = if .fetch(:assume_from_symbol) { assume_from_symbol } compute_currency(input) else input[/[A-Z]{2,3}/] end currency = computed_currency || currency || Money.default_currency currency = Money::Currency.wrap(currency) fractional = extract_cents(input, currency) Money.new(fractional, currency) end |
.parse_collection(input, currency = Money.default_currency, options = {}) ⇒ Object
53 54 55 |
# File 'lib/monetize.rb', line 53 def self.parse_collection(input, currency = Money.default_currency, = {}) Collection.parse(input, currency, ) end |