Sha256: 6fc62c2b5b50c4181535c5ace48ae7849c90d888b1b423141b06b903aae03180

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Transcriber
  class Resource
    module Parser
      def parse!(input, options = {})
        parse(input.with_different_access, options.merge(fail: true))
      end

      def parse(input, options = {})
        prepare_entries(input, options).collect do |item|
          parse_item(item, options)
        end
      end

      private

      def prepare_entries(input, options)
        path = InputPath.resolve(options, Transcriber.configuration.convert_input_keys)
        Array.wrap digg(input, path)
      end

      def parse_item(item, options)
        self.new.tap do |resource|
          keys.each do |key|
            value = digg(item, key.input_path)
            begin
              resource.__send__("#{key.name}=", key.parse(value, 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

1 entries across 1 versions & 1 rubygems

Version Path
transcriber-0.0.11 lib/transcriber/resource/parser.rb