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_code
option), if available;nil
if 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.
213 214 215 216 |
# File 'lib/invoicing/currency_value.rb', line 213 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.
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/invoicing/currency_value.rb', line 280 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(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
.
263 264 265 |
# File 'lib/invoicing/currency_value.rb', line 263 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.
254 255 256 257 258 259 260 |
# File 'lib/invoicing/currency_value.rb', line 254 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).
269 270 271 272 273 274 275 276 |
# File 'lib/invoicing/currency_value.rb', line 269 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
.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/invoicing/currency_value.rb', line 219 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 |