Skip to content

Instantly share code, notes, and snippets.

@bbwharris
Created August 13, 2011 14:03
Show Gist options
  • Save bbwharris/1143879 to your computer and use it in GitHub Desktop.
Save bbwharris/1143879 to your computer and use it in GitHub Desktop.
Irrelevant?
module ActiveRecord
class Base
# FROM http://www.xcombinator.com/2008/08/11/activerecord-from_xml-and-from_json-part-2/
def self.from_hash( hash )
h = hash.dup
h.each do |key,value|
h[key].map! { |e| reflect_on_association(key.to_sym ).klass.from_hash e } if value.is_a?(Array)
h[key] = reflect_on_association(key.to_sym).klass.from_hash(value) if value.is_a?(Hash)
end
new h
end
def self.from_json( json )
from_hash ActiveSupport::JSON.safe_json_decode(json)
end
# The xml has a surrounding class tag (e.g. ship-to),
# but the hash has no counterpart (e.g. 'ship_to' => {} )
def self.from_xml( xml )
from_hash begin
Hash.from_xml(xml)[to_s.demodulize.underscore]
rescue ; {} end
end
end # class Base
end # module ActiveRecord
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment