Module: Exchange::ErrorSafe
- Defined in:
- lib/exchange/core_extensions/float/error_safe.rb
Overview
Make Floating Points forget about their incapabilities when dealing with money
Class Method Summary collapse
- .included(base) ⇒ Object
-
.money_error_preventing_method_chain(base, meth) ⇒ Object
Installs a method chain that overwrites the old error prone meth with the new one.
- .prevent_errors_with_exchange_for(base, meth) ⇒ Object
Class Method Details
.included(base) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/exchange/core_extensions/float/error_safe.rb', line 31 def self.included base %W(* / + -).each do |meth| # @macro prevent_errors_with_exchange_for # prevent_errors_with_exchange_for base, meth.to_sym end end |
.money_error_preventing_method_chain(base, meth) ⇒ Object
Installs a method chain that overwrites the old error prone meth with the new one
10 11 12 13 |
# File 'lib/exchange/core_extensions/float/error_safe.rb', line 10 def self.money_error_preventing_method_chain base, meth base.send :alias_method, :"#{meth}_with_errors", meth base.send :alias_method, meth, :"#{meth}_without_errors" end |
.prevent_errors_with_exchange_for(base, meth) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/exchange/core_extensions/float/error_safe.rb', line 20 def self.prevent_errors_with_exchange_for base, meth base.send(:define_method, :"#{meth}_without_errors", lambda { |other| if other.is_a?(Exchange::Money) BigDecimal.new(self.to_s).send(meth, other.value).to_f else send(:"#{meth}_with_errors", other) end }) money_error_preventing_method_chain base, meth end |