building-blocks 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/building_blocks/building_blocks.rb +100 -44
- data/lib/building_blocks/helper_methods.rb +1 -4
- metadata +5 -5
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
@@ -1,33 +1,80 @@
|
|
1
1
|
module BuildingBlocks
|
2
2
|
class Base
|
3
|
+
attr_accessor :view
|
3
4
|
|
4
|
-
attr_accessor :
|
5
|
-
:start_rendering_blocks, :block_groups, :shared_options
|
5
|
+
attr_accessor :blocks
|
6
6
|
|
7
|
+
attr_accessor :block
|
8
|
+
|
9
|
+
# Array of BuildingBlocks::Container objects, storing the order of blocks as they were used
|
10
|
+
attr_accessor :block_positions
|
11
|
+
|
12
|
+
# counter, used to give unnamed blocks a unique name
|
13
|
+
attr_accessor :anonymous_block_number
|
14
|
+
|
15
|
+
attr_accessor :start_rendering_blocks
|
16
|
+
|
17
|
+
attr_accessor :block_groups
|
18
|
+
|
19
|
+
# These are the options that are passed into the initalize method
|
20
|
+
attr_accessor :global_options
|
21
|
+
|
22
|
+
# Checks if a particular block has been defined within the current block scope.
|
23
|
+
# <%= blocks.defined? :some_block_name %>
|
24
|
+
# Options:
|
25
|
+
# [+name+]
|
26
|
+
# The name of the block to check
|
7
27
|
def defined?(name)
|
8
28
|
!blocks[name.to_sym].nil?
|
9
29
|
end
|
10
30
|
|
31
|
+
# Define a block, unless a block by the same name is already defined.
|
32
|
+
# <%= blocks.define :some_block_name, :parameter1 => "1", :parameter2 => "2" do |options| %>
|
33
|
+
# <%= options[:parameter1] %> and <%= options[:parameter2] %>
|
34
|
+
# <% end %>
|
35
|
+
#
|
36
|
+
# Options:
|
37
|
+
# [+name+]
|
38
|
+
# The name of the block being defined (either a string or a symbol)
|
39
|
+
# [+options+]
|
40
|
+
# The default options for the block definition. Any or all of these options may be overrideen by
|
41
|
+
# whomever calls "blocks.use" on this block.
|
42
|
+
# [+block+]
|
43
|
+
# The block that is to be rendered when "blocks.use" is called for this block.
|
11
44
|
def define(name, options={}, &block)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
45
|
+
# Check if a block by this name is already defined.
|
46
|
+
if blocks[name.to_sym].nil?
|
47
|
+
# Store the attributes of the defined block in a container for later use
|
48
|
+
block_container = BuildingBlocks::Container.new
|
49
|
+
block_container.name = name
|
50
|
+
block_container.options = options
|
51
|
+
block_container.block = block
|
16
52
|
|
17
|
-
|
53
|
+
blocks[name.to_sym] = block_container
|
54
|
+
end
|
18
55
|
|
19
56
|
nil
|
20
57
|
end
|
21
58
|
|
59
|
+
# Define a block, replacing an existing block by the same name if it is already defined.
|
60
|
+
# <%= blocks.define :some_block_name, :parameter1 => "1", :parameter2 => "2" do |options| %>
|
61
|
+
# <%= options[:parameter1] %> and <%= options[:parameter2] %>
|
62
|
+
# <% end %>
|
63
|
+
#
|
64
|
+
# <%= blocks.replace :some_block_name, :parameter3 => "3", :parameter4 => "4" do |options| %>
|
65
|
+
# <%= options[:parameter3] %> and <%= options[:parameter4] %>
|
66
|
+
# <% end %>
|
67
|
+
# Options:
|
68
|
+
# [+name+]
|
69
|
+
# The name of the block being defined (either a string or a symbol)
|
70
|
+
# [+options+]
|
71
|
+
# The default options for the block definition. Any or all of these options may be overrideen by
|
72
|
+
# whomever calls "blocks.use" on this block.
|
73
|
+
# [+block+]
|
74
|
+
# The block that is to be rendered when "blocks.use" is called for this block.
|
22
75
|
def replace(name, options={}, &block)
|
23
|
-
|
24
|
-
|
25
|
-
block_container.options = options
|
26
|
-
block_container.block = block
|
27
|
-
|
28
|
-
blocks[name.to_sym] = block_container
|
29
|
-
|
30
|
-
nil
|
76
|
+
blocks[name.to_sym] = nil
|
77
|
+
define(name, options, &block)
|
31
78
|
end
|
32
79
|
|
33
80
|
def use(*args, &block)
|
@@ -42,26 +89,27 @@ module BuildingBlocks
|
|
42
89
|
block_container.options = options
|
43
90
|
block_container.block = block
|
44
91
|
|
45
|
-
blocks[name.to_sym] = block_container if !name.is_a?(BuildingBlocks::Container) and blocks[name.to_sym].nil? and
|
92
|
+
blocks[name.to_sym] = block_container if !name.is_a?(BuildingBlocks::Container) and blocks[name.to_sym].nil? and block_given?
|
46
93
|
|
47
|
-
if start_rendering_blocks
|
94
|
+
if start_rendering_blocks
|
48
95
|
render_block name, args, options
|
49
96
|
else
|
50
97
|
# Delays rendering this block until the partial has been rendered and all the blocks have had a chance to be defined
|
51
98
|
self.block_positions << block_container
|
99
|
+
nil
|
52
100
|
end
|
53
101
|
|
54
|
-
nil
|
102
|
+
# nil
|
55
103
|
end
|
56
104
|
|
57
105
|
def render
|
58
106
|
self.start_rendering_blocks = false
|
59
107
|
|
60
|
-
|
108
|
+
global_options[:captured_block] = view.capture(self, &self.block) if self.block
|
61
109
|
|
62
110
|
self.start_rendering_blocks = true
|
63
111
|
|
64
|
-
view.render
|
112
|
+
view.render global_options[:template], global_options
|
65
113
|
end
|
66
114
|
|
67
115
|
def before(name, options={}, &block)
|
@@ -113,7 +161,7 @@ module BuildingBlocks
|
|
113
161
|
self.block_groups[m] = self.block_positions
|
114
162
|
|
115
163
|
# Capture the contents of the block group (this will only capture block definitions and block uses)
|
116
|
-
view.capture(
|
164
|
+
view.capture(global_options.merge(options), &block) if block_given?
|
117
165
|
|
118
166
|
# restore the original block positions array
|
119
167
|
self.block_positions = original_block_positions
|
@@ -122,13 +170,13 @@ module BuildingBlocks
|
|
122
170
|
|
123
171
|
protected
|
124
172
|
|
125
|
-
def initialize(options)
|
126
|
-
self.view = options[:view]
|
127
|
-
self.shared_options = options
|
128
|
-
|
173
|
+
def initialize(view, options={}, &block)
|
129
174
|
options[options[:variable] ? options[:variable].to_sym : :blocks] = self
|
130
175
|
options[:templates_folder] = "blocks" if options[:templates_folder].nil?
|
131
176
|
|
177
|
+
self.view = view
|
178
|
+
self.global_options = options
|
179
|
+
self.block = block
|
132
180
|
self.block_positions = []
|
133
181
|
self.blocks = {}
|
134
182
|
self.anonymous_block_number = 0
|
@@ -141,9 +189,11 @@ module BuildingBlocks
|
|
141
189
|
"block_#{anonymous_block_number}"
|
142
190
|
end
|
143
191
|
|
144
|
-
def render_block(name_or_container, args, options={})
|
192
|
+
def render_block(name_or_container, args, options={})
|
145
193
|
render_options = options
|
146
194
|
|
195
|
+
buffer = ActionView::NonConcattingString.new
|
196
|
+
|
147
197
|
if (name_or_container.is_a?(BuildingBlocks::Container))
|
148
198
|
name = name_or_container.name.to_sym
|
149
199
|
render_options = render_options.merge(name_or_container.options)
|
@@ -151,86 +201,92 @@ module BuildingBlocks
|
|
151
201
|
name = name_or_container.to_sym
|
152
202
|
end
|
153
203
|
|
154
|
-
|
204
|
+
buffer << render_before_blocks(name, options)
|
155
205
|
|
156
206
|
if blocks[name]
|
157
207
|
block_container = blocks[name]
|
158
208
|
|
159
|
-
args.push(
|
209
|
+
args.push(global_options.merge(block_container.options).merge(render_options))
|
160
210
|
|
161
211
|
# If the block is taking more than one parameter, we can use *args
|
162
212
|
if block_container.block.arity > 1
|
163
|
-
view.
|
213
|
+
buffer << view.capture(*args, &block_container.block)
|
164
214
|
|
165
215
|
# However, if the block only takes a single parameter, we do not want ruby to try to cram the args list into that parameter
|
166
216
|
# as an array
|
167
217
|
else
|
168
|
-
view.
|
218
|
+
buffer << view.capture(args.first, &block_container.block)
|
169
219
|
end
|
170
220
|
elsif view.blocks.blocks[name]
|
171
221
|
block_container = view.blocks.blocks[name]
|
172
222
|
|
173
|
-
args.push(
|
223
|
+
args.push(global_options.merge(block_container.options).merge(render_options))
|
174
224
|
|
175
225
|
# If the block is taking more than one parameter, we can use *args
|
176
226
|
if block_container.block.arity > 1
|
177
|
-
view.
|
227
|
+
buffer << view.capture(*args, &block_container.block)
|
178
228
|
|
179
229
|
# However, if the block only takes a single parameter, we do not want ruby to try to cram the args list into that parameter
|
180
230
|
# as an array
|
181
231
|
else
|
182
|
-
view.
|
232
|
+
buffer << view.capture(args.first, &block_container.block)
|
183
233
|
end
|
184
234
|
else
|
185
235
|
begin
|
186
|
-
begin
|
187
|
-
view.
|
188
|
-
rescue ActionView::MissingTemplate
|
236
|
+
begin
|
237
|
+
buffer << view.render("#{name.to_s}", global_options.merge(render_options))
|
238
|
+
rescue ActionView::MissingTemplate
|
189
239
|
# This partial did not exist in the current controller's view directory; now checking in the default templates folder
|
190
|
-
view.
|
240
|
+
buffer << view.render("#{self.global_options[:templates_folder]}/#{name.to_s}", global_options.merge(render_options))
|
191
241
|
end
|
192
242
|
rescue ActionView::MissingTemplate
|
193
243
|
# This block does not exist and no partial can be found to satify it
|
194
244
|
end
|
195
245
|
end
|
196
246
|
|
197
|
-
|
247
|
+
buffer << render_after_blocks(name, options)
|
248
|
+
|
249
|
+
buffer
|
198
250
|
end
|
199
251
|
|
200
252
|
def render_before_blocks(name, options={})
|
201
253
|
name = "before_#{name.to_s}".to_sym
|
202
254
|
|
255
|
+
buffer = ActionView::NonConcattingString.new
|
256
|
+
|
203
257
|
unless blocks[name].nil?
|
204
258
|
blocks[name].each do |block_container|
|
205
|
-
view.
|
259
|
+
buffer << view.capture(global_options.merge(block_container.options).merge(options), &block_container.block)
|
206
260
|
end
|
207
261
|
end
|
208
262
|
|
209
263
|
unless view.blocks.blocks[name].nil? || view.blocks.blocks == blocks
|
210
264
|
view.blocks.blocks[name].each do |block_container|
|
211
|
-
view.
|
265
|
+
buffer << view.capture(global_options.merge(block_container.options).merge(options), &block_container.block)
|
212
266
|
end
|
213
267
|
end
|
214
268
|
|
215
|
-
|
269
|
+
buffer
|
216
270
|
end
|
217
271
|
|
218
272
|
def render_after_blocks(name, options={})
|
219
273
|
name = "after_#{name.to_s}".to_sym
|
220
274
|
|
275
|
+
buffer = ActionView::NonConcattingString.new
|
276
|
+
|
221
277
|
unless blocks[name].nil?
|
222
278
|
blocks[name].each do |block_container|
|
223
|
-
view.
|
279
|
+
buffer << view.capture(global_options.merge(block_container.options).merge(options), &block_container.block)
|
224
280
|
end
|
225
281
|
end
|
226
282
|
|
227
283
|
unless view.blocks.blocks[name].nil? || view.blocks.blocks == blocks
|
228
284
|
view.blocks.blocks[name].each do |block_container|
|
229
|
-
view.
|
285
|
+
buffer << view.capture(global_options.merge(block_container.options).merge(options), &block_container.block)
|
230
286
|
end
|
231
287
|
end
|
232
288
|
|
233
|
-
|
289
|
+
buffer
|
234
290
|
end
|
235
291
|
end
|
236
292
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: building-blocks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew Hunter
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-16 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
|
-
- -
|
42
|
+
- - ">="
|
43
43
|
- !ruby/object:Gem::Version
|
44
44
|
hash: 7
|
45
45
|
segments:
|