Class: ViewScope

Inherits:
Object show all
Includes:
AttributeScope
Defined in:
lib/volt/server/html_parser/view_scope.rb

Direct Known Subclasses

EachScope, IfViewScope, TextareaScope

Instance Attribute Summary collapse

Instance Method Summary collapse

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) ⇒ ViewScope

Returns a new instance of ViewScope.



9
10
11
12
13
14
15
16
# File 'lib/volt/server/html_parser/view_scope.rb', line 9

def initialize(handler, path)
  @handler = handler
  @path = path

  @html = ''
  @bindings = {}
  @binding_number = 0
end

Instance Attribute Details

#binding_numberObject

Returns the value of attribute binding_number.



7
8
9
# File 'lib/volt/server/html_parser/view_scope.rb', line 7

def binding_number
  @binding_number
end

#bindingsObject (readonly)

Returns the value of attribute bindings.



6
7
8
# File 'lib/volt/server/html_parser/view_scope.rb', line 6

def bindings
  @bindings
end

#htmlObject (readonly)

Returns the value of attribute html.



6
7
8
# File 'lib/volt/server/html_parser/view_scope.rb', line 6

def html
  @html
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/volt/server/html_parser/view_scope.rb', line 7

def path
  @path
end

Instance Method Details

#<<(html) ⇒ Object



18
19
20
# File 'lib/volt/server/html_parser/view_scope.rb', line 18

def <<(html)
  @html << html
end

#add_binding(content) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/volt/server/html_parser/view_scope.rb', line 22

def add_binding(content)
  case content[0]
  when '#'
		command, *content = content.split(/ /)
		content = content.join(' ')

    case command
    when '#if'
      add_if(content)
    when '#elsif'
      add_else(content)
    when '#else'
      if content.blank?
        add_else(nil)
      else
        raise "#else does not take a conditional #{content} was provided."
      end
    when '#each'
      add_each(content)
    when '#template'
      add_template(content)
    end
  when '/'
    # close binding
    close_scope
  else
    # content
    add_content_binding(content)
  end
end

#add_component(tag_name, attributes, unary) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/volt/server/html_parser/view_scope.rb', line 80

def add_component(tag_name, attributes, unary)
  component_name = tag_name[1..-1].gsub(':', '/')

  @handler.html << "<!-- $#{@binding_number} --><!-- $/#{@binding_number} -->"

  data_hash = []
  attributes.each_pair do |name, value|
    parts = value.split(/(\{[^\}]+\})/).reject(&:blank?)
    binding_count = parts.count {|p| p[0] == '{' && p[-1] == '}'}

    # if this attribute has bindings
    if binding_count > 0
      if binding_count > 1
        # Multiple bindings
      elsif parts.size == 1 && binding_count == 1
        # A single binding
        data_hash << "#{name.inspect} => Proc.new { #{value[1..-2]} }"
      end
    else
      # String
      data_hash << "#{name.inspect} => #{value.inspect}"
    end
  end

  arguments = "#{component_name.inspect}, { #{data_hash.join(',')} }"

  save_binding(@binding_number, "lambda { |__p, __t, __c, __id| ComponentBinding.new(__p, __t, __c, __id, #{@path.inspect}, Proc.new { [#{arguments}] }) }")

  @binding_number += 1
end

#add_content_binding(content) ⇒ Object



53
54
55
56
57
# File 'lib/volt/server/html_parser/view_scope.rb', line 53

def add_content_binding(content)
  @handler.html << "<!-- $#{@binding_number} --><!-- $/#{@binding_number} -->"
  save_binding(@binding_number, "lambda { |__p, __t, __c, __id| ContentBinding.new(__p, __t, __c, __id, Proc.new { #{content} }) }")
  @binding_number += 1
end

#add_each(content) ⇒ Object



69
70
71
# File 'lib/volt/server/html_parser/view_scope.rb', line 69

def add_each(content)
  @handler.scope << EachScope.new(@handler, @path, content)
end

#add_else(content) ⇒ Object



65
66
67
# File 'lib/volt/server/html_parser/view_scope.rb', line 65

def add_else(content)
  raise "#else can only be added inside of an if block"
end

#add_if(content) ⇒ Object



59
60
61
62
63
# File 'lib/volt/server/html_parser/view_scope.rb', line 59

def add_if(content)
  # Add with path for if group.
  @handler.scope << IfViewScope.new(@handler, @path + "/__ifg#{@binding_number}", content)
  @binding_number += 1
end

#add_template(content) ⇒ Object



73
74
75
76
77
78
# File 'lib/volt/server/html_parser/view_scope.rb', line 73

def add_template(content)
  @handler.html << "<!-- $#{@binding_number} --><!-- $/#{@binding_number} -->"
  save_binding(@binding_number, "lambda { |__p, __t, __c, __id| TemplateBinding.new(__p, __t, __c, __id, #{@path.inspect}, Proc.new { [#{content}] }) }")

  @binding_number += 1
end

#add_textarea(tag_name, attributes, unary) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/volt/server/html_parser/view_scope.rb', line 111

def add_textarea(tag_name, attributes, unary)
  @handler.scope << TextareaScope.new(@handler, @path + "/__txtarea#{@binding_number}", attributes)
  @binding_number += 1

  # close right away if unary
  @handler.last.close_scope if unary
end

#close_scope(pop = true) ⇒ Object

Called when this scope should be closed out



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/volt/server/html_parser/view_scope.rb', line 120

def close_scope(pop=true)
  if pop
    scope = @handler.scope.pop
  else
    scope = @handler.last
  end

  raise "template path already exists: #{scope.path}" if @handler.templates[scope.path]

  template = {
    'html' => scope.html
  }

  if scope.bindings.size > 0
    # Add the bindings if there are any
    template['bindings'] = scope.bindings
  end

  @handler.templates[scope.path] = template
end

#save_binding(binding_number, code) ⇒ Object



141
142
143
144
# File 'lib/volt/server/html_parser/view_scope.rb', line 141

def save_binding(binding_number, code)
  @bindings[binding_number] ||= []
  @bindings[binding_number] << code
end