Skip to content

Commit

Permalink
Make use of a helper method to determine when to include a key in cle…
Browse files Browse the repository at this point in the history
…aned_data
  • Loading branch information
thomsbg authored and remi committed Aug 27, 2012
1 parent e8256b7 commit 2451d17
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/her/model/orm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 # }}}
Expand Down Expand Up @@ -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
#
Expand Down

0 comments on commit 2451d17

Please sign in to comment.