class I18n::Processes::Data::Tree::Nodes
A list of nodes
Attributes
list[R]
Public Class Methods
new(opts = {})
click to toggle source
# File lib/i18n/processes/data/tree/nodes.rb, line 13 def initialize(opts = {}) @list = opts[:nodes] ? opts[:nodes].to_a.clone : [] end
Public Instance Methods
append(other)
click to toggle source
# File lib/i18n/processes/data/tree/nodes.rb, line 69 def append(other) derive.append!(other) end
Also aliased as: <<
append!(other)
click to toggle source
# File lib/i18n/processes/data/tree/nodes.rb, line 63 def append!(other) @list += other.to_a dirty! self end
attributes()
click to toggle source
# File lib/i18n/processes/data/tree/nodes.rb, line 23 def attributes { nodes: @list } end
children(&block)
click to toggle source
# 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 = {})
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# File lib/i18n/processes/data/tree/nodes.rb, line 75 def merge!(nodes) @list += nodes.to_a dirty! self end
Also aliased as: +
remove!(node)
click to toggle source
methods below change state
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/i18n/processes/data/tree/nodes.rb, line 19 def to_nodes self end
Protected Instance Methods
dirty!()
click to toggle source
# File lib/i18n/processes/data/tree/nodes.rb, line 93 def dirty! @hash = nil end