Module: Forester::Mutators
- Included in:
- TreeNode
- Defined in:
- lib/forester/tree_node_ext/mutators.rb
Instance Method Summary collapse
- #add_field!(name, definition, options = {}) ⇒ Object
- #add_fields!(fields, options = {}) ⇒ Object
- #add_fields_to_root!(fields) ⇒ Object
- #delete_values!(field, values, options = {}) ⇒ Object
- #delete_values_from_root!(field, values, percolate) ⇒ Object
- #percolate_values!(field, values, options = {}) ⇒ Object
- #remove_levels_past!(last_level_to_keep) ⇒ Object
Instance Method Details
#add_field!(name, definition, options = {}) ⇒ Object
4 5 6 |
# File 'lib/forester/tree_node_ext/mutators.rb', line 4 def add_field!(name, definition, = {}) add_fields!([{ name: name, definition: definition }], ) end |
#add_fields!(fields, options = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/forester/tree_node_ext/mutators.rb', line 8 def add_fields!(fields, = {}) = { subtree: true } = .merge() target_nodes = [:subtree] ? each_node : [self] target_nodes.each { |node| node.add_fields_to_root!(fields) } end |
#add_fields_to_root!(fields) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/forester/tree_node_ext/mutators.rb', line 19 def add_fields_to_root!(fields) fields.each do |field| value = field[:definition] value = value.call(self) if value.respond_to?(:call) put!(field[:name], value) end end |
#delete_values!(field, values, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/forester/tree_node_ext/mutators.rb', line 28 def delete_values!(field, values, = {}) = { percolate: false, subtree: true } = .merge() target_nodes = [:subtree] ? each_node : [self] target_nodes.each { |node| node.delete_values_from_root!(field, values, [:percolate]) } end |
#delete_values_from_root!(field, values, percolate) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/forester/tree_node_ext/mutators.rb', line 40 def delete_values_from_root!(field, values, percolate) return unless has?(field) current_value = get(field) return unless current_value.is_a?(Array) new_value = if percolate current_value & as_array(values) else current_value - as_array(values) end put!(field, new_value) end |
#percolate_values!(field, values, options = {}) ⇒ Object
55 56 57 |
# File 'lib/forester/tree_node_ext/mutators.rb', line 55 def percolate_values!(field, values, = {}) delete_values!(field, values, .merge(percolate: true)) end |
#remove_levels_past!(last_level_to_keep) ⇒ Object
59 60 61 62 |
# File 'lib/forester/tree_node_ext/mutators.rb', line 59 def remove_levels_past!(last_level_to_keep) nodes_of_level(last_level_to_keep).map(&:remove_all!) self end |