class Factree::Facts

Constants

MISSING_FACTS

Kernel#catch uses object ID to match thrown values. This gives us a unique ID and a readable message in case it's thrown somewhere it's not expected.

@api private

Public Class Methods

catch_missing_facts() { || ... } click to toggle source

@api private

# File lib/factree/facts.rb, line 60
def self.catch_missing_facts
  catch(MISSING_FACTS) do
    yield
  end
end
coerce(source) click to toggle source
# File lib/factree/facts.rb, line 10
def self.coerce(source)
  return source if source.is_a? self
  new(**source)
end
new(**hash) click to toggle source
# File lib/factree/facts.rb, line 15
def initialize(**hash)
  @hash = hash.freeze
  freeze
end
throw_missing_facts() click to toggle source

@api private

# File lib/factree/facts.rb, line 55
def self.throw_missing_facts
  throw MISSING_FACTS
end

Public Instance Methods

==(other) click to toggle source
# File lib/factree/facts.rb, line 50
def ==(other)
  self.to_h == other.to_h
end
[](fact_name) click to toggle source

Gets the value of a fact. This also {#require}s the fact.

@param [Symbol] fact_name @return [Object]

# File lib/factree/facts.rb, line 40
def [](fact_name)
  self.require fact_name
  peek fact_name
end
known?(fact_name) click to toggle source

Checks to see if a fact is present.

@param [Symbol] fact_name @return [Boolean]

# File lib/factree/facts.rb, line 24
def known?(fact_name)
  @hash.has_key? fact_name
end
peek(fact_name) click to toggle source

Gets the value of a fact, if present, without {#require}ing the fact.

# File lib/factree/facts.rb, line 46
def peek(fact_name)
  @hash[fact_name]
end
require(*fact_names) click to toggle source

Requires that certain facts are present in order to proceed with the decision. If any of the facts are missing, the path will stop here.

@param [Array<Symbol>] fact_names Names of facts to require @return [void]

# File lib/factree/facts.rb, line 32
def require(*fact_names)
  self.class.throw_missing_facts unless fact_names.all? { |name| known? name }
end