Class: Dozer::Rule
- Inherits:
-
Object
- Object
- Dozer::Rule
- Defined in:
- lib/dozer/rule.rb
Instance Attribute Summary collapse
-
#from ⇒ Object
Returns the value of attribute from.
-
#func ⇒ Object
Returns the value of attribute func.
-
#to ⇒ Object
Returns the value of attribute to.
Instance Method Summary collapse
- #apply!(instance) ⇒ Object
-
#initialize(options) ⇒ Rule
constructor
A new instance of Rule.
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() = .with_indifferent_access validate!() @from = [:from].to_sym @to = [:to].to_sym @func = [:func] end |
Instance Attribute Details
#from ⇒ Object
Returns the value of attribute from.
3 4 5 |
# File 'lib/dozer/rule.rb', line 3 def from @from end |
#func ⇒ Object
Returns the value of attribute func.
3 4 5 |
# File 'lib/dozer/rule.rb', line 3 def func @func end |
#to ⇒ Object
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 |