Class: ActiveSupport::Callbacks::Callback
- Defined in:
- lib/active_support/callbacks.rb
Constant Summary collapse
- @@_callback_sequence =
0
Instance Attribute Summary collapse
-
#chain ⇒ Object
Returns the value of attribute chain.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#options ⇒ Object
Returns the value of attribute options.
-
#per_key ⇒ Object
Returns the value of attribute per_key.
-
#raw_filter ⇒ Object
Returns the value of attribute raw_filter.
Instance Method Summary collapse
- #_compile_per_key_options ⇒ Object
- #_update_filter(filter_options, new_options) ⇒ Object
- #clone(chain, klass) ⇒ Object
-
#end(key = nil, object = nil) ⇒ Object
This will supply contents for around and after filters, but not before filters (for the backward pass).
-
#initialize(chain, filter, kind, options, klass) ⇒ Callback
constructor
A new instance of Callback.
- #matches?(_kind, _filter) ⇒ Boolean
- #name ⇒ Object
- #next_id ⇒ Object
- #normalize_options!(options) ⇒ Object
- #recompile!(_options, _per_key) ⇒ Object
-
#start(key = nil, object = nil) ⇒ Object
This will supply contents for before and around filters, and no contents for after filters (for the forward pass).
Constructor Details
#initialize(chain, filter, kind, options, klass) ⇒ Callback
Returns a new instance of Callback.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/active_support/callbacks.rb', line 96 def initialize(chain, filter, kind, , klass) @chain, @kind, @klass = chain, kind, klass () @per_key = .delete(:per_key) @raw_filter, = filter, @filter = _compile_filter(filter) = () @callback_id = next_id end |
Instance Attribute Details
#chain ⇒ Object
Returns the value of attribute chain.
94 95 96 |
# File 'lib/active_support/callbacks.rb', line 94 def chain @chain end |
#filter ⇒ Object
Returns the value of attribute filter.
94 95 96 |
# File 'lib/active_support/callbacks.rb', line 94 def filter @filter end |
#kind ⇒ Object
Returns the value of attribute kind.
94 95 96 |
# File 'lib/active_support/callbacks.rb', line 94 def kind @kind end |
#klass ⇒ Object
Returns the value of attribute klass.
94 95 96 |
# File 'lib/active_support/callbacks.rb', line 94 def klass @klass end |
#options ⇒ Object
Returns the value of attribute options.
94 95 96 |
# File 'lib/active_support/callbacks.rb', line 94 def end |
#per_key ⇒ Object
Returns the value of attribute per_key.
94 95 96 |
# File 'lib/active_support/callbacks.rb', line 94 def per_key @per_key end |
#raw_filter ⇒ Object
Returns the value of attribute raw_filter.
94 95 96 |
# File 'lib/active_support/callbacks.rb', line 94 def raw_filter @raw_filter end |
Instance Method Details
#_compile_per_key_options ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/active_support/callbacks.rb', line 158 def = (@per_key) @klass.class_eval " def _one_time_conditions_valid_\#{@callback_id}?\n true \#{key_options[0]}\n end\n RUBY_EVAL\nend\n", __FILE__, __LINE__ + 1 |
#_update_filter(filter_options, new_options) ⇒ Object
143 144 145 146 |
# File 'lib/active_support/callbacks.rb', line 143 def _update_filter(, ) [:if].push([:unless]) if .key?(:unless) [:unless].push([:if]) if .key?(:if) end |
#clone(chain, klass) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/active_support/callbacks.rb', line 109 def clone(chain, klass) obj = super() obj.chain = chain obj.klass = klass obj.per_key = @per_key.dup obj. = .dup obj.per_key[:if] = @per_key[:if].dup obj.per_key[:unless] = @per_key[:unless].dup obj.[:if] = [:if].dup obj.[:unless] = [:unless].dup obj end |
#end(key = nil, object = nil) ⇒ Object
This will supply contents for around and after filters, but not before filters (for the backward pass).
224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/active_support/callbacks.rb', line 224 def end(key=nil, object=nil) 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 [[0], @filter, [1]].compact.join("\n") else "end" end end end |
#matches?(_kind, _filter) ⇒ Boolean
139 140 141 |
# File 'lib/active_support/callbacks.rb', line 139 def matches?(_kind, _filter) @kind == _kind && @filter == _filter end |
#name ⇒ Object
131 132 133 |
# File 'lib/active_support/callbacks.rb', line 131 def name chain.name end |
#next_id ⇒ Object
135 136 137 |
# File 'lib/active_support/callbacks.rb', line 135 def next_id @@_callback_sequence += 1 end |
#normalize_options!(options) ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/active_support/callbacks.rb', line 122 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 |
#recompile!(_options, _per_key) ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/active_support/callbacks.rb', line 148 def recompile!(, _per_key) _update_filter(self., ) _update_filter(self.per_key, _per_key) @callback_id = next_id @filter = _compile_filter(@raw_filter) = () end |
#start(key = nil, object = nil) ⇒ Object
This will supply contents for before and around filters, and no contents for after filters (for the forward pass).
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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 |
# File 'lib/active_support/callbacks.rb', line 170 def start(key=nil, object=nil) return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?") # 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 = " unless halted\n result = \#{@filter}\n halted = (\#{chain.config[:terminator]})\n end\n RUBY_EVAL\n\n [@compiled_options[0], filter, @compiled_options[1]].compact.join(\"\\n\")\n else\n # Compile around filters with conditions into proxy methods\n # that contain the conditions.\n #\n # For `around_save :filter_name, :if => :condition':\n #\n # def _conditional_callback_save_17\n # if condition\n # filter_name do\n # yield self\n # end\n # else\n # yield self\n # end\n # end\n #\n name = \"_conditional_callback_\#{@kind}_\#{next_id}\"\n @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1\n def \#{name}(halted)\n \#{@compiled_options[0] || \"if true\"} && !halted\n \#{@filter} do\n yield self\n end\n else\n yield self\n end\n end\n RUBY_EVAL\n \"\#{name}(halted) do\"\n end\n end\nend\n" |