Class: Jogger
- Inherits:
-
Object
- Object
- Jogger
- Defined in:
- lib/jogger.rb
Overview
Allows to formulate traversals by using predefined named traversals. Also allows for method chaining.
All named traversals are defined in PacerTraversal::NamedTraversals and new ones can be added using add_traversal.
Instances have a @current_traversal variable that is updated with each chained call of #traverse.
Beware, it also uses method missing to delegate unknown methods to the current traversal. So after you’re done chaining things, you can do more stuff with it after you’re done, e.g. call count on it:
t = Jogger.new(some_node)
t.traverse(:some).traverse(:where).count
So everything except for #traverse is called on the
Class Method Summary collapse
-
.traverse(traversal_base, named_traversal, opts = nil) ⇒ Object
Runs a named traversal on a given traversal.
Instance Method Summary collapse
-
#initialize(initial_node = nil) ⇒ Jogger
constructor
A new instance of Jogger.
-
#method_missing(method, *args, &block) ⇒ Jogger
Runs the traversal with the same name as the called method on the current traversal and replaces the current traversal (== the state) with the results of the named traversal.
-
#result ⇒ Hash
The current state of the traversal as returned by pacer.
Constructor Details
#initialize(initial_node = nil) ⇒ Jogger
Returns a new instance of Jogger.
25 26 27 |
# File 'lib/jogger.rb', line 25 def initialize(initial_node = nil) @current_traversal = initial_node end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Jogger
Runs the traversal with the same name as the called method on the current traversal and replaces the current traversal (== the state) with the results of the named traversal.
If you call a method that is not a named traversal, the method call is delegated to the @current traversal. Still, this will return self. This is useful for more traversals after named routes.
#traverse calls
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jogger.rb', line 44 def method_missing(method, *args, &block) begin traversal_args = [method, args].flatten.compact @current_traversal = Jogger.traverse(@current_traversal, *traversal_args) rescue ArgumentError begin @current_traversal = @current_traversal.send(method, *args, &block) rescue NoMethodError raise "Unknown traversal #{method}" end end self end |
Class Method Details
.traverse(traversal_base, named_traversal, opts = nil) ⇒ Object
Runs a named traversal on a given traversal. For example, you could give it a start node as traversal base and a traversal named :go_to_all_subscribers to go to all subscribers from that node.
68 69 70 71 72 |
# File 'lib/jogger.rb', line 68 def self.traverse(traversal_base, named_traversal, opts = nil) raise ArgumentError, "Unknown traversal #{named_traversal}" unless valid_traversal?(named_traversal) args = [named_traversal, traversal_base] + [opts].compact Jogger::NamedTraversals.send(*args) end |
Instance Method Details
#result ⇒ Hash
Returns The current state of the traversal as returned by pacer.
30 31 32 |
# File 'lib/jogger.rb', line 30 def result @current_traversal end |