Class: Transmogrifier::Rules::Copy
- Inherits:
-
Object
- Object
- Transmogrifier::Rules::Copy
- Defined in:
- lib/transmogrifier/rules/copy.rb
Instance Method Summary collapse
- #apply!(input_hash) ⇒ Object
-
#initialize(parent_selector, from, to) ⇒ Copy
constructor
A new instance of Copy.
Constructor Details
#initialize(parent_selector, from, to) ⇒ Copy
Returns a new instance of Copy.
4 5 6 |
# File 'lib/transmogrifier/rules/copy.rb', line 4 def initialize(parent_selector, from, to) @parent_selector, @from, @to = parent_selector, from, to end |
Instance Method Details
#apply!(input_hash) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/transmogrifier/rules/copy.rb', line 8 def apply!(input_hash) top = Node.for(input_hash) *from_keys, from_key = Selector.from_string(@from).keys *to_keys, to_key = Selector.from_string(@to).keys parents = top.find_all(Selector.from_string(@parent_selector).keys) parents.each do |parent| to_parent = parent.find_all(to_keys).first object = parent.find_all(from_keys).first.clone(from_key) next if object.nil? if to_child = to_parent.find_all([to_key]).first to_child.append(object) else to_parent.append({to_key => object}) end end top.raw end |