Class: Rule
Instance Attribute Summary collapse
-
#map ⇒ Object
Returns the value of attribute map.
-
#rule ⇒ Object
Returns the value of attribute rule.
Instance Method Summary collapse
- #as_proposition ⇒ Object
- #contradictory?(other_rule) ⇒ Boolean
-
#initialize(map, &block) ⇒ Rule
constructor
A new instance of Rule.
- #satisfies?(object) ⇒ Boolean
Constructor Details
#initialize(map, &block) ⇒ Rule
Returns a new instance of Rule.
8 9 10 11 |
# File 'lib/Rule/rule.rb', line 8 def initialize(map, &block) self.rule = Proc.new(&block) self.map = map end |
Instance Attribute Details
#map ⇒ Object
Returns the value of attribute map.
6 7 8 |
# File 'lib/Rule/rule.rb', line 6 def map @map end |
#rule ⇒ Object
Returns the value of attribute rule.
6 7 8 |
# File 'lib/Rule/rule.rb', line 6 def rule @rule end |
Instance Method Details
#as_proposition ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/Rule/rule.rb', line 23 def as_proposition rule.to_source(strip_enclosure: true).gsub(/>|>=/, 'then') .gsub(/<|<=/, 'then !') .gsub(/==|equal\?/, 'only_if') .gsub(/nil\?/, '!= nil') .gsub(/\d+/) do |match| map.fetch(match) end .tr('.', ' ')[1..-2] end |
#contradictory?(other_rule) ⇒ Boolean
17 18 19 20 21 |
# File 'lib/Rule/rule.rb', line 17 def contradictory?(other_rule) analyzer = LogicAnalyzer.new(as_proposition, other_rule.as_proposition) analyzer.parse !analyzer.evaluate end |
#satisfies?(object) ⇒ Boolean
13 14 15 |
# File 'lib/Rule/rule.rb', line 13 def satisfies?(object) object.instance_eval(&rule) end |