Module: FixedPointField
- Defined in:
- lib/fixed_point_field.rb
Overview
Fixed Point Field specifies that a field that is accessed like a float from a rails script should be stored as an integer in the database. This is only practical when the field always has a fixed number of digits after the decimal place, like how US Dollars have 2 digits of cents after the decimal. More information and examples are available in the README.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(kls) ⇒ Object
When the module is included, in addition to adding the methods to instances of the class, we need to add methods to the class object, do this with extend.
Instance Method Summary collapse
-
#read_fixed_point(column_name) ⇒ Object
Retrieve the raw value of the field, which will be a Fixnum.
-
#read_floating_point(column_name, width = 2, base = 10) ⇒ Object
Reads the fixed point version and converts.
-
#set_fixed_point(column_name, value) ⇒ Object
Set a fixed point column to the specificed value (fixed).
-
#set_floating_point(column_name, value, width = 2, base = 10) ⇒ Object
Set a column using a floating point column.
Class Method Details
.included(kls) ⇒ Object
When the module is included, in addition to adding the methods to instances of the class, we need to add methods to the class object, do this with extend.
33 34 35 |
# File 'lib/fixed_point_field.rb', line 33 def self.included kls kls.send :extend, ClassMethods end |
Instance Method Details
#read_fixed_point(column_name) ⇒ Object
Retrieve the raw value of the field, which will be a Fixnum. This is wrapped behind the default getter, so it is fetched with this function, then down-converted and returned.
61 62 63 |
# File 'lib/fixed_point_field.rb', line 61 def read_fixed_point(column_name) read_attribute(column_name) end |
#read_floating_point(column_name, width = 2, base = 10) ⇒ Object
Reads the fixed point version and converts. Will return nil if the column value is nil. If you had called: fixed_point_field :price the method price would be a direct call to this function.
69 70 71 |
# File 'lib/fixed_point_field.rb', line 69 def read_floating_point(column_name, width = 2, base = 10) (rv = read_fixed_point(column_name)) ? (rv.to_f / (base**width)) : nil end |
#set_fixed_point(column_name, value) ⇒ Object
Set a fixed point column to the specificed value (fixed). This is wrapped behind a conversion for the default assignment operator, such that if you had called: fixed_point_field :price the method price= would convert and then call this function.
42 43 44 |
# File 'lib/fixed_point_field.rb', line 42 def set_fixed_point(column_name, value) write_attribute(column_name, value) end |
#set_floating_point(column_name, value, width = 2, base = 10) ⇒ Object
Set a column using a floating point column. This calls set_fixed_point after it has up-scaled the value to the specified number of digits. By default the width is 2 and base is 10, to work with USD currency. If you had called: fixed_point_field :price the method price= would be a direct call to this function.
53 54 55 56 |
# File 'lib/fixed_point_field.rb', line 53 def set_floating_point(column_name, value, width = 2, base = 10) return if value == '' set_fixed_point(column_name, (value.to_f * (base**width)).round) end |