class I18n::Tasks::Data::Tree::Node
Attributes
children[R]
key[R]
parent[R]
value[RW]
Public Class Methods
new(key:, value: nil, data: nil, parent: nil, children: nil, warn_about_add_children_to_leaf: true)
click to toggle source
rubocop:disable Metrics/ParameterLists
# 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
Protected Class Methods
from_key_value(key, value)
click to toggle source
value can be a nested hash
# 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
Public Instance Methods
append(nodes)
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 105 def append(nodes) derive.append!(nodes) end
append!(nodes)
click to toggle source
append and reparent nodes
# 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()
click to toggle source
rubocop:enable Metrics/ParameterLists
# 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=(children)
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 33 def children=(children) @children = case children when Siblings children.parent == self ? children : children.derive(parent: self) when NilClass nil else Siblings.new( nodes: children, parent: self, warn_about_add_children_to_leaf: @warn_about_add_children_to_leaf ) end dirty! end
children?()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 73 def children? children && !children.empty? end
data()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 77 def data @data ||= {} end
data?()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 81 def data? @data.present? end
derive(new_attr = {})
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 29 def derive(new_attr = {}) self.class.new(**attributes.merge(new_attr)) end
each(&block)
click to toggle source
# 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
format_value_for_inspect(value)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 89 def get(key) children.get(key) end
Also aliased as: []
inspect(level = 0)
click to toggle source
# 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?()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 60 def leaf? !children end
parent?()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 69 def parent? !parent.nil? end
reference?()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 85 def reference? value.is_a?(Symbol) end
root()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 121 def root p = nil walk_to_root { |node| p = node } p end
root?()
click to toggle source
a node with key nil is considered Empty. this is to allow for using these nodes instead of nils
# File lib/i18n/tasks/data/tree/node.rb, line 65 def root? !parent? end
set(full_key, node)
click to toggle source
# 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
Also aliased as: []=
to_hash(sort = false)
click to toggle source
# 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()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 143 def to_nodes Nodes.new([self]) end
to_siblings()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 147 def to_siblings parent&.children || Siblings.new(nodes: [self]) end
value_or_children_hash()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Protected Instance Methods
dirty!()
click to toggle source
# File lib/i18n/tasks/data/tree/node.rb, line 188 def dirty! @hash = nil @full_key = nil end