Class: I18n::Processes::Data::Tree::Nodes
- Inherits:
-
Object
- Object
- I18n::Processes::Data::Tree::Nodes
show all
- Includes:
- Enumerable, Traversal
- Defined in:
- lib/i18n/processes/data/tree/nodes.rb
Overview
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.
13
14
15
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 13
def initialize(opts = {})
@list = opts[:nodes] ? opts[:nodes].to_a.clone : []
end
|
Instance Attribute Details
#list ⇒ Object
Returns the value of attribute list.
11
12
13
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 11
def list
@list
end
|
Instance Method Details
#append(other) ⇒ Object
Also known as:
<<
69
70
71
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 69
def append(other)
derive.append!(other)
end
|
#append!(other) ⇒ Object
63
64
65
66
67
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 63
def append!(other)
@list += other.to_a
dirty!
self
end
|
#attributes ⇒ Object
23
24
25
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 23
def attributes
{ nodes: @list }
end
|
#children(&block) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 82
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
27
28
29
30
31
32
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 27
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
|
#inspect ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 47
def inspect
if present?
map(&:inspect) * "\n"
else
I18n::Processes::RainbowUtils.faint_color('{∅}')
end
end
|
#merge!(nodes) ⇒ Object
Also known as:
+
75
76
77
78
79
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 75
def merge!(nodes)
@list += nodes.to_a
dirty!
self
end
|
#remove!(node) ⇒ Object
methods below change state
57
58
59
60
61
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 57
def remove!(node)
@list.delete(node) || fail("#{node.full_key} not found in #{inspect}")
dirty!
self
end
|
#to_hash(sort = false) ⇒ Object
34
35
36
37
38
39
40
41
42
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 34
def to_hash(sort = false)
(@hash ||= {})[sort] ||= begin
if sort
sort_by(&:key)
else
self
end.map { |node| node.to_hash(sort) }.reduce({}, :deep_merge!)
end
end
|
#to_nodes ⇒ Object
19
20
21
|
# File 'lib/i18n/processes/data/tree/nodes.rb', line 19
def to_nodes
self
end
|