Module: Crystal::Callbacks::ClassMethods

Defined in:
lib/crystal/support/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#set_callback(callback_name, type, *executor_or_options, &block) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/crystal/support/callbacks.rb', line 127

def set_callback callback_name, type, *executor_or_options, &block   
  # parsing arguments
  type, callback_name = type.to_s, callback_name.to_s
  opt = executor_or_options.extract_options!        
  opt = opt.symbolize_keys
  "You can't provide both method name and block for filter!" if block and !executor_or_options.empty?
  executor = block || executor_or_options.first

  type.must_be.in %w{before around after}
  executor.must_be.defined
  
  # creating callback
  callback = case type
  when 'before' then BeforeCallback.new
  when 'around' then AroundCallback.new
  when 'after' then AfterCallback.new
  end            

  callback.executor = executor
  callback.terminator = opt.delete :terminator
  callback.conditions = opt

  callbacks[callback_name] ||= []
  callbacks[callback_name] << callback
end