Sha256: a5b1631180de283264d155326bf4a1f58a9c1f7b81fb0863b8dfafa87c55c5d5

Contents?: true

Size: 1.12 KB

Versions: 8

Compression:

Stored size: 1.12 KB

Contents

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

      def parse(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|
          parse_item(item.with_indifferent_access, 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|
            begin
              resource.__send__("#{key.name}=", key.parse(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

8 entries across 8 versions & 1 rubygems

Version Path
transcriber-0.0.26 lib/transcriber/resource/parser.rb
transcriber-0.0.25 lib/transcriber/resource/parser.rb
transcriber-0.0.24 lib/transcriber/resource/parser.rb
transcriber-0.0.23 lib/transcriber/resource/parser.rb
transcriber-0.0.22 lib/transcriber/resource/parser.rb
transcriber-0.0.21 lib/transcriber/resource/parser.rb
transcriber-0.0.20 lib/transcriber/resource/parser.rb
transcriber-0.0.19 lib/transcriber/resource/parser.rb