class Noteikumi::Engine

The main driver of the rule set

@example 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]

Attributes

path[R]

The paths this engine consulted for rules

@return [Array<String>] list of paths

Public Class Methods

new(path, logger=Logger.new(STDOUT)) click to toggle source

Creates an instance of the rule engine

@param path [String] a File::PATH_SEPARATOR list of paths to load rules from @param logger [Logger] @return [Engine]

# 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

Public Instance Methods

create_state() click to toggle source

Creates a new state that has an associated with this {Engine}

@return [State]

# File lib/noteikumi/engine.rb, line 71
def create_state
  State.new(self, @logger)
end
each_rule() { |rule| ... } click to toggle source

Iterates all the rules in the {Rules} collection

@yieldparam rule [Rule] @return [void]

# File lib/noteikumi/engine.rb, line 79
def each_rule
  rules_collection.rules.each do |rule|
    yield(rule)
  end
end
parse_path(path) click to toggle source

Parse a File::PATH_SEPARATOR seperated path into expanded directories

@api private @param path [String] The path to parse, should be a File::PATH_SEPARATOR list of paths @return [Array<String>]

# 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) click to toggle source

Given a state object process all the loaded rules

@note the rule set is processed once only @param state [State] @return [Array<Result>]

# 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_counts() click to toggle source

Reset the run count on all loaded rules

@api private @return [void]

# File lib/noteikumi/engine.rb, line 47
def reset_rule_counts
  rules_collection.rules.each(&:reset_counter)
end
rules_collection() click to toggle source

Creates and caches a rules collection

@return [Rules]

# File lib/noteikumi/engine.rb, line 88
def rules_collection
  @rules ||= Rules.new(@path, @logger)
end