Class: Invoicing::CurrencyValue::ClassInfo
- Inherits:
-
Invoicing::ClassInfo::Base
- Object
- Invoicing::ClassInfo::Base
- Invoicing::CurrencyValue::ClassInfo
- Defined in:
- lib/invoicing/currency_value.rb
Overview
:nodoc:
Instance Attribute Summary
Attributes inherited from Invoicing::ClassInfo::Base
#all_args, #all_options, #current_args, #current_options, #model_class, #new_args, #previous_info
Instance Method Summary collapse
-
#attr_conversion_input(object, attr) ⇒ Object
If other modules have registered callbacks for the event of reading a rounded attribute, they are executed here.
-
#currency_info_for(object) ⇒ Object
Returns a hash of information about the currency used by model
object. -
#currency_of(object) ⇒ Object
Returns the value of the currency code column of
object, if available; otherwise the default currency code (set by the:currency_codeoption), if available;nilif all else fails. -
#format_value(object, value, options = {}) ⇒ Object
Formats a numeric value as a nice currency string in UTF-8 encoding.
-
#generate_attrs(attr) ⇒ Object
Generates the getter and setter method for attribute
attr. -
#initialize(model_class, previous_info, args) ⇒ ClassInfo
constructor
A new instance of ClassInfo.
Methods inherited from Invoicing::ClassInfo::Base
#get, #method, #option_defaults, #set
Constructor Details
#initialize(model_class, previous_info, args) ⇒ ClassInfo
Returns a new instance of ClassInfo.
206 207 208 209 |
# File 'lib/invoicing/currency_value.rb', line 206 def initialize(model_class, previous_info, args) super new_args.each{|attr| generate_attrs(attr)} end |
Instance Method Details
#attr_conversion_input(object, attr) ⇒ Object
If other modules have registered callbacks for the event of reading a rounded attribute, they are executed here. attr is the name of the attribute being read.
273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/invoicing/currency_value.rb', line 273 def attr_conversion_input(object, attr) value = nil if callback = [:conversion_input] value = object.send(callback, attr) end unless value raw_value = object.read_attribute(attr) value = BigDecimal.new(raw_value.to_s) unless raw_value.nil? end value end |
#currency_info_for(object) ⇒ Object
Returns a hash of information about the currency used by model object.
256 257 258 |
# File 'lib/invoicing/currency_value.rb', line 256 def currency_info_for(object) ::Invoicing::CurrencyValue::Formatter.currency_info(currency_of(object), ) end |
#currency_of(object) ⇒ Object
Returns the value of the currency code column of object, if available; otherwise the default currency code (set by the :currency_code option), if available; nil if all else fails.
247 248 249 250 251 252 253 |
# File 'lib/invoicing/currency_value.rb', line 247 def currency_of(object) if object.attributes.has_key?(method(:currency)) || object.respond_to?(method(:currency)) get(object, :currency) else [:currency_code] end end |
#format_value(object, value, options = {}) ⇒ Object
Formats a numeric value as a nice currency string in UTF-8 encoding. object is the model object carrying the value (used to determine the currency).
262 263 264 265 266 267 268 269 |
# File 'lib/invoicing/currency_value.rb', line 262 def format_value(object, value, ={}) = .merge().symbolize_keys intercept = [:value_for_formatting] if intercept && object.respond_to?(intercept) value = object.send(intercept, value, ) end ::Invoicing::CurrencyValue::Formatter.format_value(currency_of(object), value, ) end |
#generate_attrs(attr) ⇒ Object
Generates the getter and setter method for attribute attr.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/invoicing/currency_value.rb', line 212 def generate_attrs(attr) model_class.class_eval do define_method(attr) do currency_info = currency_value_class_info.currency_info_for(self) return read_attribute(attr) if currency_info.nil? round_factor = BigDecimal(currency_info[:round].to_s) value = currency_value_class_info.attr_conversion_input(self, attr) value.nil? ? nil : (value / round_factor).round * round_factor end define_method("#{attr}=") do |new_value| write_attribute(attr, new_value) end define_method("#{attr}_formatted") do |*args| = args.first || {} value_as_float = begin Kernel.Float(send("#{attr}_before_type_cast")) rescue ArgumentError, TypeError nil end if value_as_float.nil? '' else format_currency_value(value_as_float, .merge({:method_name => attr})) end end end end |