Module: Factree::DSL

Included in:
Factree
Defined in:
lib/factree/dsl.rb

Overview

Readable shortcuts to common functions.

Instance Method Summary collapse

Instance Method Details

#conclusion(value = nil) ⇒ Conclusion

Creates a Conclusion to return from a decision proc.

The conclusion has an associated value (can be anything.)

Parameters:

  • value (Object) (defaults to: nil)

    Any value, including nil

Returns:



9
10
11
# File 'lib/factree/dsl.rb', line 9

def conclusion(value=nil)
  Factree::Conclusion.new(value)
end

#decide_between_alternatives(facts, *decide_procs) ⇒ Decision

A tool for composing lists of decision procs to be tried one after another until a conclusion is reached. When a proc returns nil instead of a Conclusion, the next proc in decide_procs is used instead.

If the last decision proc is reached and it returns nil, then this method returns nil.

Parameters:

  • facts (Facts)

    The facts to pass through to the decision procs.

  • decide_procs (Array<#call>)

    A decision proc followed by alternative procs.

Returns:

  • (Decision)

    A decision tree that checks the alternatives in order to reach a conclusion.



37
38
39
# File 'lib/factree/dsl.rb', line 37

def decide_between_alternatives(facts, *decide_procs)
  Factree::Aggregate.alternatives(facts, *decide_procs)
end

#find_path(**facts, &decide) ⇒ Path

Navigates as far as possible through a decision tree with the given set of facts.

The path will stop when either

  • a conclusion is returned, or

  • there aren’t enough facts to make the decision.

Errors

If a decision function fails to return a Conclusion, an InvalidConclusionError will be raised.

Parameters:

  • facts (Hash)

    The set of facts used to make decisions

  • decide (Proc)

    The decision proc. Takes a set of Facts and returns a Conclusion.

Returns:

  • (Path)

    Information about the path followed through the tree



26
27
28
# File 'lib/factree/dsl.rb', line 26

def find_path(**facts, &decide)
  Factree::Pathfinder.find(facts, &decide)
end