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

Defined in:
lib/transcriber/resource/key/property/parser.rb

Instance Method Summary collapse

Instance Method Details

#digg(input) ⇒ Object



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

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

#from_hash(value, resource = nil) ⇒ Object



33
34
35
# File 'lib/transcriber/resource/key/property/parser.rb', line 33

def from_hash(value, resource = nil)
  value
end

#parse(item, resource = nil) ⇒ Object



5
6
7
8
# File 'lib/transcriber/resource/key/property/parser.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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/transcriber/resource/key/property/parser.rb', line 37

def to_input!(value, resource, options = {})
  input_value = nil

  begin
    if translations
      input_value = translate_to_input(value, resource)
    else
      input_value = serializer.desserialize(value)
    end
  rescue => exception
    raise exception if options[:fail]
  end

  input = {}
  path = input_path

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

  input
end

#translate_from_input(value, resource) ⇒ Object



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

def translate_from_input(value, resource)
  case translations
  when nil  then value
  when Proc then resource.instance_eval(&translations)
  when Hash
    fail "from input error" unless translations.has_value?(value)
    translations.key(value)
  end
end

#translate_to_input(value, resource) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/transcriber/resource/key/property/parser.rb', line 24

def translate_to_input(value, resource)
  case translations
  when Proc then resource.instance_eval(&translations)
  when Hash
    fail "to input error" unless translations.has_key?(value)
    translations[value]
  end
end