Class: Grammoire::Grammar

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

Instance Method Summary collapse

Constructor Details

#initialize(random_generator = RandomGenerator.new) ⇒ Grammar

Returns a new instance of Grammar.



4
5
6
7
8
# File 'lib/grammoire/grammar.rb', line 4

def initialize(random_generator = RandomGenerator.new)
  @rules = []
  @chooser = RuleChooser.new(random_generator)
  context(EvaluationContext)
end

Instance Method Details

#context(context_class) ⇒ Object



10
11
12
# File 'lib/grammoire/grammar.rb', line 10

def context(context_class)
  @context = context_class.new(self)
end

#evaluate(rule_name, data = {}) ⇒ Object



18
19
20
21
22
# File 'lib/grammoire/grammar.rb', line 18

def evaluate(rule_name, data = {})
  @context.with_data_points(data)

  return @chooser.select_from(rules_for(rule_name)).evaluate(@context)
end

#rule(name, &action) ⇒ Object



14
15
16
# File 'lib/grammoire/grammar.rb', line 14

def rule(name, &action)
  @rules << Rule.new(name, &action)
end