Sha256: 6c74cff8a72faa2c37a5ca8ee291514c30d483e0769f065ad8bf6fec029e23b3

Contents?: true

Size: 1.22 KB

Versions: 30

Compression:

Stored size: 1.22 KB

Contents

class RestModel
  module Source
    module Retriever
      def from_source!(input, options = {})
        from_source(input, options.merge(fail: true))
      end

      def from_source(input, options = {})
        entries = prepare_entries(input, options)

        if entries.any? {|e| !e.kind_of?(Hash)}
          fail "invalid input" if options[:fail]
          return []
        end

        entries.collect do |item|
          handle_item(item.with_indifferent_access, options)
        end
      end

      alias :parse! :from_source!
      alias :parse  :from_source

      private

      def prepare_entries(input, options)
        path = Source::Path.resolve(options, RestModel::Configuration.convert_input_keys)
        Array.wrap digg(input, path)
      end

      def handle_item(item, options)
        self.new.tap do |resource|
          keys.each do |key|
            begin
              resource.__send__("#{key.name}=", key.from_source(item, resource)) if key.present?(resource)
            rescue => exception
              raise exception if options[:fail]
            end
          end
        end
      end

      def digg(input, path)
        path.inject(input) {|buffer, key| buffer = buffer[key]}
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
rest_model-0.1.9 lib/rest_model/source/retriever.rb
rest_model-0.1.8 lib/rest_model/source/retriever.rb
rest_model-0.1.7 lib/rest_model/source/retriever.rb
rest_model-0.1.6 lib/rest_model/source/retriever.rb
rest_model-0.1.5 lib/rest_model/source/retriever.rb
rest_model-0.1.4 lib/rest_model/source/retriever.rb
rest_model-0.1.3 lib/rest_model/source/retriever.rb
rest_model-0.1.2 lib/rest_model/source/retriever.rb
rest_model-0.1.1 lib/rest_model/source/retriever.rb
rest_model-0.1.0 lib/rest_model/source/retriever.rb