Module: Factree::FactSource
- Defined in:
- lib/factree/fact_source.rb
Overview
Mixin to help define fact source classes.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- UndefinedFactError =
Class.new(StandardError)
- UnknownFactError =
Class.new(StandardError)
- UNKNOWN_FACT =
"Attempted to determine the value of a fact that is unknown".freeze
Class Method Summary collapse
- .included(base) ⇒ Object
-
.to_combined_h(*sources) ⇒ Object
Takes several FactSources and returns a single hash containing all of their facts mixed together.
Instance Method Summary collapse
-
#[](fact_name) ⇒ Object
Returns the value of the fact, or nil if the value is unknown.
- #ensure_defined(fact_name) ⇒ Object private
-
#fetch(fact_name, &block) ⇒ Object
Returns the value of the fact.
- #fetch_unknown(fact_name) ⇒ Object private
-
#known?(fact_name) ⇒ Boolean
Checks to see if the value of the fact is known.
-
#to_h ⇒ Object
A hash mapping all of the known fact names to values.
-
#unknown ⇒ Object
Calling this method in a fact proc will signal that the fact’s value is unknown.
Class Method Details
.included(base) ⇒ Object
7 8 9 |
# File 'lib/factree/fact_source.rb', line 7 def self.included(base) base.extend(ClassMethods) end |
.to_combined_h(*sources) ⇒ Object
Takes several FactSources and returns a single hash containing all of their facts mixed together.
94 95 96 |
# File 'lib/factree/fact_source.rb', line 94 def self.to_combined_h(*sources) sources.map(&:to_h).inject({}, &:merge) end |
Instance Method Details
#[](fact_name) ⇒ Object
Returns the value of the fact, or nil if the value is unknown
48 49 50 |
# File 'lib/factree/fact_source.rb', line 48 def [](fact_name) fetch(fact_name) { nil } end |
#ensure_defined(fact_name) ⇒ 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.
70 71 72 73 74 |
# File 'lib/factree/fact_source.rb', line 70 def ensure_defined(fact_name) unless self.class.defined? fact_name raise UndefinedFactError, "undefined fact referenced: #{fact_name}" end end |
#fetch(fact_name, &block) ⇒ Object
Returns the value of the fact.
If the value is unknown, then the block will be called with the name of the fact. If no block is supplied, then an UnknownFactError will be raised.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/factree/fact_source.rb', line 55 def fetch(fact_name, &block) ensure_defined fact_name fact_proc = self.class.fact_procs[fact_name] fact_value = nil fact_known = catch(UNKNOWN_FACT) do fact_value = instance_eval(&fact_proc) true end return fact_value if fact_known fetch_unknown(fact_name, &block) end |
#fetch_unknown(fact_name) ⇒ 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.
77 78 79 80 81 |
# File 'lib/factree/fact_source.rb', line 77 def fetch_unknown(fact_name) return yield(fact_name) if block_given? raise UnknownFactError, "unknown fact: #{fact_name}" end |
#known?(fact_name) ⇒ Boolean
Checks to see if the value of the fact is known.
42 43 44 45 |
# File 'lib/factree/fact_source.rb', line 42 def known?(fact_name) fetch(fact_name) { return false } true end |
#to_h ⇒ Object
A hash mapping all of the known fact names to values.
84 85 86 87 88 89 90 91 |
# File 'lib/factree/fact_source.rb', line 84 def to_h self.class.fact_procs.flat_map { |fact_name, _fact_proc| fact_known = true fact_value = fetch(fact_name) { fact_known = false } fact_known ? [[fact_name, fact_value]] : [] }.to_h end |
#unknown ⇒ Object
Calling this method in a fact proc will signal that the fact’s value is unknown.
99 100 101 |
# File 'lib/factree/fact_source.rb', line 99 def unknown throw UNKNOWN_FACT end |