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

+(nodes)
Alias for: merge!
<<(other)
Alias for: append
append(other) click to toggle source
# File lib/i18n/tasks/data/tree/nodes.rb, line 68
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 62
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 81
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 46
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 74
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 56
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] ||= 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/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 93
def dirty!
  @hash = nil
end