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 NamedTraversals, so that’s where you should define your own.
Instances have a @current_traversal variable that is updated with each call. Beware: Jogger 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, e.g. call count on it:
t = Jogger.new(some_node)
t.go.some.where.count
Everything except for #result is called on the ‘@current_traversal`, so what you really want to do to get the count of your traversal would be
t = Jogger.new(some_node)
t.go.some.where.result.count
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.
30 31 32 |
# File 'lib/jogger.rb', line 30 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.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jogger.rb', line 49 def method_missing(method, *args, &block) begin traversal_args = [method, args].flatten.compact @current_traversal = Jogger.traverse(@current_traversal, method, args) rescue UnknownTraversalError => a begin @current_traversal = @current_traversal.send(method, *args, &block) rescue NoMethodError => m raise "Unknown traversal #{method}. From (#{a}) via (#{m}) (method_missing rocks)" 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.
73 74 75 76 77 |
# File 'lib/jogger.rb', line 73 def self.traverse(traversal_base, named_traversal, opts = nil) raise UnknownTraversalError, "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.
35 36 37 |
# File 'lib/jogger.rb', line 35 def result @current_traversal end |