Module: RestModel::Property::Sender

Included in:
RestModel::Property
Defined in:
lib/rest_model/key/property/sender.rb

Instance Method Summary collapse

Instance Method Details

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



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rest_model/key/property/sender.rb', line 4

def to_source!(value, resource, options = {})
  source_value = begin
    translation.translates_to_source? ? translation.translate_to_source(value, resource)
                                      : serializer.desserialize(value)

  rescue TranslationError, SerializationError => error
    raise error if options[:fail] and validates?(value)
  end

  source = {}
  path = source_path

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

  source
end