Class: LMDocstache::ConditionalBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/lm_docstache/conditional_block.rb

Constant Summary collapse

BLOCK_MATCHER =
LMDocstache::Parser::BLOCK_MATCHER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements:, condition:, content: nil) ⇒ ConditionalBlock

Returns a new instance of ConditionalBlock.



9
10
11
12
13
14
# File 'lib/lm_docstache/conditional_block.rb', line 9

def initialize(elements:, condition:, content: nil)
  @elements = elements
  @condition = condition
  @content = content
  @evaluated = false
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



7
8
9
# File 'lib/lm_docstache/conditional_block.rb', line 7

def condition
  @condition
end

#elementsObject (readonly)

Returns the value of attribute elements.



7
8
9
# File 'lib/lm_docstache/conditional_block.rb', line 7

def elements
  @elements
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/lm_docstache/conditional_block.rb', line 7

def value
  @value
end

Class Method Details

.inline_blocks_from_paragraph(paragraph) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lm_docstache/conditional_block.rb', line 36

def self.inline_blocks_from_paragraph(paragraph)
  node_set = Nokogiri::XML::NodeSet.new(paragraph.document, [paragraph])
  conditional_blocks = []
  scanner = StringScanner.new(paragraph.text)
  matches = []

  # This loop will iterate through all existing inline conditional blocks
  # inside a given paragraph node.
  while scanner.scan_until(BLOCK_MATCHER)
    next if matches.include?(scanner.matched)

    # +scanner.matched+ holds the whole regex-matched string, which could be
    # represented by the following string:
    #
    #    {{#variable == value}}content{{/variable}}
    #
    # While +scanner.captures+ holds the group matches referenced in the
    # +BLOCK_MATCHER+ regex, and it's basically comprised as the following:
    #
    #   [
    #     '#',
    #     'variable',
    #     '==',
    #     'value'
    #   ]
    #
    content = scanner.captures[4]
    condition = Condition.new(
      left_term: scanner.captures[1],
      right_term: scanner.captures[3],
      operator: scanner.captures[2],
      negation: scanner.captures[0] == '^',
      original_match: scanner.matched
    )

    matches << scanner.matched
    conditional_blocks << new(
      elements: node_set,
      condition: condition,
      content: content
    )
  end

  conditional_blocks
end

Instance Method Details

#contentObject



16
17
18
# File 'lib/lm_docstache/conditional_block.rb', line 16

def content
  return @content if inline?
end

#evaluate_with_value!(value) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/lm_docstache/conditional_block.rb', line 20

def evaluate_with_value!(value)
  return false if evaluated?

  inline? ? evaluate_inline_block!(value) : evaluate_multiple_nodes_block!(value)

  @evaluated = true
end

#evaluated?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/lm_docstache/conditional_block.rb', line 28

def evaluated?
  !!@evaluated
end

#inline?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/lm_docstache/conditional_block.rb', line 32

def inline?
  @elements.size == 1
end