Sha256: 491ac19eb157f46c0c1b3ca6411270e80f9e52bb103eb240508c22bffba6f159

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

module Ixtlan
  module Babel
    class Deserializer

      def initialize(model_class)
        @model_class = model_class
      end

      private

      def self.config
        @config ||= FilterConfig.new
      end

      def filter
        @filter ||= HashFilter.new
      end

      protected

      def self.default_context_key(default)
        config.default_context_key(default)
      end

      def self.add_context(key, options = {})
        config[key] = options
      end

      public

      def use(context_or_options)
        @context_or_options = context_or_options
        self
      end
      
      def from_array_hash( data )
        if filter.options[:root]
          data.collect{ |d| @model_class.new( filter.filter( d[ filter.options[:root] ] ) ) }
        else
          data.collect{ |d| @model_class.new( filter.filter( d ) ) }
        end
      end
      private :from_array_hash

      def from_hash(data, options = nil)
        filter.options = options || {}
        filter.options[:root] ||= self.class.config.root
        if data.is_a? Array
          from_array_hash( data )
        else
          data = data[ filter.options[:root] ] if filter.options[:root]
          @model_class.new( filter.filter( data ) )
        end
      end

      def from_json(json, options = nil)
        data = JSON.parse(json)
        from_hash(data, options)
      end
      
      def from_yaml(yaml, options = nil)
        data = YAML.load_stream(StringIO.new(yaml)).documents
        from_hash(data, options)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ixtlan-babel-0.3.0 lib/ixtlan/babel/deserializer.rb
ixtlan-babel-0.2.1 lib/ixtlan/babel/deserializer.rb
ixtlan-babel-0.2.0 lib/ixtlan/babel/deserializer.rb