Sha256: e1f8b1c4a5fd5783507ccbdf2d4c14a14c00443872535c2bef41dbec60127062
Contents?: true
Size: 1.08 KB
Versions: 2
Compression:
Stored size: 1.08 KB
Contents
module CustomMoney module ClassMethods def acts_as_money(*symbols) symbols.each do |symbol| define_method symbol do raw_data_to_money(read_attribute(symbol)) end define_method "#{symbol}=" do |new_value| write_attribute(symbol, round_off_raw_data(new_value)) end end end def validate_currency(*symbols) symbols.each do |symbol| define_method symbol do validate_curr(read_attribute(symbol)) end define_method "#{symbol}=" do |new_value| write_attribute(symbol, new_value) end end end end module InstanceMethods def raw_data_to_money(value) Money.new(value) rescue nil end def round_off_raw_data(value) raw_data_to_money(value).try(:to_d) end def validate_curr(value) errors.add(:currency, "Invalid currency - #{value}") if (Money::Currency.find(value)).nil? value end end def self.included(receiver) receiver.extend ClassMethods receiver.send :include, InstanceMethods end end
Version data entries
2 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
apl-library-0.0.90 | lib/apl-library/custom_money.rb |
apl-library-0.0.90 | vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/lib/apl-library/custom_money.rb |