Class: I18n::Tasks::Data::Tree::Nodes

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Traversal
Defined in:
lib/i18n/tasks/data/tree/nodes.rb

Overview

A list of nodes

Direct Known Subclasses

Siblings

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Traversal

#breadth_first, #depth_first, #get_nodes_by_key_filter, #grep_keys, #intersect_keys, #key_names, #key_values, #keys, #leaves, #levels, #nodes, #root_key_value_data, #root_key_values, #select_keys, #select_keys!, #select_nodes, #select_nodes!, #set_each_value!

Constructor Details

#initialize(opts = {}) ⇒ Nodes

Returns a new instance of Nodes.



12
13
14
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 12

def initialize(opts = {})
  @list = opts[:nodes] ? opts[:nodes].to_a.clone : []
end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



10
11
12
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 10

def list
  @list
end

Instance Method Details

#append(other) ⇒ Object Also known as: <<



66
67
68
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 66

def append(other)
  derive.append!(other)
end

#append!(other) ⇒ Object



60
61
62
63
64
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 60

def append!(other)
  @list += other.to_a
  dirty!
  self
end

#attributesObject



22
23
24
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 22

def attributes
  { nodes: @list }
end

#children(&block) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 79

def children(&block)
  return to_enum(:children) { map { |c| c.children ? c.children.size : 0 }.reduce(:+) } unless block

  each do |node|
    node.children.each(&block) if node.children?
  end
end

#derive(new_attr = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 26

def derive(new_attr = {})
  attr = attributes.except(:nodes, :parent).merge(new_attr)
  node_attr = new_attr.slice(:parent)
  attr[:nodes] ||= @list.map { |node| node.derive(node_attr) }
  self.class.new(attr)
end

#inspectObject



44
45
46
47
48
49
50
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 44

def inspect
  if present?
    map(&:inspect) * "\n"
  else
    Rainbow('{∅}').faint
  end
end

#merge!(nodes) ⇒ Object Also known as: +



72
73
74
75
76
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 72

def merge!(nodes)
  @list += nodes.to_a
  dirty!
  self
end

#remove!(node) ⇒ Object

methods below change state



54
55
56
57
58
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 54

def remove!(node)
  @list.delete(node) || fail("#{node.full_key} not found in #{inspect}")
  dirty!
  self
end

#to_hash(sort = false) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 33

def to_hash(sort = false)
  (@hash ||= {})[sort] ||= if sort
                             sort_by(&:key)
                           else
                             self
                           end.map { |node| node.to_hash(sort) }.reduce({}, :deep_merge!)
end

#to_nodesObject



18
19
20
# File 'lib/i18n/tasks/data/tree/nodes.rb', line 18

def to_nodes
  self
end