Report abuse


			
module ActiveRecord
  class Base


    # Begin custom code

    def self.from_hash( hash )
      h = hash.dup
      h.each do |key,value|
        case value.class.to_s
        when 'Array'
          h[key].map! { |e|
             Object.const_get(key.camelize.singularize).from_hash e }
        when 'Hash'
          h[key] = Object.const_get(key.camelize).from_hash value
        end
      end
      self.new h
    end

    def self.from_json( json )
      hash = ActiveSupport::JSON.decode json
      self.from_hash hash
    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
      self.from_hash hash[self.to_s.underscore]
    end

    # End custom code

  end
end