Sha256: a0d19a054c74d3fd31e27c396f0a75407ac9d2c3636766068e09115e5b33ed05

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module Transcriber
  class Resource
    module Parser
      module Property
        def parse(item, resource = nil)
          value = digg(item)
          translate_from_input(serializer.serialize(value), resource)
        end

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

        def translate_from_input(value, resource)
          case translations
          when nil  then value
          when Hash then translations.key(value)
          when Proc then resource.instance_eval(&translations)
          end
        end

        def translate_to_input(value, resource)
          case translations
          when nil  then value
          when Hash then translations[value]
          when Proc then resource.instance_eval(&translations)
          end
        end

        def from_hash(value)
          value
        end

        def to_input(value, resource, options = {});
          value = translate_to_input(value, resource)
          input = {}
          path = input_path

          if path.any?
            last = path.pop
            key_input = path.inject(input) {|buffer, key| buffer[key] = {}; buffer[key]}
            key_input[last] = value
          else
            input.merge!(value)
          end

          input
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transcriber-0.0.22 lib/transcriber/resource/parser/property.rb
transcriber-0.0.20 lib/transcriber/resource/parser/property.rb