Class: CouchRest::Mixins::Callbacks::Callback
- Inherits:
-
Object
- Object
- CouchRest::Mixins::Callbacks::Callback
- Defined in:
- lib/couchrest/mixins/callbacks.rb
Constant Summary
- @@_callback_sequence =
0
Instance Attribute Summary (collapse)
-
- (Object) filter
Returns the value of attribute filter.
-
- (Object) kind
Returns the value of attribute kind.
-
- (Object) klass
Returns the value of attribute klass.
-
- (Object) name
Returns the value of attribute name.
-
- (Object) options
Returns the value of attribute options.
-
- (Object) per_key
Returns the value of attribute per_key.
Instance Method Summary (collapse)
- - (Object) _compile_per_key_options
- - (Object) _update_filter(filter_options, new_options)
- - (Object) clone(klass)
-
- (Object) end(key = nil, options = {})
This will supply contents for around and after filters, but not before filters (for the backward pass).
-
- (Callback) initialize(filter, kind, options, klass)
constructor
A new instance of Callback.
- - (Boolean) matches?(_kind, _filter)
- - (Object) next_id
- - (Object) normalize_options!(options)
- - (Object) recompile!(_options, _per_key)
-
- (Object) start(key = nil, options = {})
This will supply contents for before and around filters, and no contents for after filters (for the forward pass).
Constructor Details
- (Callback) initialize(filter, kind, options, klass)
A new instance of Callback
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/couchrest/mixins/callbacks.rb', line 117 def initialize(filter, kind, , klass) @kind, @klass = kind, klass () @per_key = .delete(:per_key) @raw_filter, @options = filter, @filter = _compile_filter(filter) @compiled_options = () @callback_id = next_id end |
Instance Attribute Details
- (Object) filter
Returns the value of attribute filter
116 117 118 |
# File 'lib/couchrest/mixins/callbacks.rb', line 116 def filter @filter end |
- (Object) kind
Returns the value of attribute kind
116 117 118 |
# File 'lib/couchrest/mixins/callbacks.rb', line 116 def kind @kind end |
- (Object) klass
Returns the value of attribute klass
116 117 118 |
# File 'lib/couchrest/mixins/callbacks.rb', line 116 def klass @klass end |
- (Object) name
Returns the value of attribute name
116 117 118 |
# File 'lib/couchrest/mixins/callbacks.rb', line 116 def name @name end |
- (Object) options
Returns the value of attribute options
116 117 118 |
# File 'lib/couchrest/mixins/callbacks.rb', line 116 def @options end |
- (Object) per_key
Returns the value of attribute per_key
116 117 118 |
# File 'lib/couchrest/mixins/callbacks.rb', line 116 def per_key @per_key end |
Instance Method Details
- (Object) _compile_per_key_options
176 177 178 179 180 181 182 183 184 |
# File 'lib/couchrest/mixins/callbacks.rb', line 176 def = (@per_key) @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def _one_time_conditions_valid_#{@callback_id}? true #{[0]} end RUBY_EVAL end |
- (Object) _update_filter(filter_options, new_options)
161 162 163 164 |
# File 'lib/couchrest/mixins/callbacks.rb', line 161 def _update_filter(, ) [:if].push([:unless]) if .key?(:unless) [:unless].push([:if]) if .key?(:if) end |
- (Object) clone(klass)
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/couchrest/mixins/callbacks.rb', line 131 def clone(klass) obj = super() obj.klass = klass obj.per_key = @per_key.dup obj. = @options.dup obj.per_key[:if] = @per_key[:if].dup obj.per_key[:unless] = @per_key[:unless].dup obj.[:if] = @options[:if].dup obj.[:unless] = @options[:unless].dup obj end |
- (Object) end(key = nil, options = {})
This will supply contents for around and after filters, but not before filters (for the backward pass).
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/couchrest/mixins/callbacks.rb', line 247 def end(key = nil, = {}) object = ( || {})[:object] return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?") if @kind == :around || @kind == :after # if condition # after_save :filter_name, :if => :condition # filter_name # end if @kind == :after [@compiled_options[0], @filter, @compiled_options[1]].compact.join("\n") else "end" end end end |
- (Boolean) matches?(_kind, _filter)
156 157 158 159 |
# File 'lib/couchrest/mixins/callbacks.rb', line 156 def matches?(_kind, _filter) @kind == _kind && @filter == _filter end |
- (Object) next_id
152 153 154 |
# File 'lib/couchrest/mixins/callbacks.rb', line 152 def next_id @@_callback_sequence += 1 end |
- (Object) normalize_options!(options)
143 144 145 146 147 148 149 150 |
# File 'lib/couchrest/mixins/callbacks.rb', line 143 def () [:if] = Array.wrap([:if]) [:unless] = Array.wrap([:unless]) [:per_key] ||= {} [:per_key][:if] = Array.wrap([:per_key][:if]) [:per_key][:unless] = Array.wrap([:per_key][:unless]) end |
- (Object) recompile!(_options, _per_key)
166 167 168 169 170 171 172 173 174 |
# File 'lib/couchrest/mixins/callbacks.rb', line 166 def recompile!(, _per_key) _update_filter(self., ) _update_filter(self.per_key, _per_key) @callback_id = next_id @filter = _compile_filter(@raw_filter) @compiled_options = (@options) end |
- (Object) start(key = nil, options = {})
This will supply contents for before and around filters, and no contents for after filters (for the forward pass).
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/couchrest/mixins/callbacks.rb', line 188 def start(key = nil, = {}) object, terminator = ( || {}).values_at(:object, :terminator) return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?") terminator ||= false # options[0] is the compiled form of supplied conditions # options[1] is the "end" for the conditional if @kind == :before || @kind == :around if @kind == :before # if condition # before_save :filter_name, :if => :condition # filter_name # end filter = <<-RUBY_EVAL unless halted result = #{@filter} halted = (#{terminator}) end RUBY_EVAL [@compiled_options[0], filter, @compiled_options[1]].compact.join("\n") else # Compile around filters with conditions into proxy methods # that contain the conditions. # # For `around_save :filter_name, :if => :condition': # # def _conditional_callback_save_17 # if condition # filter_name do # yield self # end # else # yield self # end # end name = "_conditional_callback_#{@kind}_#{next_id}" txt, line = <<-RUBY_EVAL, __LINE__ + 1 def #{name}(halted) #{@compiled_options[0] || "if true"} && !halted #{@filter} do yield self end else yield self end end RUBY_EVAL @klass.class_eval(txt, __FILE__, line) "#{name}(halted) do" end end end |