Class: Orator::Base Abstract

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/orator/base.rb

Overview

This class is abstract.

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.

Direct Known Subclasses

ApplicationOrator, ExampleOrator

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ClassMethods

after, after_list, before, before_list, event_list, inherited, on, orator_name, register_with

Constructor Details

#initialize(context) ⇒ Base

Initialize the orator with the context.

Parameters:

  • context (Object)


13
14
15
# File 'lib/orator/base.rb', line 13

def initialize(context)
  @__context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

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

Returns:

  • (Object)


77
78
79
80
81
# 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

Instance Method Details

#__event_listHash

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

Returns:

  • (Hash)

See Also:

  • Orator::Base.[[::event_list]


63
64
65
# File 'lib/orator/base.rb', line 63

def __event_list
  self.class.event_list
end

#__method_exists?(method) ⇒ Set<Symbol, Proc>?

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

Returns:

  • (Set<Symbol, Proc>, nil)


55
56
57
# File 'lib/orator/base.rb', line 55

def __method_exists?(method)
  __event_list[method]
end

#__trigger(event, *args) ⇒ Array<Object>?

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

Parameters:

  • event (Symbol, String)

    the event to trigger.

Returns:

  • (Array<Object>, nil)

    the values of the trigger, or nil if the event was prevented.

Raises:

  • (NoMethodError)

    if the event isn’t defined on the orator.



24
25
26
27
28
29
30
31
32
33
34
35
# 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

#eventString

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

Returns:

  • (String)


48
49
50
# File 'lib/orator/base.rb', line 48

def event
  @event[1]
end

#full_eventString

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

Returns:

  • (String)


41
42
43
# File 'lib/orator/base.rb', line 41

def full_event
  @event.join('.')
end

#prevent_eventObject

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



69
70
71
# File 'lib/orator/base.rb', line 69

def prevent_event
  @__prevent_event = true
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

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

Returns:

  • (Boolean)


86
87
88
# 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) ⇒ Object

This forwards #send on to the context.

Returns:

  • (Object)


93
94
95
# File 'lib/orator/base.rb', line 93

def send(*args, &block)
  @__context.send(*args, &block)
end