Module: TableSalt::MultiparameterAssignment
- Included in:
- TableSalt
- Defined in:
- lib/table_salt.rb
Instance Method Summary collapse
-
#assign_multiparameter_attributes(pairs) ⇒ Object
Instantiates objects for all attribute classes that needs more than one constructor parameter.
- #collect_param_parts(pairs) ⇒ Object
- #instantiate_time_object(name, values) ⇒ Object
Instance Method Details
#assign_multiparameter_attributes(pairs) ⇒ Object
Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done by calling new on the column type or aggregation type (through composed_of) object with these parameters. So having the pairs written_on(1) = “2004”, written_on(2) = “6”, written_on(3) = “24”, will instantiate written_on (a date type) with Date.new(“2004”, “6”, “24”). You can also specify a typecast character in the parentheses to have the parameters typecasted before they’re used in the constructor. Use i for Fixnum, f for Float, s for String, and a for Array. If all the values for a given attribute are empty, the attribute will be set to nil.
77 78 79 80 81 82 83 |
# File 'lib/table_salt.rb', line 77 def assign_multiparameter_attributes( pairs ) collect_param_parts( pairs ).each do |attribute, parts| parts = parts.sort { |a,b| a.first <=> b.first }.map { |k,v| v.to_i } instance = instantiate_time_object( attribute, parts ) send "#{attribute}=", instance end end |
#collect_param_parts(pairs) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/table_salt.rb', line 85 def collect_param_parts( pairs ) datetime_params = {} pairs.each do |key, value| attribute_name, date_part = key.match( /^(.*)\((\di)\)$/ )[1..2] if attribute_name datetime_params[attribute_name] ||= {} datetime_params[attribute_name][date_part] = value end end datetime_params end |
#instantiate_time_object(name, values) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/table_salt.rb', line 100 def instantiate_time_object( name, values ) DateTime.new *values # this will throw an exception if the date is not valid (Time#local does not) Time.zone.local *values rescue self.errors.add name, 'invalid date or time' nil end |