Class: Sablon::Processor::Document::ConditionalHandler

Inherits:
FieldHandler
  • Object
show all
Defined in:
lib/sablon/processor/document/field_handlers.rb

Overview

Handles conditional blocks in the template

Instance Method Summary collapse

Methods inherited from FieldHandler

#handles?

Constructor Details

#initializeConditionalHandler

Returns a new instance of ConditionalHandler.



59
60
61
# File 'lib/sablon/processor/document/field_handlers.rb', line 59

def initialize
  super(/([^ ]+):if(?:\(([^)]+)\))?/)
end

Instance Method Details

#build_statement(constructor, field, _options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sablon/processor/document/field_handlers.rb', line 63

def build_statement(constructor, field, _options = {})
  expr_name = field.expression.match(@pattern).to_a[1]
  args = [
    # end expression (first arg)
    "#{expr_name}:endIf",
    # sub block patterns to check for
    /(#{expr_name}):els[iI]f(?:\(([^)]+)\))?/,
    /(#{expr_name}):else/
  ]
  blocks = process_blocks(constructor.consume_multi_block(*args))
  Statement::Condition.new(blocks)
end