Sha256: fce55cae8d8bd31592f1a51c112f48bfced56c1bbb4871b7f9a7f2f9bb5c675b

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 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, resource = nil)
          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

3 entries across 3 versions & 1 rubygems

Version Path
transcriber-0.0.25 lib/transcriber/resource/parser/property.rb
transcriber-0.0.24 lib/transcriber/resource/parser/property.rb
transcriber-0.0.23 lib/transcriber/resource/parser/property.rb