Module: Transcriber::Resource::Parser::Property

Included in:
Transcriber::Resource::Property
Defined in:
lib/transcriber/resource/parser/property.rb

Instance Method Summary collapse

Instance Method Details

#digg(input) ⇒ Object



10
11
12
# File 'lib/transcriber/resource/parser/property.rb', line 10

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

#from_hash(value, resource = nil) ⇒ Object



30
31
32
# File 'lib/transcriber/resource/parser/property.rb', line 30

def from_hash(value, resource = nil)
  value
end

#parse(item, resource = nil) ⇒ Object



5
6
7
8
# File 'lib/transcriber/resource/parser/property.rb', line 5

def parse(item, resource = nil)
  value = digg(item)
  translate_from_input(serializer.serialize(value), resource)
end

#to_input(value, resource, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/transcriber/resource/parser/property.rb', line 34

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

#translate_from_input(value, resource) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/transcriber/resource/parser/property.rb', line 14

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

#translate_to_input(value, resource) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/transcriber/resource/parser/property.rb', line 22

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