Module: EasyMoney
- Defined in:
- lib/easy_money.rb
Overview
EasyMoney - Ruby class mixin library to add money helpers to attribute/methods
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.float_to_integer(value, *args) ⇒ Object
Returns the integer (cents) value from a Float.
-
.format_money(value, pattern = "%.2m", *args) ⇒ Object
Replacing the sprintf function to deal with money as float.
-
.included(base) ⇒ Object
:nodoc:.
- .integer_to_float(value, *args) ⇒ Object
-
.integer_to_money(value, *args) ⇒ Object
Returns the money string of the given integer value.
-
.money_to_integer(value, *args) ⇒ Object
Returns the integer of the given money string.
Class Method Details
.float_to_integer(value, *args) ⇒ Object
Returns the integer (cents) value from a Float
100 101 102 103 104 |
# File 'lib/easy_money.rb', line 100 def self.float_to_integer(value, *args) opt = args.last.is_a?(Hash) ? args.pop : {} return (opt[:blank]||nil) if value.nil? value = (value.to_f*(10**((opt[:precision]||2)+1))).to_i/10 # helps rounding 4.56 -> 455 ouch! end |
.format_money(value, pattern = "%.2m", *args) ⇒ Object
Replacing the sprintf function to deal with money as float. “… %[flags]m …”
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/easy_money.rb', line 107 def self.format_money(value, pattern="%.2m", *args) opt = args.last.is_a?(Hash) ? args.pop : {} sign = value < 0 ? -1 : 1 dollars, cents = value.abs.divmod( 10 ** (opt[:precision]||2)) dollars *= sign parts = pattern.match(/^(.*)%([-\. \d+0]*)[fm](.*)/) return pattern unless parts intdec = parts[2].match(/(.*)\.(\d*)/) dprec, cprec = intdec ? [intdec[1], intdec[2]] : ['', ''] dollars = sprintf("%#{dprec}d", dollars) cents = '0' + cents.to_s while cents.to_s.length < (opt[:precision]||2) cents = cents.to_s[0,cprec.to_i] cents = cents + '0' while cents.length < cprec.to_i value = parts[1] + "#{(dollars.to_i==0 && sign==-1) ? '-' : '' }#{dollars}#{cents>' '? '.':''}#{cents}" + parts[3] value end |
.included(base) ⇒ Object
:nodoc:
4 5 6 |
# File 'lib/easy_money.rb', line 4 def self.included(base) #:nodoc: base.extend( ClassMethods ) end |
.integer_to_float(value, *args) ⇒ Object
72 73 74 75 76 |
# File 'lib/easy_money.rb', line 72 def self.integer_to_float(value, *args) opt = args.last.is_a?(Hash) ? args.pop : {} return (opt[:blank]||nil) if value.nil? value = 1.0 * value / (10**(opt[:precision]||2)) end |
.integer_to_money(value, *args) ⇒ Object
Returns the money string of the given integer value. Uses relevant options from #easy_money
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 |
# File 'lib/easy_money.rb', line 45 def self.integer_to_money(value, *args) opt = args.last.is_a?(Hash) ? args.pop : {} opt[:positive] ||= "%.#{opt[:precision]||2}f" pattern = if value.nil? value = 0 opt[:nil] || opt[:positive] else case value <=> 0 when 1 then opt[:positive] when 0 then opt[:zero] || opt[:positive] else value = -value if opt[:negative] && opt[:negative] != opt[:positive] opt[:negative] || opt[:positive] end end value = self.format_money( value, pattern, opt) value = opt[:unit]+value if opt[:unit] value.gsub!(/\./,opt[:separator]) if opt[:separator] if opt[:delimiter] && (m = value.match(/^(\D*)(\d+)(.*)/)) # Adapted From Rails' ActionView::Helpers::NumberHelper n = m[2].gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{opt[:delimiter]}") value=m[1]+n+m[3] end value end |
.money_to_integer(value, *args) ⇒ Object
Returns the integer of the given money string. Uses relevant options from #easy_money
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/easy_money.rb', line 79 def self.money_to_integer(value, *args) opt = args.last.is_a?(Hash) ? args.pop : {} value.gsub!(opt[:delimiter],'') if opt[:delimiter] value.gsub!(opt[:separator],'.') if opt[:separator] value.gsub!(/^[^\d\.\-\,]+/,'') return (opt[:blank]||nil) unless value =~ /\d/ m = value.to_s.match(opt[:negative_regex]||/^(-?)(.+\d)\s*cr/i) value = value.match(/^-/) ? m[2] : "-#{m[2]}" if m && m[2] # Money string ("123.45") to proper integer withough passing through the float transformation match = value.match(/(-?\d*)\.?(\d*)/) return 0 unless match value = match[1].to_i * (10 ** (opt[:precision]||2)) cents = match[2] cents = cents + '0' while cents.length < (opt[:precision]||2) cents = cents.to_s[0,opt[:precision]||2] value += cents.to_i * (value<0 ? -1 : 1) value end |