Class: LMDocstache::Block
- Inherits:
-
Object
- Object
- LMDocstache::Block
- Defined in:
- lib/lm_docstache/block.rb
Instance Attribute Summary collapse
-
#closing_element ⇒ Object
readonly
Returns the value of attribute closing_element.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#content_elements ⇒ Object
readonly
Returns the value of attribute content_elements.
-
#inline ⇒ Object
readonly
Returns the value of attribute inline.
-
#inverted ⇒ Object
readonly
Returns the value of attribute inverted.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#opening_element ⇒ Object
readonly
Returns the value of attribute opening_element.
Class Method Summary collapse
- .extract_block_from_element(name, data, element, inverted, condition) ⇒ Object
- .find_all(name:, data:, elements:, inverted:, condition: nil, ignore_missing: true, child: false) ⇒ Object
Instance Method Summary collapse
- #conditional? ⇒ Boolean
-
#initialize(name:, data:, opening_element:, content_elements:, closing_element:, inverted:, condition: nil, inline: false) ⇒ Block
constructor
A new instance of Block.
- #loop? ⇒ Boolean
- #type ⇒ Object
Constructor Details
#initialize(name:, data:, opening_element:, content_elements:, closing_element:, inverted:, condition: nil, inline: false) ⇒ Block
Returns a new instance of Block.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/lm_docstache/block.rb', line 4 def initialize(name:, data:, opening_element:, content_elements:, closing_element:, inverted:, condition: nil, inline: false) @name = name @data = data @opening_element = opening_element @content_elements = content_elements @closing_element = closing_element @inverted = inverted @condition = condition @inline = inline end |
Instance Attribute Details
#closing_element ⇒ Object (readonly)
Returns the value of attribute closing_element.
3 4 5 |
# File 'lib/lm_docstache/block.rb', line 3 def closing_element @closing_element end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
3 4 5 |
# File 'lib/lm_docstache/block.rb', line 3 def condition @condition end |
#content_elements ⇒ Object (readonly)
Returns the value of attribute content_elements.
3 4 5 |
# File 'lib/lm_docstache/block.rb', line 3 def content_elements @content_elements end |
#inline ⇒ Object (readonly)
Returns the value of attribute inline.
3 4 5 |
# File 'lib/lm_docstache/block.rb', line 3 def inline @inline end |
#inverted ⇒ Object (readonly)
Returns the value of attribute inverted.
3 4 5 |
# File 'lib/lm_docstache/block.rb', line 3 def inverted @inverted end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/lm_docstache/block.rb', line 3 def name @name end |
#opening_element ⇒ Object (readonly)
Returns the value of attribute opening_element.
3 4 5 |
# File 'lib/lm_docstache/block.rb', line 3 def opening_element @opening_element end |
Class Method Details
.extract_block_from_element(name, data, element, inverted, condition) ⇒ Object
67 68 69 |
# File 'lib/lm_docstache/block.rb', line 67 def self.extract_block_from_element(name, data, element, inverted, condition) return Block.new(name: name, data: data, opening_element: element.parent.previous, content_elements: [element.parent], closing_element: element.parent.next, inverted: inverted, condition: condition, inline: true) end |
.find_all(name:, data:, elements:, inverted:, condition: nil, ignore_missing: true, child: false) ⇒ Object
35 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 |
# File 'lib/lm_docstache/block.rb', line 35 def self.find_all(name:, data:, elements:, inverted:, condition: nil, ignore_missing: true, child: false) inverted_op = inverted ? '\^' : '\#' full_tag_regex = /\{\{#{inverted_op}(#{name})\s?#{condition}\}\}.+?\{\{\/\k<1>\}\}/m start_tag_regex = /\{\{#{inverted_op}#{name}\s?#{condition}\}\}/m close_tag_regex = /\{\{\/#{name}\}\}/s if elements.text.match(full_tag_regex) if elements.any? { |e| e.text.match(full_tag_regex) } matches = elements.select { |e| e.text.match(full_tag_regex) } return matches.flat_map do |match| if match.elements.any? find_all(name: name, data: data, elements: match.elements, inverted: inverted, condition: condition, child: true) else extract_block_from_element(name, data, match, inverted, condition) end end else opening = elements.find { |e| e.text.match(start_tag_regex) } content = [] next_sibling = opening.next while !next_sibling.text.match(close_tag_regex) 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" unless ignore_missing end end |
Instance Method Details
#conditional? ⇒ Boolean
31 32 33 |
# File 'lib/lm_docstache/block.rb', line 31 def conditional? type == :conditional end |
#loop? ⇒ Boolean
27 28 29 |
# File 'lib/lm_docstache/block.rb', line 27 def loop? type == :loop end |
#type ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/lm_docstache/block.rb', line 15 def type @type ||= if @inverted :conditional else if @data.get(@name).is_a? Array :loop else :conditional end end end |