Module: Dozer::Mapperable::ClassMethods

Defined in:
lib/dozer/mapperable.rb

Instance Method Summary collapse

Instance Method Details

#mapping(options) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
# File 'lib/dozer/mapperable.rb', line 6

def mapping(options)
  options = options.with_indifferent_access
  raise ArgumentError, 'from is missing' if options[:from].nil?
  raise ArgumentError, 'to is missing' if options[:to].nil?

  dozer_forward_mapper[options[:from]] = options[:to].to_sym
  dozer_backward_mapper[options[:to]] = options[:from].to_sym
end

#transform(hash, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dozer/mapperable.rb', line 23

def transform(hash, options={})
  result = {}.with_indifferent_access

  options = options.with_indifferent_access
  mapper = if options[:reverse] == true
             dozer_backward_mapper
           else
             dozer_forward_mapper
           end

  hash.each do |k, v|
    new_key = mapper[k]
    result[new_key] = v if new_key
  end

  result
end