Class: Speculations::Parser::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/speculations/parser/context.rb

Defined Under Namespace

Classes: Example, Include

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/speculations/parser/context.rb', line 7

def filename
  @filename
end

#levelObject (readonly)

Returns the value of attribute level.



7
8
9
# File 'lib/speculations/parser/context.rb', line 7

def level
  @level
end

#lnbObject (readonly)

Returns the value of attribute lnb.



7
8
9
# File 'lib/speculations/parser/context.rb', line 7

def lnb
  @lnb
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/speculations/parser/context.rb', line 7

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/speculations/parser/context.rb', line 7

def root
  @root
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/speculations/parser/context.rb', line 7

def title
  @title
end

#tree_levelObject (readonly)

Returns the value of attribute tree_level.



7
8
9
# File 'lib/speculations/parser/context.rb', line 7

def tree_level
  @tree_level
end

Instance Method Details

#childrenObject



34
35
36
# File 'lib/speculations/parser/context.rb', line 34

def children
  @__children__ ||= []
end

#examplesObject



38
39
40
# File 'lib/speculations/parser/context.rb', line 38

def examples
  @__examples__ ||= []
end

#includesObject



42
43
44
# File 'lib/speculations/parser/context.rb', line 42

def includes
  @__includes__ ||= []
end

#map_lines(*lines, indent: 0) ⇒ Object



46
47
48
49
# 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}".rstrip }
end

#new_context(title:, lnb:, level:) ⇒ Object



9
10
11
12
# 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:) ⇒ Object



14
15
16
17
# 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:) ⇒ Object



19
20
21
22
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
# 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_codeObject



51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
# File 'lib/speculations/parser/context.rb', line 61

def with_new_parent new_parent
  @parent = new_parent
  @tree_level += 1
  self
end