class Docstache::Block
Attributes
closing_element[R]
condition[R]
content_elements[R]
inverted[R]
name[R]
opening_element[R]
Public Class Methods
find_all(name:, data:, elements:, inverted:, condition: nil)
click to toggle source
# 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
new(name:, data:, opening_element:, content_elements:, closing_element:, inverted:, condition: nil)
click to toggle source
# 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
Public Instance Methods
conditional?()
click to toggle source
# File lib/docstache/block.rb, line 30 def conditional? type == :conditional end
loop?()
click to toggle source
# File lib/docstache/block.rb, line 26 def loop? type == :loop end
type()
click to toggle source
# 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