Class: Dozer::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/dozer/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Rule

Returns a new instance of Rule.



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

def initialize(options)
  options = options.with_indifferent_access
  validate!(options)

  @from = options[:from].to_sym
  @to   = options[:to].to_sym
  @func = options[:func]
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



3
4
5
# File 'lib/dozer/rule.rb', line 3

def from
  @from
end

#funcObject

Returns the value of attribute func.



3
4
5
# File 'lib/dozer/rule.rb', line 3

def func
  @func
end

#toObject

Returns the value of attribute to.



3
4
5
# File 'lib/dozer/rule.rb', line 3

def to
  @to
end

Instance Method Details

#apply!(instance) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dozer/rule.rb', line 14

def apply!(instance)
  return if !applicable?(instance.input)

  value = case 
  when func.nil?
    instance.input[from]
  when func.is_a?(Proc)
    func.call(instance.input[from])
  when func.is_a?(Symbol) && instance.respond_to?(func)
    instance.send(func)
  end

  instance.output[to] = value
end