Class: MotionSupport::Callbacks::Callback
Overview
:nodoc:#
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.
-
#raw_filter ⇒ Object
Returns the value of attribute raw_filter.
Instance Method Summary collapse
- #_update_filter(filter_options, new_options) ⇒ Object
-
#apply(code) ⇒ Object
Wraps code with filter.
- #clone(chain, klass) ⇒ Object
- #duplicates?(other) ⇒ Boolean
-
#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) ⇒ Object
Constructor Details
#initialize(chain, filter, kind, options, klass) ⇒ Callback
Returns a new instance of Callback
89 90 91 92 93 94 95 96 |
# File 'motion/callbacks.rb', line 89 def initialize(chain, filter, kind, , klass) @chain, @kind, @klass = chain, kind, klass () @raw_filter, @options = filter, @filter = _compile_filter(filter) end |
Instance Attribute Details
#chain ⇒ Object
Returns the value of attribute chain
87 88 89 |
# File 'motion/callbacks.rb', line 87 def chain @chain end |
#filter ⇒ Object
Returns the value of attribute filter
87 88 89 |
# File 'motion/callbacks.rb', line 87 def filter @filter end |
#kind ⇒ Object
Returns the value of attribute kind
87 88 89 |
# File 'motion/callbacks.rb', line 87 def kind @kind end |
#klass ⇒ Object
Returns the value of attribute klass
87 88 89 |
# File 'motion/callbacks.rb', line 87 def klass @klass end |
#options ⇒ Object
Returns the value of attribute options
87 88 89 |
# File 'motion/callbacks.rb', line 87 def @options end |
#raw_filter ⇒ Object
Returns the value of attribute raw_filter
87 88 89 |
# File 'motion/callbacks.rb', line 87 def raw_filter @raw_filter end |
Instance Method Details
#_update_filter(filter_options, new_options) ⇒ Object
129 130 131 132 |
# File 'motion/callbacks.rb', line 129 def _update_filter(, ) [:if].concat(Array([:unless])) if .key?(:unless) [:unless].concat(Array([:if])) if .key?(:if) end |
#apply(code) ⇒ Object
Wraps code with filter
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'motion/callbacks.rb', line 141 def apply(code) case @kind when :before lambda do |obj, value, halted| if !halted && @compiled_options.call(obj) result = @filter.call(obj) halted = chain.config[:terminator].call(result) if halted obj.halted_callback_hook(@raw_filter.inspect) end end code.call(obj, value, halted) end when :after lambda do |obj, value, halted| value, halted = *(code.call(obj, value, halted)) if (!chain.config[:skip_after_callbacks_if_terminated] || !halted) && @compiled_options.call(obj) @filter.call(obj) end [value, halted] end when :around lambda do |obj, value, halted| if @compiled_options.call(obj) && !halted @filter.call(obj) do value, halted = *(code.call(obj, value, halted)) value end else value, halted = *(code.call(obj, value, halted)) value end [value, halted] end end end |
#clone(chain, klass) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'motion/callbacks.rb', line 98 def clone(chain, klass) obj = super() obj.chain = chain obj.klass = klass obj. = @options.dup obj.[:if] = @options[:if].dup obj.[:unless] = @options[:unless].dup obj end |
#duplicates?(other) ⇒ Boolean
125 126 127 |
# File 'motion/callbacks.rb', line 125 def duplicates?(other) matches?(other.kind, other.raw_filter) end |
#matches?(_kind, _filter) ⇒ Boolean
121 122 123 |
# File 'motion/callbacks.rb', line 121 def matches?(_kind, _filter) @kind == _kind && @raw_filter == _filter end |
#name ⇒ Object
113 114 115 |
# File 'motion/callbacks.rb', line 113 def name chain.name end |
#next_id ⇒ Object
117 118 119 |
# File 'motion/callbacks.rb', line 117 def next_id @@_callback_sequence += 1 end |
#normalize_options!(options) ⇒ Object
108 109 110 111 |
# File 'motion/callbacks.rb', line 108 def () [:if] = Array([:if]) [:unless] = Array([:unless]) end |
#recompile!(_options) ⇒ Object
134 135 136 137 138 |
# File 'motion/callbacks.rb', line 134 def recompile!() _update_filter(self., ) end |