Class: Sablon::Processor::Document::OperationConstruction

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

Instance Method Summary collapse

Constructor Details

#initialize(fields, field_handlers, default_handler) ⇒ OperationConstruction

Returns a new instance of OperationConstruction.



5
6
7
8
9
10
# File 'lib/sablon/processor/document/operation_construction.rb', line 5

def initialize(fields, field_handlers, default_handler)
  @fields = fields
  @field_handlers = field_handlers
  @default_handler = default_handler
  @operations = []
end

Instance Method Details

#consume(allow_insertion) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sablon/processor/document/operation_construction.rb', line 17

def consume(allow_insertion)
  return unless (@field = @fields.shift)
  #
  # step over provided handlers to see if any can process the field
  handler = @field_handlers.detect(proc { @default_handler }) do |fh|
    fh.handles?(@field)
  end
  return if handler.nil?
  #
  # process and return
  handler.build_statement(self, @field, allow_insertion: allow_insertion)
end

#consume_block(end_expression) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sablon/processor/document/operation_construction.rb', line 30

def consume_block(end_expression)
  start_field = end_field = @field
  while end_field && end_field.expression != end_expression
    consume(false)
    end_field = @field
  end

  unless end_field
    raise TemplateError, "Could not find end field for «#{start_field.expression}». Was looking for «#{end_expression}»"
  end
  Block.enclosed_by(start_field, end_field) if end_field
end

#consume_multi_block(end_expression, *sub_expr_patterns) ⇒ Object

Creates multiple blocks based on the sub expression patterns supplied while searching for the end expresion. The start and end fields of adjacent blocks are shared. For example in an if-else-endif block the else field is the end for the if clause block and the start of the else clause block.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sablon/processor/document/operation_construction.rb', line 48

def consume_multi_block(end_expression, *sub_expr_patterns)
  start_field = end_field = @field
  blocks = []
  while end_field && end_field.expression != end_expression
    consume(false)
    break unless (end_field = @field)
    if sub_expr_patterns.any? { |pat| end_field.expression =~ pat }
      blocks << Block.enclosed_by(start_field, end_field)
      start_field = end_field
    end
  end

  # raise error if no final end field
  unless end_field
    raise TemplateError, "Could not find end field for «#{start_field.expression}». Was looking for «#{end_expression}»"
  end

  # add final block and return
  blocks << Block.enclosed_by(start_field, end_field)
end

#operationsObject



12
13
14
15
# File 'lib/sablon/processor/document/operation_construction.rb', line 12

def operations
  @operations << consume(true) while @fields.any?
  @operations.compact
end