Class: Grammoire::Rule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &setup) ⇒ Rule

Returns a new instance of Rule.



5
6
7
8
9
10
11
# File 'lib/grammoire/rule.rb', line 5

def initialize(name, &setup)
  @name = name
  weights(1)
  pre_condition { true }
  
  instance_eval &setup if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/grammoire/rule.rb', line 3

def name
  @name
end

#weightObject (readonly)

Returns the value of attribute weight.



3
4
5
# File 'lib/grammoire/rule.rb', line 3

def weight
  @weight
end

Instance Method Details

#applies?(context) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/grammoire/rule.rb', line 21

def applies?(context)
  context.instance_eval &@condition
end

#evaluate(context) ⇒ Object



29
30
31
# File 'lib/grammoire/rule.rb', line 29

def evaluate(context)
  context.instance_eval &@action
end

#pre_condition(&condition) ⇒ Object



17
18
19
# File 'lib/grammoire/rule.rb', line 17

def pre_condition(&condition)
  @condition = condition 
end

#produce(&action) ⇒ Object



25
26
27
# File 'lib/grammoire/rule.rb', line 25

def produce(&action)
  @action = action
end

#weights(weight) ⇒ Object



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

def weights(weight)
  @weight = weight
end