rocketio 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 058d969c6b3f050392f25ce9be512029b10cf7cf
|
4
|
+
data.tar.gz: fe12296fa46e09d621044bc15a926099daf0f85d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57ff44d810bc535f35cacc5fd09983e9fda1c2902296e14744ec56e7c59a80fa2229c3302ce2a27a0a24fc6a613a4063cc25510c70e124a77fa9286315d70acb
|
7
|
+
data.tar.gz: 98a60d1d19ef24fa204796f607eefb9dea785a11af451ad5bceb6b60027709869f30a79d2402bee2e4521a02b27216340b3b99bf33b56a62c55c67e38881492e
|
@@ -69,7 +69,7 @@ module RocketIO
|
|
69
69
|
engine, engine_opts = resolve_engine(opts)
|
70
70
|
template = block_given? ? yield : resolve_template(template, engine)
|
71
71
|
scope = opts.fetch(:scope, self)
|
72
|
-
locals = opts.fetch(:locals, RocketIO::EMPTY_HASH).freeze
|
72
|
+
locals = template_vars.merge(opts.fetch(:locals, RocketIO::EMPTY_HASH)).freeze
|
73
73
|
layout = opts.fetch(:layout, self.layout)
|
74
74
|
template = compile_template(template, engine, engine_opts).render(scope, locals)
|
75
75
|
layout ? render_layout(layout, opts) {template} : template
|
@@ -110,8 +110,8 @@ module RocketIO
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
113
|
-
scope
|
114
|
-
locals
|
113
|
+
scope = opts.fetch(:scope, self)
|
114
|
+
locals = template_vars.merge(opts.fetch(:locals, RocketIO::EMPTY_HASH)).freeze
|
115
115
|
compile_template(template, engine, engine_opts)
|
116
116
|
.render(scope, locals, &(block || RocketIO::EMPTY_STRING_PROC))
|
117
117
|
end
|
@@ -164,3 +164,4 @@ require 'rocketio/controller/render/engine'
|
|
164
164
|
require 'rocketio/controller/render/layout'
|
165
165
|
require 'rocketio/controller/render/layouts'
|
166
166
|
require 'rocketio/controller/render/templates'
|
167
|
+
require 'rocketio/controller/render/template_vars'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module RocketIO
|
2
|
+
class Controller
|
3
|
+
|
4
|
+
# define variables to be used on templates alongside provided locals.
|
5
|
+
#
|
6
|
+
# @param name
|
7
|
+
# @param value
|
8
|
+
#
|
9
|
+
def self.define_template_var name, value = nil, &block
|
10
|
+
value || block || raise(ArgumentError, 'A value or a block expected')
|
11
|
+
(@__template_vars__ ||= {})[name.to_sym] = (block || value).freeze
|
12
|
+
define_template_vars_methods
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.define_template_vars_methods source = self
|
16
|
+
return unless source.instance_variables.include?(:@__template_vars__)
|
17
|
+
vars = source.instance_variable_get(:@__template_vars__).each_with_object(allocate.__template_vars__.dup) do |(name,value),o|
|
18
|
+
o[name] = :"__#{name}_template_var__"
|
19
|
+
if value.is_a?(Proc)
|
20
|
+
define_method(o[name], &value)
|
21
|
+
else
|
22
|
+
define_method(o[name]) {value}
|
23
|
+
end
|
24
|
+
end.freeze
|
25
|
+
define_method(:__template_vars__) {vars}
|
26
|
+
end
|
27
|
+
|
28
|
+
def __template_vars__; RocketIO::EMPTY_HASH end
|
29
|
+
|
30
|
+
def template_vars
|
31
|
+
__template_vars__.each_with_object({}) {|(k,v),o| o[k] = __send__(v)}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/rocketio/version.rb
CHANGED
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'setup'
|
2
|
+
|
3
|
+
spec :template_vars do
|
4
|
+
context :compositing do
|
5
|
+
it 'inherits vars from superclass' do
|
6
|
+
a = mock_controller {
|
7
|
+
define_template_var(:a, 'a')
|
8
|
+
}
|
9
|
+
b = mock_controller(a) {}.initialize_controller
|
10
|
+
assert(b.template_vars[:a]) == 'a'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'complements vars inherited from superclass' do
|
14
|
+
a = mock_controller {
|
15
|
+
define_template_var(:a, 'a')
|
16
|
+
}
|
17
|
+
b = mock_controller(a) {
|
18
|
+
define_template_var(:b, 'b')
|
19
|
+
}.initialize_controller
|
20
|
+
assert(b.template_vars[:a]) == 'a'
|
21
|
+
assert(b.template_vars[:b]) == 'b'
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'overrides vars inherited from superclass' do
|
25
|
+
a = mock_controller {
|
26
|
+
define_template_var(:a, 'a')
|
27
|
+
}
|
28
|
+
b = mock_controller(a) {
|
29
|
+
define_template_var(:a, 'b')
|
30
|
+
}.initialize_controller
|
31
|
+
assert(b.template_vars[:a]) == 'b'
|
32
|
+
end
|
33
|
+
|
34
|
+
test '`inherit` overrides vars inherited from superclass' do
|
35
|
+
a = mock_controller {
|
36
|
+
define_template_var(:a, 'a')
|
37
|
+
}
|
38
|
+
b = mock_controller(a) {
|
39
|
+
define_template_var(:a, 'b')
|
40
|
+
}
|
41
|
+
c = mock_controller(a) {
|
42
|
+
inherit :template_vars, from: b
|
43
|
+
}.initialize_controller
|
44
|
+
assert(c.template_vars[:a]) == 'b'
|
45
|
+
end
|
46
|
+
|
47
|
+
test '`inherit` complements vars inherited from superclass' do
|
48
|
+
a = mock_controller {
|
49
|
+
define_template_var(:a, 'a')
|
50
|
+
}
|
51
|
+
b = mock_controller(a) {
|
52
|
+
define_template_var(:b, 'b')
|
53
|
+
}
|
54
|
+
c = mock_controller(a) {
|
55
|
+
inherit :template_vars, from: b
|
56
|
+
}.initialize_controller
|
57
|
+
assert(c.template_vars[:a]) == 'a'
|
58
|
+
assert(c.template_vars[:b]) == 'b'
|
59
|
+
end
|
60
|
+
|
61
|
+
test '`inherit` overrides defined vars' do
|
62
|
+
a = mock_controller {
|
63
|
+
define_template_var(:a, 'a')
|
64
|
+
}
|
65
|
+
b = mock_controller {
|
66
|
+
define_template_var(:a, 'b')
|
67
|
+
inherit :template_vars, from: a
|
68
|
+
}.initialize_controller
|
69
|
+
assert(b.template_vars[:a]) == 'a'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context :rendering do
|
74
|
+
|
75
|
+
it 'accepts plain value via second argument' do
|
76
|
+
c = mock_controller {
|
77
|
+
define_template(:get) {'<%= x %>'}
|
78
|
+
define_template_var(:x, 'y')
|
79
|
+
}.initialize_controller
|
80
|
+
assert(c.render(:get)) == 'y'
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'accepts a proc as second argument' do
|
84
|
+
c = mock_controller {
|
85
|
+
define_template(:get) {'<%= x %>'}
|
86
|
+
define_template_var(:x, -> {'y'})
|
87
|
+
}.initialize_controller
|
88
|
+
assert(c.render(:get)) == 'y'
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'accepts a block without second argument' do
|
92
|
+
c = mock_controller {
|
93
|
+
define_template(:get) {'<%= x %>'}
|
94
|
+
define_template_var(:x) {'y'}
|
95
|
+
}.initialize_controller
|
96
|
+
assert(c.render(:get)) == 'y'
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'raises ArgumentError if no value nor block given' do
|
100
|
+
assert { mock_controller {
|
101
|
+
define_template_var(:x)
|
102
|
+
}}.raise(ArgumentError)
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rocketio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Slee Woo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -138,6 +138,7 @@ files:
|
|
138
138
|
- lib/rocketio/controller/render/engine.rb
|
139
139
|
- lib/rocketio/controller/render/layout.rb
|
140
140
|
- lib/rocketio/controller/render/layouts.rb
|
141
|
+
- lib/rocketio/controller/render/template_vars.rb
|
141
142
|
- lib/rocketio/controller/render/templates.rb
|
142
143
|
- lib/rocketio/controller/request.rb
|
143
144
|
- lib/rocketio/controller/response.rb
|
@@ -178,6 +179,7 @@ files:
|
|
178
179
|
- test/render/put.erb
|
179
180
|
- test/render/render_test.rb
|
180
181
|
- test/render/setup.rb
|
182
|
+
- test/render/template_vars_test.rb
|
181
183
|
- test/render/templates/a/get.erb
|
182
184
|
- test/render/templates/master.erb
|
183
185
|
- test/render/templates_test.rb
|
@@ -210,5 +212,5 @@ rubyforge_project:
|
|
210
212
|
rubygems_version: 2.5.1
|
211
213
|
signing_key:
|
212
214
|
specification_version: 4
|
213
|
-
summary: '["rocketio-0.0.
|
215
|
+
summary: '["rocketio-0.0.8", "Simple, fast, scalable web framework for Ruby"]'
|
214
216
|
test_files: []
|