Module: Orator::Base::ClassMethods

Included in:
Orator::Base
Defined in:
lib/orator/base.rb

Instance Method Summary collapse

Instance Method Details

#after(method = nil, &block) ⇒ Object

This handles the after callbacks that our operator needs. It can accept a method or a block, and given the two, chooses the method over the block.

Examples:

after do; something; end
after :some_method

Parameters:

  • method (Symbol) (defaults to: nil)

    the method that will be called for the after callback.



219
220
221
# File 'lib/orator/base.rb', line 219

def after(method = nil, &block)
  after_list << (method or block)
end

#after_listSet<Symbol, Block>

This sets up the after list on the first call, and returns the list on the first and subsequent calls.

Returns:

  • (Set<Symbol, Block>)

    the after callback list.



227
228
229
# File 'lib/orator/base.rb', line 227

def after_list
  @after_list ||= Set.new
end

#before(method = nil, &block) ⇒ Object

This handles the before callbacks that our orator needs. It can accept a method or a block, and given the two, chooses the method over the block.

Examples:

before do; something; end
before :some_method

Parameters:

  • method (Symbol) (defaults to: nil)

    the method that will be called for the before callback.



196
197
198
# File 'lib/orator/base.rb', line 196

def before(method = nil, &block)
  before_list << (method or block)
end

#before_listSet<Symbol, Block>

This sets up the before list on the first call, and returns the list on the first and subsequent calls.

Returns:

  • (Set<Symbol, Block>)

    the before callback list.



204
205
206
# File 'lib/orator/base.rb', line 204

def before_list
  @before_list ||= Set.new
end

#event_listHash

The event matchings. The keys are the event name, the values can be an array of mappings or a the mapping itself.

Returns:

  • (Hash)

    the event list.



171
172
173
# File 'lib/orator/base.rb', line 171

def event_list
  @event_list ||= {}
end

#inherited(klass) ⇒ Object

This is called when this class is inherited from. We use this to make sure that each subclass inherits the event list as well as the before and after methods.

Parameters:



236
237
238
239
240
# File 'lib/orator/base.rb', line 236

def inherited(klass)
  klass.instance_variable_set(:@event_list,  event_list.dup )
  klass.instance_variable_set(:@before_list, before_list.dup)
  klass.instance_variable_set(:@after_list,  after_list.dup )
end

#on(event, method = nil, &block) ⇒ void

This method returns an undefined value.

This registers a method for this orator.

Parameters:

  • event (Symbol)

    the event to register it to.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/orator/base.rb', line 152

def on(event, method = nil, &block)
  if event.is_a? Hash
    method = event.values.first
    event  = event.keys.first
  end
  
  event_list[event.to_s] ||= []
  
  if block_given?
    event_list[event.to_s] << block       
  else
    event_list[event.to_s] << (method || event).to_sym
  end
end

#orator_name(value = nil) ⇒ String

This manages the orator’s name. If an argument is passed, the name is set to that. Otherwise, it returns the orator’s name.

Parameters:

  • value (String, nil) (defaults to: nil)

    the new name.

Returns:

  • (String)

    the orator name.



140
141
142
143
144
145
146
# File 'lib/orator/base.rb', line 140

def orator_name(value = nil)
  if value
    @_orator_name = value
  end

  @_orator_name
end

#register_with(event_handler) ⇒ Object

This registers this orator with the event handler by telling it what methods it responds to.

Parameters:



179
180
181
182
183
# File 'lib/orator/base.rb', line 179

def register_with(event_handler)
  event_list.keys.each do |event|
    event_handler.on("#{self.orator_name}.#{event}", self)
  end
end