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 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

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



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

Returns:

  • (Jogger)

    Returns itself so you can chain multiple



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.

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:

  • (ArgumentError)


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

#resultHash

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

Returns:

  • (Hash)

    The current state of the traversal as returned by pacer



30
31
32
# File 'lib/jogger.rb', line 30

def result
  @current_traversal
end