lib/frenchy/model.rb in frenchy-0.0.4 vs lib/frenchy/model.rb in frenchy-0.0.5
- old
+ new
@@ -11,11 +11,11 @@
base.extend(ClassMethods)
end
# Create a new instance of this model with the given attributes
def initialize(attrs={})
- self.class.defaults.merge(attrs).each do |k,v|
+ self.class.defaults.merge((attrs || {}).reject {|k,v| v.nil? }).each do |k,v|
if self.class.fields[k.to_sym]
send("#{k}=", v)
end
end
end
@@ -123,10 +123,14 @@
define_method("#{name}=") do |v|
if options[:many]
set(name, Frenchy::Collection.new(Array(v).map {|vv| klass.new(vv)}))
else
- set(name, klass.new(v))
+ if v.is_a?(Hash)
+ set(name, klass.new(v))
+ else
+ set(name, v)
+ end
end
end
end
self.fields[name.to_sym] = options