Sha256: 60587a1a999f86fce75bdb66d1b8778b66ec291497a5fae30f621c72d3d7c1ed

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

module Transcriber
  class Resource
    module Parser
      module Embeddable
        def parse(item, resource = nil)
          return parse_fields(item) if fields
          value = digg(item)
          parsed_value = raw? ? value : resource_class.parse(value)
          one? ? parsed_value.first : parsed_value
        end

        def parse_fields(item)
          convert_input_keys.call(fields).map {|field| item[field]}
        end

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

        def from_hash(attrs)
          raw? ? attrs : super
        end

        def to_input(value, options = {})
          return {} if value.nil?
          fields ? to_multiple_input(value, options)
                 : to_single_input(value)
        end

        private

        def to_single_input(value)
          input = {}
          path = input_path

          if path.any?
            last = path.pop
            key_input = path.inject(input) {|buffer, key| buffer[key] = {}; buffer[key]}
            key_input[last] = raw? ? value
                                   : one? ? value.to_input(options)
                                          : value.map {|item| item.to_input(options)}
          else
            input.merge!(value.to_input(options))
          end

          input
        end

        def to_multiple_input(value, options)
          input = {}
          mapped_fields = convert_input_keys.call(fields)

          value.each_with_index do |item, index|
            input[mapped_fields[index]] = item
          end

          input
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transcriber-0.0.19 lib/transcriber/resource/parser/embeddable.rb
transcriber-0.0.18 lib/transcriber/resource/parser/embeddable.rb