Sha256: 5627c45917ce27bf183197d8eedff8893b159067fc43e0f87f13aa306ee15bb0

Contents?: true

Size: 1018 Bytes

Versions: 7

Compression:

Stored size: 1018 Bytes

Contents

module Transcriber
  class Resource
    module Parser
      def parse!(input, options = {})
        parse(input, 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

7 entries across 7 versions & 1 rubygems

Version Path
transcriber-0.0.10 lib/transcriber/resource/parser.rb
transcriber-0.0.9 lib/transcriber/resource/parser.rb
transcriber-0.0.8 lib/transcriber/resource/parser.rb
transcriber-0.0.7 lib/transcriber/resource/parser.rb
transcriber-0.0.6 lib/transcriber/resource/parser.rb
transcriber-0.0.5 lib/transcriber/resource/parser.rb
transcriber-0.0.4 lib/transcriber/resource/parser.rb