Sha256: b24bbb5b2c7ecca3bd89d7b5f5a83a9faca5eacdfcc283268073395d8df973ad

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Transcriber
  class Resource
    class Embeddable
      module Parser
        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, resource = nil)
          raw? ? attrs : super
        end

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

        private

        def to_single_input(value, options)
          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

1 entries across 1 versions & 1 rubygems

Version Path
transcriber-0.0.26 lib/transcriber/resource/key/embeddable/parser.rb