lib/rasti/db/model.rb in rasti-db-0.4.0 vs lib/rasti/db/model.rb in rasti-db-0.4.1
- old
+ new
@@ -16,11 +16,11 @@
class << self
def [](*attribute_names)
Class.new(self) do
- attribute *attribute_names
+ attribute(*attribute_names)
def self.inherited(subclass)
subclass.instance_variable_set :@attributes, attributes.dup
end
end
@@ -80,29 +80,34 @@
alias_method :inspect, :to_s
def to_h
self.class.attributes.each_with_object({}) do |name, hash|
if attributes.key? name
- case attributes[name]
+ value = fetch_attribute name
+ case value
when Model
- hash[name] = attributes[name].to_h
+ hash[name] = value.to_h
when Array
- hash[name] = attributes[name].map do |e|
+ hash[name] = value.map do |e|
e.is_a?(Model) ? e.to_h : e
end
else
- hash[name] = attributes[name]
+ hash[name] = value
end
end
end
end
private
attr_reader :attributes
def fetch_attribute(name)
- attributes.key?(name) ? attributes[name] : raise(UninitializedAttributeError, name)
+ attributes.key?(name) ? casted_attribute(name) : raise(UninitializedAttributeError, name)
+ end
+
+ def casted_attribute(name)
+ attributes[name].is_a?(Time) ? Timing::TimeInZone.new(attributes[name]) : attributes[name]
end
end
end
end
\ No newline at end of file