module Orator::Base::ClassMethods

Public Instance Methods

after(method = nil, &block) click to toggle source

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.

@example

after do; something; end

@example

after :some_method

@param method [Symbol] the method that will be called for the after

callback.

@yields []

# File lib/orator/base.rb, line 219
def after(method = nil, &block)
  after_list << (method or block)
end
after_list() click to toggle source

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

@return [Set<Symbol, Block>] the after callback list.

# File lib/orator/base.rb, line 227
def after_list
  @after_list ||= Set.new
end
before(method = nil, &block) click to toggle source

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.

@example

before do; something; end

@example

before :some_method

@param method [Symbol] the method that will be called for the before

callback.

@yields []

# File lib/orator/base.rb, line 196
def before(method = nil, &block)
  before_list << (method or block)
end
before_list() click to toggle source

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

@return [Set<Symbol, Block>] the before callback list.

# File lib/orator/base.rb, line 204
def before_list
  @before_list ||= Set.new
end
event_list() click to toggle source

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

@return [Hash] the event list.

# File lib/orator/base.rb, line 171
def event_list
  @event_list ||= {}
end
inherited(klass) click to toggle source

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.

@param klass [Base]

# 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) click to toggle source

This registers a method for this orator.

@param event [Symbol] the event to register it to. @return [void]

# 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) click to toggle source

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

@param value [String, nil] the new name. @return [String] the orator name.

# 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) click to toggle source

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

@param event_handler [EventHandler]

# 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