Class: MotionSupport::Callbacks::Callback

Inherits:
Object
  • Object
show all
Defined in:
motion/callbacks.rb

Overview

:nodoc:#

Constant Summary collapse

@@_callback_sequence =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chain, filter, kind, options, klass) ⇒ Callback

Returns a new instance of Callback.



87
88
89
90
91
92
93
94
# File 'motion/callbacks.rb', line 87

def initialize(chain, filter, kind, options, klass)
  @chain, @kind, @klass = chain, kind, klass
  normalize_options!(options)

  @raw_filter, @options = filter, options
  @filter               = _compile_filter(filter)
  recompile_options!
end

Instance Attribute Details

#chainObject

Returns the value of attribute chain.



85
86
87
# File 'motion/callbacks.rb', line 85

def chain
  @chain
end

#filterObject

Returns the value of attribute filter.



85
86
87
# File 'motion/callbacks.rb', line 85

def filter
  @filter
end

#kindObject

Returns the value of attribute kind.



85
86
87
# File 'motion/callbacks.rb', line 85

def kind
  @kind
end

#klassObject

Returns the value of attribute klass.



85
86
87
# File 'motion/callbacks.rb', line 85

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



85
86
87
# File 'motion/callbacks.rb', line 85

def options
  @options
end

#raw_filterObject

Returns the value of attribute raw_filter.



85
86
87
# File 'motion/callbacks.rb', line 85

def raw_filter
  @raw_filter
end

Instance Method Details

#_update_filter(filter_options, new_options) ⇒ Object



127
128
129
130
# File 'motion/callbacks.rb', line 127

def _update_filter(filter_options, new_options)
  filter_options[:if].concat(Array(new_options[:unless])) if new_options.key?(:unless)
  filter_options[:unless].concat(Array(new_options[:if])) if new_options.key?(:if)
end

#apply(code) ⇒ Object

Wraps code with filter



139
140
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
# File 'motion/callbacks.rb', line 139

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



96
97
98
99
100
101
102
103
104
# File 'motion/callbacks.rb', line 96

def clone(chain, klass)
  obj                  = super()
  obj.chain            = chain
  obj.klass            = klass
  obj.options          = @options.dup
  obj.options[:if]     = @options[:if].dup
  obj.options[:unless] = @options[:unless].dup
  obj
end

#duplicates?(other) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'motion/callbacks.rb', line 123

def duplicates?(other)
  matches?(other.kind, other.raw_filter)
end

#matches?(_kind, _filter) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'motion/callbacks.rb', line 119

def matches?(_kind, _filter)
  @kind == _kind && @raw_filter == _filter
end

#nameObject



111
112
113
# File 'motion/callbacks.rb', line 111

def name
  chain.name
end

#next_idObject



115
116
117
# File 'motion/callbacks.rb', line 115

def next_id
  @@_callback_sequence += 1
end

#normalize_options!(options) ⇒ Object



106
107
108
109
# File 'motion/callbacks.rb', line 106

def normalize_options!(options)
  options[:if] = Array(options[:if])
  options[:unless] = Array(options[:unless])
end

#recompile!(_options) ⇒ Object



132
133
134
135
136
# File 'motion/callbacks.rb', line 132

def recompile!(_options)
  _update_filter(self.options, _options)

  recompile_options!
end