Class: Factree::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/factree/path.rb

Overview

Paths record useful information about an attempt to reach a Conclusion for a decision proc.

A path may or may not actually reach a conclusion. If it does, it will be #complete? and return the #conclusion value. If it doesn’t, then you can get the names of all of the facts needed to get past the next decision step from #required_facts.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(required_facts = [], conclusion = nil) ⇒ Path

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Want to create a path? Use DSL#find_path instead.



12
13
14
15
16
# File 'lib/factree/path.rb', line 12

def initialize(required_facts=[], conclusion=nil)
  @required_facts = required_facts.to_a.uniq
  @conclusion = conclusion
  freeze
end

Instance Attribute Details

#required_factsArray<Symbol> (readonly)

A list of the facts that were required to get this far, plus any facts needed to make further progress. If the path is not complete, then the facts in this list are sufficient to progress past this point.

Returns:

  • (Array<Symbol>)

    A list of fact names in the order they’re required in the tree



34
35
36
# File 'lib/factree/path.rb', line 34

def required_facts
  @required_facts
end

Instance Method Details

#==(other) ⇒ Object



36
37
38
39
40
# File 'lib/factree/path.rb', line 36

def ==(other)
  self.class == other.class &&
    @required_facts == other.instance_variable_get(:@required_facts) &&
    @conclusion == other.instance_variable_get(:@conclusion)
end

#complete?Boolean

A path is #complete? if it has reached a conclusion.

Returns:

  • (Boolean)


19
20
21
# File 'lib/factree/path.rb', line 19

def complete?
  !@conclusion.nil?
end

#conclusionObject

Returns the conclusion value if this path is complete.



24
25
26
27
28
29
# File 'lib/factree/path.rb', line 24

def conclusion
  # We don't want to return nil to indicate a missing conclusion, since that could confused for a nil conclusion with a nil value.
  raise Factree::NoConclusionError, "Attempted to get conclusion from incomplete path" unless complete?

  @conclusion.value
end