blocks 0.7 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/blocks/builder.rb +67 -54
  2. metadata +4 -4
@@ -4,58 +4,8 @@ module Blocks
4
4
  attr_accessor :view, :blocks, :block_positions, :anonymous_block_number,
5
5
  :start_rendering_blocks, :block_groups, :shared_options
6
6
 
7
- # If a method is missing, we'll assume the user is starting a new block group by that missing method name
8
- def method_missing(m, options={}, &block)
9
- # If the specified block group has already been defined, it is simply returned here for iteration.
10
- # It will consist of all the blocks used in this block group that have yet to be rendered,
11
- # as the call for their use occurred before the template was rendered (where their definitions likely occurred)
12
- return self.block_groups[m] unless self.block_groups[m].nil?
13
-
14
- # Allows for nested block groups, store the current block positions array and start a new one
15
- original_block_positions = self.block_positions
16
- self.block_positions = []
17
- self.block_groups[m] = self.block_positions
18
-
19
- # Capture the contents of the block group (this will only capture block definitions and block uses)
20
- view.capture(shared_options.merge(options), &block) if block_given?
21
-
22
- # restore the original block positions array
23
- self.block_positions = original_block_positions
24
- nil
25
- end
26
-
27
- def before(name, options={}, &block)
28
- name = "before_#{name.to_s}".to_sym
29
-
30
- block_container = Blocks::Container.new
31
- block_container.name = name
32
- block_container.options = options
33
- block_container.block = block
34
-
35
- if blocks[name].nil?
36
- blocks[name] = [block_container]
37
- else
38
- blocks[name] << block_container
39
- end
40
-
41
- nil
42
- end
43
-
44
- def after(name, options={}, &block)
45
- name = "after_#{name.to_s}".to_sym
46
-
47
- block_container = Blocks::Container.new
48
- block_container.name = name
49
- block_container.options = options
50
- block_container.block = block
51
-
52
- if blocks[name].nil?
53
- blocks[name] = [block_container]
54
- else
55
- blocks[name] << block_container
56
- end
57
-
58
- nil
7
+ def defined?(name)
8
+ !blocks[name.to_sym].nil?
59
9
  end
60
10
 
61
11
  def define(name, options={}, &block)
@@ -68,6 +18,11 @@ module Blocks
68
18
 
69
19
  nil
70
20
  end
21
+
22
+ def replace(name, options, &block)
23
+ blocks[name.to_sym] = nil
24
+ define(name, options, block)
25
+ end
71
26
 
72
27
  def use(*args, &block)
73
28
  options = args.extract_options!
@@ -105,6 +60,60 @@ module Blocks
105
60
  view.render shared_options[:template], shared_options
106
61
  end
107
62
 
63
+ def before(name, options={}, &block)
64
+ name = "before_#{name.to_s}".to_sym
65
+
66
+ block_container = Blocks::Container.new
67
+ block_container.name = name
68
+ block_container.options = options
69
+ block_container.block = block
70
+
71
+ if blocks[name].nil?
72
+ blocks[name] = [block_container]
73
+ else
74
+ blocks[name] << block_container
75
+ end
76
+
77
+ nil
78
+ end
79
+
80
+ def after(name, options={}, &block)
81
+ name = "after_#{name.to_s}".to_sym
82
+
83
+ block_container = Blocks::Container.new
84
+ block_container.name = name
85
+ block_container.options = options
86
+ block_container.block = block
87
+
88
+ if blocks[name].nil?
89
+ blocks[name] = [block_container]
90
+ else
91
+ blocks[name] << block_container
92
+ end
93
+
94
+ nil
95
+ end
96
+
97
+ # If a method is missing, we'll assume the user is starting a new block group by that missing method name
98
+ def method_missing(m, options={}, &block)
99
+ # If the specified block group has already been defined, it is simply returned here for iteration.
100
+ # It will consist of all the blocks used in this block group that have yet to be rendered,
101
+ # as the call for their use occurred before the template was rendered (where their definitions likely occurred)
102
+ return self.block_groups[m] unless self.block_groups[m].nil?
103
+
104
+ # Allows for nested block groups, store the current block positions array and start a new one
105
+ original_block_positions = self.block_positions
106
+ self.block_positions = []
107
+ self.block_groups[m] = self.block_positions
108
+
109
+ # Capture the contents of the block group (this will only capture block definitions and block uses)
110
+ view.capture(shared_options.merge(options), &block) if block_given?
111
+
112
+ # restore the original block positions array
113
+ self.block_positions = original_block_positions
114
+ nil
115
+ end
116
+
108
117
  protected
109
118
 
110
119
  def initialize(options)
@@ -164,11 +173,13 @@ module Blocks
164
173
  end
165
174
  end
166
175
 
167
- unless view.blocks.blocks[name].nil?
176
+ unless view.blocks.blocks[name].nil? || view.blocks.blocks == blocks
168
177
  view.blocks.blocks[name].each do |block_container|
169
178
  view.concat(view.capture shared_options.merge(block_container.options).merge(options), &block_container.block)
170
179
  end
171
180
  end
181
+
182
+ nil
172
183
  end
173
184
 
174
185
  def render_after_blocks(name, options={})
@@ -180,11 +191,13 @@ module Blocks
180
191
  end
181
192
  end
182
193
 
183
- unless view.blocks.blocks[name].nil?
194
+ unless view.blocks.blocks[name].nil? || view.blocks.blocks == blocks
184
195
  view.blocks.blocks[name].each do |block_container|
185
196
  view.concat(view.capture shared_options.merge(block_container.options).merge(options), &block_container.block)
186
197
  end
187
198
  end
199
+
200
+ nil
188
201
  end
189
202
  end
190
203
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blocks
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 7
9
- version: "0.7"
8
+ - 8
9
+ version: "0.8"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrew Hunter
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-10 00:00:00 -05:00
18
+ date: 2010-12-21 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21