Netzke::ActiveRecord::Attributes

Public Instance Methods

set_value_for_attribute(a, v) click to toggle source
     # File lib/netzke/active_record/attributes.rb, line 168
168:   def set_value_for_attribute(a, v)
169:     if a[:setter]
170:       a[:setter].call(self, v)
171:     elsif a[:nested_attribute] && a[:name].to_s.index("__")
172:       # We want:
173:       #     set_value_for_attribute({:name => :assoc_1__assoc_2__method, :nested_attribute => true}, 100)
174:       # ~>
175:       #     self.assoc_1.assoc_2.method = 100
176:       split = a[:name].to_s.split(/\.|__/)
177:       split.inject(self) { |r,m| m == split.last ? (r && r.send("#{m}=", v) && r.save) : r.send(m) }
178:     elsif respond_to?("#{a[:name]}=")
179:       send("#{a[:name]}=", v)
180:     end
181:   end
to_array(attributes) click to toggle source

Transforms a record to array of values according to the passed attributes

     # File lib/netzke/active_record/attributes.rb, line 142
142:   def to_array(attributes)
143:     res = []
144:     for a in attributes
145:       next if a[:included] == false
146:       res << value_for_attribute(a)
147:     end
148:     res
149:   end
to_hash(attributes) click to toggle source

Accepts both hash and array of attributes

     # File lib/netzke/active_record/attributes.rb, line 152
152:   def to_hash(attributes)
153:     res = {}
154:     for a in (attributes.is_a?(Hash) ? attributes.values : attributes)
155:       next if a[:included] == false
156:       res[a[:name].to_sym] = self.value_for_attribute(a)
157:     end
158:     res
159:   end
value_for_attribute(a) click to toggle source
     # File lib/netzke/active_record/attributes.rb, line 161
161:   def value_for_attribute(a)
162:     v = a[:getter] ? a[:getter].call(self) : send(a[:name])
163:     # a work-around for to_json not taking the current timezone into account when serializing ActiveSupport::TimeWithZone
164:     v = v.to_datetime.to_s(:db) if v.is_a?(ActiveSupport::TimeWithZone)
165:     v
166:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.