Class: Noteikumi::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/noteikumi/engine.rb

Overview

The main driver of the rule set

Examples:

create a engine and process some data


engine = Engine.new("rules")
state = engine.create_state

state[:thing] = data_to_process

engine.process_state(state)

puts "%d rules ran" % [state.results.size]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, logger = Logger.new(STDOUT)) ⇒ Engine

Creates an instance of the rule engine

Parameters:

  • path (String)

    a File::PATH_SEPARATOR list of paths to load rules from

  • logger (Logger) (defaults to: Logger.new(STDOUT))


25
26
27
28
29
30
# File 'lib/noteikumi/engine.rb', line 25

def initialize(path, logger=Logger.new(STDOUT))
  @logger = logger
  @path = parse_path(path)

  rules_collection.load_rules
end

Instance Attribute Details

#pathArray<String> (readonly)

The paths this engine consulted for rules

Returns:

  • (Array<String>)

    list of paths



18
19
20
# File 'lib/noteikumi/engine.rb', line 18

def path
  @path
end

Instance Method Details

#create_stateState

Creates a new state that has an associated with this Noteikumi::Engine

Returns:



71
72
73
# File 'lib/noteikumi/engine.rb', line 71

def create_state
  State.new(self, @logger)
end

#each_rule {|rule| ... } ⇒ void

This method returns an undefined value.

Iterates all the rules in the Rules collection

Yield Parameters:



79
80
81
82
83
# File 'lib/noteikumi/engine.rb', line 79

def each_rule
  rules_collection.rules.each do |rule|
    yield(rule)
  end
end

#inspectString

:nodoc:

Returns:

  • (String)


94
95
96
# File 'lib/noteikumi/engine.rb', line 94

def inspect
  "#<%s:%s %d rules from %s>" % [self.class, object_id, rules_collection.count, @path.inspect]
end

#parse_path(path) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse a File::PATH_SEPARATOR seperated path into expanded directories

Parameters:

  • path (String)

    The path to parse, should be a File::PATH_SEPARATOR list of paths

Returns:

  • (Array<String>)


37
38
39
40
41
# File 'lib/noteikumi/engine.rb', line 37

def parse_path(path)
  path.split(File::PATH_SEPARATOR).map do |part|
    File.expand_path(part)
  end
end

#process_state(state) ⇒ Array<Result>

Note:

the rule set is processed once only

Given a state object process all the loaded rules

Parameters:

Returns:



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/noteikumi/engine.rb', line 56

def process_state(state)
  raise("No rules have been loaded into engine %s" % self) if rules_collection.empty?

  reset_rule_counts

  rules_collection.by_priority.each do |rule|
    state.process_rule(rule)
  end

  state.results
end

#reset_rule_countsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Reset the run count on all loaded rules



47
48
49
# File 'lib/noteikumi/engine.rb', line 47

def reset_rule_counts
  rules_collection.rules.each(&:reset_counter)
end

#rules_collectionRules

Creates and caches a rules collection

Returns:



88
89
90
# File 'lib/noteikumi/engine.rb', line 88

def rules_collection
  @rules ||= Rules.new(@path, @logger)
end