Class: FactChecker::Base
- Inherits:
-
Object
- Object
- FactChecker::Base
- Defined in:
- lib/fact_checker/base.rb
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
Returns the value of attribute dependencies.
-
#facts ⇒ Object
Returns the value of attribute facts.
-
#requirements ⇒ Object
Returns the value of attribute requirements.
Instance Method Summary collapse
- #accomplished_facts(context) ⇒ Object
-
#def_fact(*opt) ⇒ Object
Syntactic sugar, adds fact with its requirement and dependency.
-
#fact_accomplished?(context, fact) ⇒ Boolean
Checks if requirement and dependency for the fact are satisfied.
-
#fact_possible?(context, fact) ⇒ Boolean
Checks if dependency for the fact is satisfied.
-
#initialize(facts = nil, deps = nil, reqs = nil) ⇒ Base
constructor
A new instance of Base.
- #initialize_copy(orig) ⇒ Object
- #possible_facts(context) ⇒ Object
-
#requirement_satisfied_for?(context, fact) ⇒ Boolean
Checks if requirement for the fact is satisfied (no dependency checks here).
Constructor Details
#initialize(facts = nil, deps = nil, reqs = nil) ⇒ Base
Returns a new instance of Base.
7 8 9 10 11 |
# File 'lib/fact_checker/base.rb', line 7 def initialize(facts = nil, deps = nil, reqs = nil) @facts = facts || [] @dependencies = deps || {} @requirements = reqs || {} end |
Instance Attribute Details
#dependencies ⇒ Object
Returns the value of attribute dependencies.
5 6 7 |
# File 'lib/fact_checker/base.rb', line 5 def dependencies @dependencies end |
#facts ⇒ Object
Returns the value of attribute facts.
5 6 7 |
# File 'lib/fact_checker/base.rb', line 5 def facts @facts end |
#requirements ⇒ Object
Returns the value of attribute requirements.
5 6 7 |
# File 'lib/fact_checker/base.rb', line 5 def requirements @requirements end |
Instance Method Details
#accomplished_facts(context) ⇒ Object
20 21 22 |
# File 'lib/fact_checker/base.rb', line 20 def accomplished_facts(context) facts.select{ |fact| fact_accomplished?(context, fact) } end |
#def_fact(*opt) ⇒ Object
Syntactic sugar, adds fact with its requirement and dependency. Examples:
-
def_fact(:fact)
-
def_fact(:fact, :if => :requirement)
-
def_fact(:fact => :dependency)
-
def_fact(:fact => :dependency, :if => :requirement)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fact_checker/base.rb', line 55 def def_fact(*opt) raise ArgumentError, "wrong number of arguments (#{opt.size} for 2)" if opt.size > 2 raise ArgumentError, "wrong number of arguments (0 for 1)" if opt.size == 0 if opt[0].is_a?(Hash) raise ArgumentError, "wrong arguments (hash argument can only be the last one)" if opt.size > 1 hash = opt[0] else raise ArgumentError, "wrong arguments (second argument must be a hash)" if opt[1] && ! opt[1].is_a?(Hash) hash = (opt[1] || {}).merge(opt[0] => nil) end req = hash.delete(:if) fact = hash.keys.first dep = hash.delete(fact) raise ArgumentError, "wrong arguments: #{hash.keys.join(', ')}" if hash.size > 0 @requirements[fact] = req @dependencies[fact] = dep @facts |= [fact] fact end |
#fact_accomplished?(context, fact) ⇒ Boolean
Checks if requirement and dependency for the fact are satisfied
29 30 31 |
# File 'lib/fact_checker/base.rb', line 29 def fact_accomplished?(context, fact) fact_possible?(context, fact) && requirement_satisfied_for?(context, fact) end |
#fact_possible?(context, fact) ⇒ Boolean
Checks if dependency for the fact is satisfied
34 35 36 |
# File 'lib/fact_checker/base.rb', line 34 def fact_possible?(context, fact) [* @dependencies[fact] || []].all?{ |dep| fact_accomplished?(context, dep) } end |
#initialize_copy(orig) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/fact_checker/base.rb', line 13 def initialize_copy(orig) super @facts = orig.facts.dup @dependencies = orig.dependencies.dup @requirements = orig.requirements.dup end |
#possible_facts(context) ⇒ Object
24 25 26 |
# File 'lib/fact_checker/base.rb', line 24 def possible_facts(context) facts.select{ |fact| fact_possible?(context, fact) } end |
#requirement_satisfied_for?(context, fact) ⇒ Boolean
Checks if requirement for the fact is satisfied (no dependency checks here)
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fact_checker/base.rb', line 39 def requirement_satisfied_for?(context, fact) return false unless @facts.include?(fact) case req = @requirements[fact] when Symbol then context.send(req) when Proc then req.arity < 1 ? req.call : req.call(context) when NilClass then true else raise RuntimeError, "can't check this fact - wrong requirement" end end |