class I18n::Tasks::Data::Tree::Node
Attributes
Public Class Methods
Source
# 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
value can be a nested hash
Source
# 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
rubocop:disable Metrics/ParameterLists
Public Instance Methods
Source
# File lib/i18n/tasks/data/tree/node.rb, line 105 def append(nodes) derive.append!(nodes) end
Source
# 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
append and reparent nodes
Source
# 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
rubocop:enable Metrics/ParameterLists
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
Source
# File lib/i18n/tasks/data/tree/node.rb, line 73 def children? children && !children.empty? end
Source
# File lib/i18n/tasks/data/tree/node.rb, line 29 def derive(new_attr = {}) self.class.new(**attributes.merge(new_attr)) end
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
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
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
Source
# File lib/i18n/tasks/data/tree/node.rb, line 89 def get(key) children.get(key) end
Also aliased as: []
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
Source
# File lib/i18n/tasks/data/tree/node.rb, line 85 def reference? value.is_a?(Symbol) end
Source
# File lib/i18n/tasks/data/tree/node.rb, line 121 def root p = nil walk_to_root { |node| p = node } p end
Source
# File lib/i18n/tasks/data/tree/node.rb, line 65 def root? !parent? end
a node with key nil is considered Empty. this is to allow for using these nodes instead of nils
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: []=
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
Source
# File lib/i18n/tasks/data/tree/node.rb, line 143 def to_nodes Nodes.new([self]) end
Source
# File lib/i18n/tasks/data/tree/node.rb, line 147 def to_siblings parent&.children || Siblings.new(nodes: [self]) end
Source
# File lib/i18n/tasks/data/tree/node.rb, line 56 def value_or_children_hash leaf? ? value : children.try(:to_hash) end
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
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
Source
# File lib/i18n/tasks/data/tree/node.rb, line 188 def dirty! @hash = nil @full_key = nil end