class Speculations::Parser::Context

Attributes

filename[R]
level[R]
lnb[R]
parent[R]
root[R]
title[R]
tree_level[R]

Public Class Methods

new(lnb: 0, title: nil, filename: nil, parent: nil, level: 0) click to toggle source
# File lib/speculations/parser/context.rb, line 70
def initialize(lnb: 0, title: nil, filename: nil, parent: nil, level: 0)
  @filename = filename
  @level    = level
  @lnb      = lnb
  @title    = title
  @parent   = parent
  if parent
    _init_from_parent
  else
    @root = self
    @tree_level  = 0
  end
end

Public Instance Methods

children() click to toggle source
# File lib/speculations/parser/context.rb, line 34
def children
  @__children__ ||= []
end
examples() click to toggle source
# File lib/speculations/parser/context.rb, line 38
def examples
  @__examples__ ||= []
end
includes() click to toggle source
# File lib/speculations/parser/context.rb, line 42
def includes
  @__includes__ ||= []
end
map_lines(*lines, indent: 0) click to toggle source
# File lib/speculations/parser/context.rb, line 46
def map_lines(*lines, indent: 0)
  prefix = "  " * (tree_level + indent).succ
  lines.flatten.map{ |line| "#{prefix}#{line.strip}" }
end
new_context(title:, lnb:, level: ) click to toggle source
# File lib/speculations/parser/context.rb, line 9
def new_context(title:, lnb:, level: )
  new_child = self.class.new(title: title, lnb: lnb, parent: self, level: level)
  _realign_levels(new_child)
end
new_example(lnb:, title:) click to toggle source
# File lib/speculations/parser/context.rb, line 14
def new_example(lnb:, title:)
  examples << Example.new(lnb: lnb, parent: self, title: title)
  examples.last
end
new_include(lnb:) click to toggle source
# File lib/speculations/parser/context.rb, line 19
def new_include(lnb:)
  includes << Include.new(lnb: lnb, parent: self)
  includes.last
end
parent_of_level(needed_min_level) click to toggle source
# File lib/speculations/parser/context.rb, line 24
def parent_of_level needed_min_level
  # I would love to write
  # self.enum_by(:parent).find{ |ctxt| ctxt.level <= needed_min_level }
  current = self
  while current.level > needed_min_level
    current = current.parent
  end
  current
end
to_code() click to toggle source
# File lib/speculations/parser/context.rb, line 51
def to_code
  [
    _header,
    includes.map(&:to_code),
    examples.map(&:to_code),
    children.map(&:to_code),
    _footer
  ].flatten.compact
end
with_new_parent(new_parent) click to toggle source
# File lib/speculations/parser/context.rb, line 61
def with_new_parent new_parent
  @parent = new_parent
  @tree_level += 1
  self
end

Private Instance Methods

_header() click to toggle source
# File lib/speculations/parser/context.rb, line 84
def _header
  if parent
    map_lines(%{# #{filename}:#{lnb}}, %{context "#{title}" do}, indent: -1)
  else
    []
  end
end
_init_from_parent() click to toggle source
# File lib/speculations/parser/context.rb, line 92
def _init_from_parent
  @root = parent.root
  @filename = parent.filename
  @tree_level = parent.tree_level.succ
end
_realign_levels(new_parent) click to toggle source
# File lib/speculations/parser/context.rb, line 106
def _realign_levels new_parent
  if children.empty? || children.first.level == new_parent.level
    children << new_parent
    return new_parent
  end
  children.each do |child|
    new_parent.children << child.with_new_parent(new_parent)
  end
  @__children__ = [new_parent]
  new_parent
end