Method: Resourceful::Serialize.normalize_attributes

Defined in:
lib/resourceful/serialize.rb

.normalize_attributes(attributes) ⇒ Object

Takes an attributes option in the form passed to Builder#publish and returns a hash (or nil, if attributes is nil) containing the same data, but in a more consistent format. All keys are converted to symbols, and all lists are converted to hashes. For example:

Resourceful::Serialize.normalize_attributes([:foo, :bar, {"baz" => ["boom"]}])
  #=> {"baz"=>["boom"], :foo=>nil, :bar=>nil}


89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/resourceful/serialize.rb', line 89

def self.normalize_attributes(attributes) # :nodoc:
  return nil if attributes.nil?
  return {attributes.to_sym => nil} if String === attributes
  return {attributes => nil} if !attributes.respond_to?(:inject)

  attributes.inject({}) do |hash, attr|
    if Array === attr
      hash[attr[0]] = attr[1]
      hash
    else
      hash.merge normalize_attributes(attr)
    end
  end
end