Module: Exchange::Typecasting
- Defined in:
- lib/exchange/typecasting.rb
Overview
Allows to access properties of an object as currencies The setter takes care of passing the right values to the original setter method, The getter takes care of instantiating a currency of the original getter method, This is framework agnostic. It works in ActiveRecord/Rails, Datamapper, Ohm, Your own, whatever you want
Instance Method Summary collapse
-
#attribute(data) ⇒ Object
Installs a money setter.
- #evaluate_money_option ⇒ Object
-
#exchange_typecasting_alias_method_chain(attribute, setter = nil) ⇒ Object
Install an alias method chain for an attribute.
- #install_currency_error_tester ⇒ Object
-
#install_money_getter(attribute, options = {}) ⇒ Object
Installs a money getter.
- #install_money_option_eval ⇒ Object
-
#install_money_setter(attribute, options = {}) ⇒ Object
Installs a money setter.
-
#money(*attributes, options = {}) ⇒ Object
installs a setter and a getter for the attribute you want to typecast as exchange money.
- #test_for_currency_error ⇒ Object
Instance Method Details
#attribute(data) ⇒ Object
Installs a money setter
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/exchange/typecasting.rb', line 54 def install_money_getter attribute, ={} define_method :"#{attribute}_with_exchange_typecasting" do currency = evaluate_money_option([:currency]) if [:currency] test_for_currency_error(currency, attribute) time = evaluate_money_option([:at]) if [:at] if value = send(:"#{attribute}_without_exchange_typecasting") Exchange::Money.new(value) do |c| c.currency = currency c.time = time if time end end end exchange_typecasting_alias_method_chain attribute end |
#evaluate_money_option ⇒ Object
108 109 110 111 112 |
# File 'lib/exchange/typecasting.rb', line 108 def install_money_option_eval define_method :evaluate_money_option do |option| option.is_a?(Proc) ? instance_eval(&option) : send(option) end end |
#exchange_typecasting_alias_method_chain(attribute, setter = nil) ⇒ Object
Install an alias method chain for an attribute
100 101 102 103 |
# File 'lib/exchange/typecasting.rb', line 100 def exchange_typecasting_alias_method_chain attribute, setter=nil alias_method :"#{attribute}_without_exchange_typecasting#{setter}", :"#{attribute}#{setter}" alias_method :"#{attribute}#{setter}", :"#{attribute}_with_exchange_typecasting#{setter}" end |
#install_currency_error_tester ⇒ Object
117 118 119 120 121 |
# File 'lib/exchange/typecasting.rb', line 117 def install_currency_error_tester define_method :test_for_currency_error do |currency, attribute| raise NoCurrencyError.new("No currency is given for typecasting #{attribute}. Make sure a currency is present") unless currency end end |
#install_money_getter(attribute, options = {}) ⇒ Object
Installs a money getter
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/exchange/typecasting.rb', line 54 def install_money_getter attribute, ={} define_method :"#{attribute}_with_exchange_typecasting" do currency = evaluate_money_option([:currency]) if [:currency] test_for_currency_error(currency, attribute) time = evaluate_money_option([:at]) if [:at] if value = send(:"#{attribute}_without_exchange_typecasting") Exchange::Money.new(value) do |c| c.currency = currency c.time = time if time end end end exchange_typecasting_alias_method_chain attribute end |
#install_money_option_eval ⇒ Object
108 109 110 111 112 |
# File 'lib/exchange/typecasting.rb', line 108 def install_money_option_eval define_method :evaluate_money_option do |option| option.is_a?(Proc) ? instance_eval(&option) : send(option) end end |
#install_money_setter(attribute, options = {}) ⇒ Object
Installs a money setter
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/exchange/typecasting.rb', line 78 def install_money_setter attribute, ={} define_method :"#{attribute}_with_exchange_typecasting=" do |data| att = send(attribute) currency = evaluate_money_option([:currency]) attribute_setter = :"#{attribute}_without_exchange_typecasting=" send(attribute_setter, if !data.respond_to?(:currency) data elsif currency == data.currency data.value elsif currency != data.currency data.to(currency).value end) end exchange_typecasting_alias_method_chain attribute, '=' end |
#money(*attributes, options = {}) ⇒ Object
installs a setter and a getter for the attribute you want to typecast as exchange money
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 |
# File 'lib/exchange/typecasting.rb', line 135 def money *attributes = attributes.last.is_a?(Hash) ? attributes.pop : {} attributes.each do |attribute| # Get the attribute typecasted into money # @return [Exchange::Money] an instance of money # install_money_getter attribute, # Set the attribute either with money or just any data # Implicitly converts values given that are not in the same currency as the currency option evaluates to # @param [Exchange::Money, String, Numberic] data The data to set the attribute to # install_money_setter attribute, end # Evaluates options given either as symbols or as procs # @param [Symbol, Proc] option The option to evaluate # install_money_option_eval # Evaluates whether an error should be raised because there is no currency present # @param [Symbol] currency The currency, if given # install_currency_error_tester end |
#test_for_currency_error ⇒ Object
117 118 119 120 121 |
# File 'lib/exchange/typecasting.rb', line 117 def install_currency_error_tester define_method :test_for_currency_error do |currency, attribute| raise NoCurrencyError.new("No currency is given for typecasting #{attribute}. Make sure a currency is present") unless currency end end |