class I18n::Tasks::Data::Tree::Nodes
A list of nodes
Attributes
list[R]
Public Class Methods
new(opts = {})
click to toggle source
# File lib/i18n/tasks/data/tree/nodes.rb, line 12 def initialize(opts = {}) @list = opts[:nodes] ? opts[:nodes].to_a.clone : [] end
Public Instance Methods
append(other)
click to toggle source
# File lib/i18n/tasks/data/tree/nodes.rb, line 66 def append(other) derive.append!(other) end
Also aliased as: <<
append!(other)
click to toggle source
# File lib/i18n/tasks/data/tree/nodes.rb, line 60 def append!(other) @list += other.to_a dirty! self end
attributes()
click to toggle source
# File lib/i18n/tasks/data/tree/nodes.rb, line 22 def attributes { nodes: @list } end
children(&block)
click to toggle source
# File lib/i18n/tasks/data/tree/nodes.rb, line 79 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/tasks/data/tree/nodes.rb, line 26 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/tasks/data/tree/nodes.rb, line 44 def inspect if present? map(&:inspect) * "\n" else Rainbow('{∅}').faint end end
merge!(nodes)
click to toggle source
# File lib/i18n/tasks/data/tree/nodes.rb, line 72 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/tasks/data/tree/nodes.rb, line 54 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/tasks/data/tree/nodes.rb, line 33 def to_hash(sort = false) (@hash ||= {})[sort] ||= if sort sort_by(&:key) else self end.map { |node| node.to_hash(sort) }.reduce({}, :deep_merge!) end
to_nodes()
click to toggle source
# File lib/i18n/tasks/data/tree/nodes.rb, line 18 def to_nodes self end
Protected Instance Methods
dirty!()
click to toggle source
# File lib/i18n/tasks/data/tree/nodes.rb, line 91 def dirty! @hash = nil end