Class: Rule

Inherits:
Object show all
Defined in:
lib/Rule/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#mapObject

Returns the value of attribute map.



6
7
8
# File 'lib/Rule/rule.rb', line 6

def map
  @map
end

#ruleObject

Returns the value of attribute rule.



6
7
8
# File 'lib/Rule/rule.rb', line 6

def rule
  @rule
end

Instance Method Details

#as_propositionObject



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

Returns:

  • (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

Returns:

  • (Boolean)


13
14
15
# File 'lib/Rule/rule.rb', line 13

def satisfies?(object)
  object.instance_eval(&rule)
end