Class: Volt::ViewScope
Instance Attribute Summary collapse
Instance Method Summary
collapse
#add_asset_url_attribute, #add_id_to_attributes, #add_multiple_attribute, #add_single_attribute, #add_string_template_renderer, #attribute_string, #binding_parts_and_count, #getter_to_setter, included, #process_attribute, #process_attributes, #process_event_binding
Constructor Details
#initialize(handler, path) ⇒ ViewScope
Returns a new instance of ViewScope.
10
11
12
13
14
15
16
17
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 10
def initialize(handler, path)
@handler = handler
@path = path
@html = ''
@bindings = {}
@binding_number = 0
end
|
Instance Attribute Details
#binding_number ⇒ Object
Returns the value of attribute binding_number.
8
9
10
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 8
def binding_number
@binding_number
end
|
Returns the value of attribute bindings.
7
8
9
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 7
def bindings
@bindings
end
|
Returns the value of attribute html.
7
8
9
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 7
def html
@html
end
|
Returns the value of attribute path.
8
9
10
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 8
def path
@path
end
|
Instance Method Details
19
20
21
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 19
def <<(html)
@html << html
end
|
#add_asset_url(args) ⇒ Object
The asset_url binding handles linking assets so they will be precompiled properly using the sprockets pipeline.
151
152
153
154
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 151
def add_asset_url(args)
@handler.link_asset(args.gsub(/["']/, '').strip)
end
|
#add_binding(content) ⇒ Object
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 23
def add_binding(content)
content = content.strip
index = content.index(/[ \(]/)
if index
first_symbol = content[0...index]
args = content[index..-1].strip
case first_symbol
when 'if'
add_if(args)
when 'unless'
add_if("!(#{args})")
when 'elsif'
add_else(args)
when 'else'
if args.blank?
add_else(nil)
else
fail "else does not take a conditional, #{content} was provided."
end
when 'view'
add_template(args)
when 'template'
Volt.logger.warn('Deprecation warning: The template binding has been renamed to view. Please update any views accordingly.')
add_template(args)
when 'yield'
add_yield(args)
when 'asset_url'
add_asset_url(args)
else
if content =~ /.each\s+do\s+\|/
add_each(content, false)
elsif content =~ /.each_with_index\s+do\s+\|/
add_each(content, true)
elsif content[0] == '#'
else
add_content_binding(content)
end
end
else
case content
when 'end'
close_scope
when 'else'
add_else(nil)
when 'yield'
add_yield
else
add_content_binding(content)
end
end
nil
end
|
#add_component(tag_name, attributes, unary) ⇒ Object
135
136
137
138
139
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 135
def add_component(tag_name, attributes, unary)
@handler.scope << ComponentViewScope.new(@handler, @path + "/__component#{@binding_number}", tag_name, attributes, unary)
@handler.last.close_scope if unary
end
|
#add_content_binding(content) ⇒ Object
80
81
82
83
84
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 80
def add_content_binding(content)
@handler.html << "<!-- $#{@binding_number} --><!-- $/#{@binding_number} -->"
save_binding(@binding_number, "lambda { |__p, __t, __c, __id| Volt::ContentBinding.new(__p, __t, __c, __id, Proc.new { #{content} }) }")
@binding_number += 1
end
|
#add_each(content, with_index) ⇒ Object
96
97
98
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 96
def add_each(content, with_index)
@handler.scope << EachScope.new(@handler, @path + "/__each#{@binding_number}", content, with_index)
end
|
#add_else(content) ⇒ Object
92
93
94
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 92
def add_else(content)
fail '#else can only be added inside of an if block'
end
|
#add_if(content) ⇒ Object
86
87
88
89
90
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 86
def add_if(content)
@handler.scope << IfViewScope.new(@handler, @path + "/__ifg#{@binding_number}", content)
@binding_number += 1
end
|
#add_template(content) ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 100
def add_template(content)
content = content.strip.gsub(/^\(/, '').gsub(/\)$/, '')
@handler.html << "<!-- $#{@binding_number} --><!-- $/#{@binding_number} -->"
save_binding(@binding_number, "lambda { |__p, __t, __c, __id| Volt::ViewBinding.new(__p, __t, __c, __id, #{@path.inspect}, Proc.new { [#{content}] }) }")
@binding_number += 1
end
|
#add_textarea(tag_name, attributes, unary) ⇒ Object
141
142
143
144
145
146
147
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 141
def add_textarea(tag_name, attributes, unary)
@handler.scope << TextareaScope.new(@handler, @path + "/__txtarea#{@binding_number}", attributes)
@binding_number += 1
@handler.last.close_scope if unary
end
|
#add_yield(content = nil) ⇒ Object
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 110
def add_yield(content = nil)
content ||= ''
content = content.strip.gsub(/^\(/, '').gsub(/\)$/, '')
@handler.html << "<!-- $#{@binding_number} --><!-- $/#{@binding_number} -->"
save_binding(@binding_number, "lambda { |__p, __t, __c, __id| Volt::YieldBinding.new(__p, __t, __c, __id, Proc.new { [#{content}] }) }")
@binding_number += 1
end
|
#close_scope(pop = true) ⇒ Object
Called when this scope should be closed out
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 157
def close_scope(pop = true)
if pop
scope = @handler.scope.pop
else
scope = @handler.last
end
fail "template path already exists: #{scope.path}" if @handler.templates[scope.path]
template = {
'html' => scope.html
}
if scope.bindings.size > 0
template['bindings'] = scope.bindings
end
@handler.templates[scope.path] = template
end
|
#last_method_name(getter) ⇒ Object
131
132
133
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 131
def last_method_name(getter)
getter.strip[/[^.]+$/]
end
|
#parent_fetcher(getter) ⇒ Object
Returns ruby code to fetch the parent. (by removing the last fetch) TODO: Probably want to do this with AST transforms with the parser/unparser gems
123
124
125
126
127
128
129
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 123
def parent_fetcher(getter)
parent = getter.strip.gsub(/[.][^.]+$/, '')
parent = 'self' if parent.blank? || !getter.index('.')
parent
end
|
#save_binding(binding_number, code) ⇒ Object
178
179
180
181
|
# File 'lib/volt/server/html_parser/view_scope.rb', line 178
def save_binding(binding_number, code)
@bindings[binding_number] ||= []
@bindings[binding_number] << code
end
|