Permalink
Browse files

Make use of a helper method to determine when to include a key in cle…

…aned_data
  • Loading branch information...
1 parent e8256b7 commit 2451d17e6ebacc13648db324c13c3e9ac5d07b95 @thomsbg thomsbg committed with Aug 9, 2012
Showing with 9 additions and 1 deletion.
  1. +9 −1 lib/her/model/orm.rb
View
@@ -10,10 +10,12 @@ def initialize(data={}) # {{{
@data = {}
@metadata = data.delete(:_metadata) || {}
@errors = data.delete(:_errors) || {}
+
+ # Only keep the keys that don't have corresponding writer methods
cleaned_data = data.inject({}) do |memo, item|
key, value = item
send "#{key}=".to_sym, value unless value.nil?
- respond_to?("#{key}=") ? memo : memo.merge({ key => value })
+ writer_method_defined?(key) ? memo : memo.merge({ key => value })
end
@data.merge! self.class.parse_relationships(cleaned_data)
end # }}}
@@ -125,6 +127,12 @@ def to_params # {{{
@data.dup
end # }}}
+ private
+
+ def writer_method_defined?(key)
+ self.class.instance_methods.include? "#{key}=".to_sym
+ end
+
module ClassMethods
# Initialize a collection of resources with raw data from an HTTP request
#

0 comments on commit 2451d17

Please sign in to comment.