Class: Factree::Facts
- Inherits:
-
Object
- Object
- Factree::Facts
- Extended by:
- Forwardable
- Defined in:
- lib/factree/facts.rb
Constant Summary collapse
- MISSING_FACTS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
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.
"Attempted to read missing facts from a Factree::Facts instance"
Class Method Summary collapse
- .catch_missing_facts ⇒ Object private
- .coerce(source) ⇒ Object
- .throw_missing_facts ⇒ Object private
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](fact_name) ⇒ Object
Gets the value of a fact.
-
#initialize(**hash) ⇒ Facts
constructor
A new instance of Facts.
-
#known?(fact_name) ⇒ Boolean
Checks to see if a fact is present.
-
#peek(fact_name) ⇒ Object
Gets the value of a fact, if present, without #requireing the fact.
-
#require(*fact_names) ⇒ void
Requires that certain facts are present in order to proceed with the decision.
Constructor Details
#initialize(**hash) ⇒ Facts
Returns a new instance of Facts.
15 16 17 18 |
# File 'lib/factree/facts.rb', line 15 def initialize(**hash) @hash = hash.freeze freeze end |
Class Method Details
.catch_missing_facts ⇒ Object
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.
60 61 62 63 64 |
# File 'lib/factree/facts.rb', line 60 def self.catch_missing_facts catch(MISSING_FACTS) do yield end end |
.coerce(source) ⇒ Object
10 11 12 13 |
# File 'lib/factree/facts.rb', line 10 def self.coerce(source) return source if source.is_a? self new(**source) end |
.throw_missing_facts ⇒ Object
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.
55 56 57 |
# File 'lib/factree/facts.rb', line 55 def self.throw_missing_facts throw MISSING_FACTS end |
Instance Method Details
#==(other) ⇒ Object
50 51 52 |
# File 'lib/factree/facts.rb', line 50 def ==(other) self.to_h == other.to_h end |
#[](fact_name) ⇒ Object
Gets the value of a fact. This also #requires the fact.
40 41 42 43 |
# File 'lib/factree/facts.rb', line 40 def [](fact_name) self.require fact_name peek fact_name end |
#known?(fact_name) ⇒ Boolean
Checks to see if a fact is present.
24 25 26 |
# File 'lib/factree/facts.rb', line 24 def known?(fact_name) @hash.has_key? fact_name end |
#peek(fact_name) ⇒ Object
Gets the value of a fact, if present, without #requireing the fact.
46 47 48 |
# File 'lib/factree/facts.rb', line 46 def peek(fact_name) @hash[fact_name] end |
#require(*fact_names) ⇒ void
This method returns an undefined value.
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.
32 33 34 |
# File 'lib/factree/facts.rb', line 32 def require(*fact_names) self.class.throw_missing_facts unless fact_names.all? { |name| known? name } end |