Module: EasyMoney::ClassMethods
- Defined in:
- lib/easy_money.rb
Instance Method Summary collapse
-
#easy_money(method, *args) ⇒ Object
Creates an money instance method for the given method, named “#method_money” which returns a formatted money string, and a #method_money= method used to set an edited money string.
Instance Method Details
#easy_money(method, *args) ⇒ Object
Creates an money instance method for the given method, named “#method_money” which returns a formatted money string, and a #method_money= method used to set an edited money string. The original method stores the value as integer (cents, or other precision/currency setting). Options:
-
:money_method - Use this as the alternative name to the money-access methods
-
:units - Use this as an alternative suffix name to the money methods (‘dollars’ gives ‘xx_dollars’)
-
:precision - The number of digits implied after the decimal, default is 2
-
:separator - The character to use after the integer part, default is ‘.’
-
:delimiter - The character to use between every 3 digits of the integer part, default none
-
:positive - The sprintf format to use for positive numbers, default is based on precision
-
:negative - The sprintf format to use for negative numbers, default is same as :positive
-
:zero - The sprintf format to use for zero, default is same as :positive
-
:nil - The sprintf format to use for nil values, default none
-
:unit - Prepend this to the front of the money value, say ‘$’, default none
-
:blank - Return this value when the money string is empty or has no digits on assignment
-
:negative_regex - A Regular Expression used to determine if a number is negative (and without a - sign)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/easy_money.rb', line 26 def easy_money(method, *args) opt = args.last.is_a?(Hash) ? args.pop : {} money_method = opt.delete(:money_method) || "#{method}_#{opt.delete(:units)||'money'}" class_eval %Q( def #{money_method}(*args) opt = args.last.is_a?(Hash) ? args.pop : {} EasyMoney.integer_to_money( #{method}, #{opt.inspect}.merge(opt)) end def #{money_method}=(v, *args) opt = args.last.is_a?(Hash) ? args.pop : {} self.#{method} = EasyMoney.money_to_integer( v, #{opt.inspect}.merge(opt)) end ) end |