Module: SpookAndPuff::MoneyAttributes
- Defined in:
- lib/spook_and_puff/money_attributes.rb
Overview
A module for extending ActiveRecord::Base subclasses. It provides a macro for defining money accessors; it produces writers which coerce the input into SpookAndPuff::Money instances.
Instance Method Summary collapse
-
#attr_money(*attrs) ⇒ Object
A class macro which defines serialization and attribute writers for the specified attributes.
Instance Method Details
#attr_money(*attrs) ⇒ Object
A class macro which defines serialization and attribute writers for the specified attributes. This ensures that the specified attributes will always be instances of SpookAndPuff::Money.
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 |
# File 'lib/spook_and_puff/money_attributes.rb', line 15 def attr_money(*attrs) attrs.each do |attr| class_eval %{ def #{attr} if self[:#{attr}] @#{attr} ||= SpookAndPuff::Money.new(self[:#{attr}]) end end def #{attr}=(value) if value.nil? @#{attr}, self[:#{attr}] = nil else @#{attr} = case value when SpookAndPuff::Money then value else SpookAndPuff::Money.new(value) end self[:#{attr}] = @#{attr}.raw end @#{attr} end } end nil end |