Class: Jogger

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(initial_node = nil) ⇒ Jogger

Returns a new instance of Jogger.

Parameters:

  • initial_node (Object) (defaults to: nil)

    A node to start out with. Can also be the result of a prior traversal you did outside of this class



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.

Returns:

  • (Jogger)

    Returns itself so you can chain multiple calls just like you would do with pacer



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.

Parameters:

  • traversal_base (Object)

    The result of a previous traversal or a pacer node you want to begin your traversal with

  • named_traversal (Symbol)

    The name of the predefined traversal you want to run on the traversal_base

  • opts (Object) (defaults to: nil)

    Whatever you want to pass to the named traverser

Returns:

  • (Object)

    The result of the traversal.

Raises:



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

#resultHash

Returns The current state of the traversal as returned by pacer.

Returns:

  • (Hash)

    The current state of the traversal as returned by pacer



35
36
37
# File 'lib/jogger.rb', line 35

def result
  @current_traversal
end