Module: Forester::Mutators

Included in:
TreeNode
Defined in:
lib/forester/tree_node_ext/mutators.rb

Instance Method Summary collapse

Instance Method Details

#add_child_content(content) ⇒ Object



14
15
16
17
# File 'lib/forester/tree_node_ext/mutators.rb', line 14

def add_child_content(content)
  new_node = Forester.tree_factory.node_from_content(content)
  add(new_node)
end

#add_field_in_node(name, definition) ⇒ Object



19
20
21
22
23
# File 'lib/forester/tree_node_ext/mutators.rb', line 19

def add_field_in_node(name, definition)
  value = definition.respond_to?(:call) ? definition.call(self) : definition

  content[name] = value
end

#add_field_in_subtree(name, definition) ⇒ Object



31
32
33
# File 'lib/forester/tree_node_ext/mutators.rb', line 31

def add_field_in_subtree(name, definition)
  add_fields_in_subtree([name: name, definition: definition])
end

#add_fields_in_node(fields) ⇒ Object



25
26
27
28
29
# File 'lib/forester/tree_node_ext/mutators.rb', line 25

def add_fields_in_node(fields)
  fields.each do |field|
    add_field_in_node(field[:name], field[:definition])
  end
end

#add_fields_in_subtree(fields) ⇒ Object



35
36
37
# File 'lib/forester/tree_node_ext/mutators.rb', line 35

def add_fields_in_subtree(fields)
  each_node { |node| node.add_fields_in_node(fields) }
end

#change_parent_to(new_parent_node, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/forester/tree_node_ext/mutators.rb', line 3

def change_parent_to(new_parent_node, options = {})
  default_options = {
    subtree: true
  }
  options = default_options.merge(options)

  children.each { |child| parent.add(child) } unless options[:subtree]

  new_parent_node.add(self) # always as its last child
end

#delete_values_in_node(field, values, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/forester/tree_node_ext/mutators.rb', line 39

def delete_values_in_node(field, values, options = {})
  default_options = {
    percolate: false
  }
  options = default_options.merge(options)

  return unless has_field?(field)
  current_value = get(field)

  operation = options[:percolate] ? :& : :-
  return unless current_value.respond_to?(operation)

  new_value = current_value.public_send(operation, as_array(values))

  content[field] = new_value
end

#delete_values_in_subtree(field, values, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/forester/tree_node_ext/mutators.rb', line 56

def delete_values_in_subtree(field, values, options = {})
  default_options = {
    percolate: false
  }
  options = default_options.merge(options)

  each_node { |node| node.delete_values_in_node(field, values, options) }
end

#remove_levels_past(last_level_to_keep) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/forester/tree_node_ext/mutators.rb', line 65

def remove_levels_past(last_level_to_keep)
  unless last_level_to_keep >= 1
    raise ArgumentError, "expected a positive integer, got #{last_level_to_keep}"
  end

  nodes_of_level(last_level_to_keep).map(&:remove_all!)
  self
end