Class: I18n::Tasks::Data::Tree::Node
- Inherits:
-
Object
- Object
- I18n::Tasks::Data::Tree::Node
show all
- Includes:
- Enumerable, Traversal
- Defined in:
- lib/i18n/tasks/data/tree/node.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
Class Method 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(key:, value: nil, data: nil, parent: nil, children: nil, warn_about_add_children_to_leaf: true) ⇒ Node
rubocop:disable Metrics/ParameterLists
14
15
16
17
18
19
20
21
22
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 14
def initialize(key:, value: nil, data: nil, parent: nil, children: nil, warn_about_add_children_to_leaf: true)
@key = key
@key = @key.to_s.freeze if @key
@value = value
@data = data
@parent = parent
@warn_about_add_children_to_leaf = warn_about_add_children_to_leaf
self.children = children if children
end
|
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
11
12
13
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 11
def children
@children
end
|
#key ⇒ Object
Returns the value of attribute key.
11
12
13
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 11
def key
@key
end
|
#parent ⇒ Object
Returns the value of attribute parent.
11
12
13
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 11
def parent
@parent
end
|
#value ⇒ Object
Returns the value of attribute value.
10
11
12
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 10
def value
@value
end
|
Class Method Details
.from_key_value(key, value) ⇒ Object
value can be a nested hash
195
196
197
198
199
200
201
202
203
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 195
def from_key_value(key, value)
Node.new(key: key.try(:to_s)).tap do |node|
if value.is_a?(Hash)
node.children = Siblings.from_nested_hash(value)
else
node.value = value
end
end
end
|
Instance Method Details
#append(nodes) ⇒ Object
105
106
107
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 105
def append(nodes)
derive.append!(nodes)
end
|
#append!(nodes) ⇒ Object
append and reparent nodes
96
97
98
99
100
101
102
103
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 96
def append!(nodes)
if @children
@children.merge!(nodes)
else
@children = Siblings.new(nodes: nodes, parent: self)
end
self
end
|
#attributes ⇒ Object
rubocop:enable Metrics/ParameterLists
25
26
27
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 25
def attributes
{ key: @key, value: @value, data: @data.try(:clone), parent: @parent, children: @children }
end
|
#children? ⇒ Boolean
73
74
75
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 73
def children?
children && !children.empty?
end
|
#data ⇒ Object
77
78
79
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 77
def data
@data ||= {}
end
|
#data? ⇒ Boolean
81
82
83
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 81
def data?
@data.present?
end
|
#derive(new_attr = {}) ⇒ Object
29
30
31
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 29
def derive(new_attr = {})
self.class.new(**attributes.merge(new_attr))
end
|
#each(&block) ⇒ Object
49
50
51
52
53
54
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 49
def each(&block)
return to_enum(:each) { 1 } unless block
block.yield(self)
self
end
|
178
179
180
181
182
183
184
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 178
def format_value_for_inspect(value)
if value.is_a?(Symbol)
"#{Rainbow('⮕ ').bright.yellow}#{Rainbow(value).yellow}"
else
Rainbow(value).cyan
end
end
|
#full_key(root: true) ⇒ Object
109
110
111
112
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 109
def full_key(root: true)
@full_key ||= {}
@full_key[root] ||= "#{"#{parent.full_key(root: root)}." if parent? && (root || parent.parent?)}#{key}"
end
|
#get(key) ⇒ Object
Also known as:
[]
89
90
91
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 89
def get(key)
children.get(key)
end
|
#inspect(level = 0) ⇒ Object
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 167
def inspect(level = 0)
label = if key.nil?
Rainbow('∅').faint
else
[Rainbow(key).color(1 + (level % 15)),
(": #{format_value_for_inspect(value)}" if leaf?),
(" #{data}" if data?)].compact.join
end
[' ' * level, label, ("\n#{children.map { |c| c.inspect(level + 1) }.join("\n")}" if children?)].compact.join
end
|
#leaf? ⇒ Boolean
60
61
62
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 60
def leaf?
!children
end
|
#parent? ⇒ Boolean
69
70
71
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 69
def parent?
!parent.nil?
end
|
#reference? ⇒ Boolean
85
86
87
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 85
def reference?
value.is_a?(Symbol)
end
|
#root ⇒ Object
121
122
123
124
125
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 121
def root
p = nil
walk_to_root { |node| p = node }
p
end
|
#root? ⇒ Boolean
a node with key nil is considered Empty. this is to allow for using these nodes instead of nils
65
66
67
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 65
def root?
!parent?
end
|
#set(full_key, node) ⇒ Object
Also known as:
[]=
135
136
137
138
139
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 135
def set(full_key, node)
(@children ||= Siblings.new(parent: self)).set(full_key, node)
dirty!
node
end
|
#to_hash(sort = false) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 151
def to_hash(sort = false)
(@hash ||= {})[sort] ||= begin
children_hash = children ? children.to_hash(sort) : {}
if key.nil?
children_hash
elsif leaf?
{ key => value }
else
{ key => children_hash }
end
end
end
|
#to_nodes ⇒ Object
143
144
145
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 143
def to_nodes
Nodes.new([self])
end
|
#to_siblings ⇒ Object
147
148
149
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 147
def to_siblings
parent&.children || Siblings.new(nodes: [self])
end
|
#value_or_children_hash ⇒ Object
56
57
58
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 56
def value_or_children_hash
leaf? ? value : children.try(:to_hash)
end
|
#walk_from_root(&visitor) ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 127
def walk_from_root(&visitor)
return to_enum(:walk_from_root) unless visitor
walk_to_root.reverse_each do |node|
visitor.yield node
end
end
|
#walk_to_root(&visitor) ⇒ Object
114
115
116
117
118
119
|
# File 'lib/i18n/tasks/data/tree/node.rb', line 114
def walk_to_root(&visitor)
return to_enum(:walk_to_root) unless visitor
visitor.yield self
parent.walk_to_root(&visitor) if parent?
end
|