class Orator::Base

A base for the orators to split off of. It handles contexts and running methods. Most of the methods defined in this class are prefixed with two dashes to prevent polluting the namespace.

@abstract

Public Class Methods

new(context) click to toggle source

Initialize the orator with the context.

@param context [Object]

# File lib/orator/base.rb, line 13
def initialize(context)
  @__context = context
end

Public Instance Methods

__event_list() click to toggle source

A map of the events that determine how this class will respond.

@return [Hash] @see [::event_list]

# File lib/orator/base.rb, line 63
def __event_list
  self.class.event_list
end
__method_exists?(method) click to toggle source

Checks the event list to see if this class responds to it.

@return [Set<Symbol, Proc>, nil]

# File lib/orator/base.rb, line 55
def __method_exists?(method)
  __event_list[method]
end
__trigger(event, *args) click to toggle source

This triggers an event if it’s defined on this orator. If it is not defined, it raises an error.

@param event [Symbol, String] the event to trigger. @raise [NoMethodError] if the event isn’t defined on the orator. @return [Array<Object>, nil] the values of the trigger, or nil if the

event was prevented.
# File lib/orator/base.rb, line 24
def __trigger(event, *args)
  orator = event[0..event.index('.')-1]
  method_name = event[event.index('.')+1..-1]
  __check_event(orator, method_name)

  @event = [orator, method_name]
  __run_callbacks(:before, args)
  out = __run_event(method_name, args) unless @__prevent_event
  __run_callbacks(:after, args)

  out
end
event() click to toggle source

Returns the current event that the orator is handling in ‘event` form.

@return [String]

# File lib/orator/base.rb, line 48
def event
  @event[1]
end
full_event() click to toggle source

Returns the current event that the orator is handling in ‘orator.event` form.

@return [String]

# File lib/orator/base.rb, line 41
def full_event
  @event.join('.')
end
method_missing(method, *args, &block) click to toggle source

Handles missing methods by delegating them to the context. Should never be called directly.

@return [Object]

Calls superclass method
# File lib/orator/base.rb, line 77
def method_missing(method, *args, &block)
  super unless respond_to_missing?(method)

  @__context.public_send(method, *args, &block)
end
prevent_event() click to toggle source

This tells the orator to not execute the events in the class. This should only be called in a ‘before` block.

# File lib/orator/base.rb, line 69
def prevent_event
  @__prevent_event = true
end
respond_to_missing?(method, include_private = false) click to toggle source

Lets Ruby know that there are some undefined methods on this class.

@return [Boolean]

# File lib/orator/base.rb, line 86
def respond_to_missing?(method, include_private = false)
  @__context.respond_to?(method, include_private)
end
send(*args, &block) click to toggle source

This forwards {#send} on to the context.

@return [Object]

# File lib/orator/base.rb, line 93
def send(*args, &block)
  @__context.send(*args, &block)
end

Private Instance Methods

__check_event(orator, method) click to toggle source

Check the event, making sure that this orator can respond to it.

# File lib/orator/base.rb, line 110
def __check_event(orator, method)
  if orator != self.class.orator_name
    raise NoMethodError,
      "Event #{orator} does not match #{self.class.name}"
    return
  elsif !__method_exists?(method)
    raise NoMethodError,
      "Method #{method} does not exist on #{self.class.name}"
    return
  end
end
__run_callbacks(type, args) click to toggle source

Runs the callbacks.

# File lib/orator/base.rb, line 123
def __run_callbacks(type, args)
  self.class.send("#{type}_list").map do |cb|
    if cb.is_a? Symbol
      method(cb).call(*args)
    else
      instance_exec(*args, &cb)
    end
  end
end
__run_event(method_name, args) click to toggle source

Run the event.

# File lib/orator/base.rb, line 100
def __run_event(method_name, args)
  result = __event_list[method_name].map do |responder|
    responder = method(responder) if responder.is_a? Symbol
    instance_exec *args, &responder
  end

  result
end