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.



214
215
216
# File 'lib/orator/base.rb', line 214

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.



222
223
224
# File 'lib/orator/base.rb', line 222

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.



191
192
193
# File 'lib/orator/base.rb', line 191

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.



199
200
201
# File 'lib/orator/base.rb', line 199

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.



166
167
168
# File 'lib/orator/base.rb', line 166

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:



231
232
233
234
235
# File 'lib/orator/base.rb', line 231

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
# File 'lib/orator/base.rb', line 152

def on(event, method = nil, &block)
  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:



174
175
176
177
178
# File 'lib/orator/base.rb', line 174

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