Class: Docstache::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/docstache/block.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, data:, opening_element:, content_elements:, closing_element:, inverted:, condition: nil) ⇒ Block



4
5
6
7
8
9
10
11
12
# File 'lib/docstache/block.rb', line 4

def initialize(name:, data:, opening_element:, content_elements:, closing_element:, inverted:, condition: nil)
  @name = name
  @data = data
  @opening_element = opening_element
  @content_elements = content_elements
  @closing_element = closing_element
  @inverted = inverted
  @condition = condition
end

Instance Attribute Details

#closing_elementObject (readonly)

Returns the value of attribute closing_element.



3
4
5
# File 'lib/docstache/block.rb', line 3

def closing_element
  @closing_element
end

#conditionObject (readonly)

Returns the value of attribute condition.



3
4
5
# File 'lib/docstache/block.rb', line 3

def condition
  @condition
end

#content_elementsObject (readonly)

Returns the value of attribute content_elements.



3
4
5
# File 'lib/docstache/block.rb', line 3

def content_elements
  @content_elements
end

#invertedObject (readonly)

Returns the value of attribute inverted.



3
4
5
# File 'lib/docstache/block.rb', line 3

def inverted
  @inverted
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/docstache/block.rb', line 3

def name
  @name
end

#opening_elementObject (readonly)

Returns the value of attribute opening_element.



3
4
5
# File 'lib/docstache/block.rb', line 3

def opening_element
  @opening_element
end

Class Method Details

.find_all(name:, data:, elements:, inverted:, condition: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/docstache/block.rb', line 34

def self.find_all(name:, data:, elements:, inverted:, condition: nil)
  if elements.text.match(/\{\{#{inverted ? '\^' : '\#'}#{name}#{condition ? " when #{condition}" : ''}\}\}.+?\{\{\/#{name}\}\}/m)
    if elements.any? { |e| e.text.match(/\{\{#{inverted ? '\^' : '\#'}#{name}#{condition ? " when #{condition}" : ''}\}\}.+?\{\{\/#{name}\}\}/m) }
      matches = elements.select { |e| e.text.match(/\{\{#{inverted ? '\^' : '\#'}#{name}#{condition ? " when #{condition}" : ''}\}\}.+?\{\{\/#{name}\}\}/m) }
      finds = matches.map { |match| find_all(name: name, data: data, elements: match.elements, inverted: inverted, condition: condition) }.flatten
      return finds
    else
      opening = elements.select { |e| e.text.match(/\{\{#{inverted ? '\^' : '\#'}#{name}#{condition ? " when #{condition}" : ''}\}\}/) }.first
      content = []
      next_sibling = opening.next
      while !next_sibling.text.match(/\{\{\/#{name}\}\}/)
        content << next_sibling
        next_sibling = next_sibling.next
      end
      closing = next_sibling
      return Block.new(name: name, data: data, opening_element: opening, content_elements: content, closing_element: closing, inverted: inverted, condition: condition)
    end
  else
    raise "Block not found in given elements"
  end
end

Instance Method Details

#conditional?Boolean



30
31
32
# File 'lib/docstache/block.rb', line 30

def conditional?
  type == :conditional
end

#loop?Boolean



26
27
28
# File 'lib/docstache/block.rb', line 26

def loop?
  type == :loop
end

#typeObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/docstache/block.rb', line 14

def type
  @type ||= if @inverted
    :conditional
  else
    if @data.get(@name).is_a? Array
      :loop
    else
      :conditional
    end
  end
end