Sha256: 5061d1fe8817eabc244eb2c1da1179c60d4e116fd39ff4b704f0cc9f080a89de
Contents?: true
Size: 1.62 KB
Versions: 1
Compression:
Stored size: 1.62 KB
Contents
module ActiveRecord class Base # basic code from Matt Pulver # found at http://www.xcombinator.com/2008/08/11/activerecord-from_xml-and-from_json-part-2/ # addapted to support links def self.from_hash( hash ) h = {} h = hash.dup if hash links = nil h.each do |key,value| case value.class.to_s when 'Array' if key=="link" links = h[key] h.delete("link") else h[key].map! { |e| reflect_on_association(key.to_sym ).klass.from_hash e } end when /\AHash(WithIndifferentAccess)?\Z/ if key=="link" links = [h[key]] h.delete("link") else h[key] = reflect_on_association(key.to_sym ).klass.from_hash value end end h.delete("xmlns") if key=="xmlns" end result = self.new h if !(links.nil?) && self.include?(Restfulie::Client::Instance) add_transitions(result, links) end result end def self.from_json( json ) from_hash 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 ) hash = Hash.from_xml xml head = hash[self.to_s.underscore] result = self.from_hash head return nil if result.nil? result._came_from = :xml if self.include?(Restfulie::Client::Instance) result end end end def safe_json_decode( json ) return {} if !json begin ActiveSupport::JSON.decode json rescue ; {} end end # end of code based on Matt Pulver's
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
restfulie-0.3 | lib/restfulie/unmarshalling.rb |