lib/clean_model/attribute.rb in clean_model-0.0.2 vs lib/clean_model/attribute.rb in clean_model-0.0.3
- old
+ new
@@ -16,11 +16,11 @@
@options[:transformation].call(value)
elsif value.is_a?(Hash) && klass.new.respond_to?(:assign_attributes)
obj = klass.new
obj.assign_attributes value
obj
- elsif value.is_a?(Array) && collection_class.new.respond_to?(:assign_attributes)
+ elsif value.is_a?(Array) && collection_class.instance_methods.include?(:assign_attributes)
value.map do |v|
if v.is_a? collection_class
v
else
obj = collection_class.new
@@ -29,9 +29,13 @@
end
end
else
value
end
+ end
+
+ def assign_default(model)
+ model.send("#{@name}=", @options[:default]) if @options[:default] && model.respond_to?("#{@name}=")
end
private
def klass
\ No newline at end of file