Class: IfViewScope

Inherits:
ViewScope show all
Defined in:
lib/volt/server/html_parser/if_view_scope.rb

Instance Attribute Summary

Attributes inherited from ViewScope

#binding_number, #bindings, #html, #path

Instance Method Summary collapse

Methods inherited from ViewScope

#<<, #add_binding, #add_component, #add_content_binding, #add_each, #add_if, #add_template, #add_textarea, #save_binding

Methods included from AttributeScope

#add_id_to_attributes, #add_multiple_attribute, #add_reactive_template, #add_single_attribute, #attribute_string, #process_attribute, #process_attributes, #process_event_binding

Constructor Details

#initialize(handler, path, content) ⇒ IfViewScope

Returns a new instance of IfViewScope.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/volt/server/html_parser/if_view_scope.rb', line 2

def initialize(handler, path, content)
  super(handler, path)
  
  @original_path = @path
  
  @last_content = content
  @branches = []
  
  # We haven't added the if yet
  @if_binding_number = @handler.last.binding_number
  @handler.last.binding_number += 1
  
  @path_number = 0
  
  new_path
end

Instance Method Details

#add_else(content) ⇒ Object

When we reach an else block, we basically commit the current html and template, and start a new one.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/volt/server/html_parser/if_view_scope.rb', line 26

def add_else(content)
  close_scope(false)

  @last_content = content
  
  # Clear existing
  @html = ''
  @bindings = {}
  
  # Close scope removes us, so lets add it back.
  @handler.scope << self
  
  @binding_number = 0

  # Generate a new template path for this section.
  new_path
end

#close_scope(final = true) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/volt/server/html_parser/if_view_scope.rb', line 44

def close_scope(final=true)
  @branches << [@last_content, path]    

  super()

  if final    
    # Add the binding to the parent
    branches = @branches.map do |branch|
      content = branch[0]
      if content == nil
        content = nil.inspect
      else
        content = "Proc.new { #{branch[0]} }"
      end
    
      "[#{content}, #{branch[1].inspect}]"
    end.join(', ')
  
    new_scope = @handler.last

    # variables are captured for branches, so we must prefix them so they don't conflict.
    # page, target, context, id
    new_scope.save_binding(@if_binding_number, "lambda { |__p, __t, __c, __id| IfBinding.new(__p, __t, __c, __id, [#{branches}]) }")
  
    new_scope.html << "<!-- $#{@if_binding_number} --><!-- $/#{@if_binding_number} -->"
  end
end

#new_pathObject



19
20
21
22
# File 'lib/volt/server/html_parser/if_view_scope.rb', line 19

def new_path
  @path = @original_path + "/__if#{@path_number}"
  @path_number += 1
end