Class: Noteikumi::Engine
- Inherits:
-
Object
- Object
- Noteikumi::Engine
- Defined in:
- lib/noteikumi/engine.rb
Overview
The main driver of the rule set
Instance Attribute Summary collapse
-
#path ⇒ Array<String>
readonly
The paths this engine consulted for rules.
Instance Method Summary collapse
-
#create_state ⇒ State
Creates a new state that has an associated with this Engine.
-
#each_rule {|rule| ... } ⇒ void
Iterates all the rules in the Rules collection.
-
#initialize(path, logger = Logger.new(STDOUT)) ⇒ Engine
constructor
Creates an instance of the rule engine.
-
#inspect ⇒ String
:nodoc:.
-
#parse_path(path) ⇒ Array<String>
private
Parse a File::PATH_SEPARATOR seperated path into expanded directories.
-
#process_state(state) ⇒ Array<Result>
Given a state object process all the loaded rules.
-
#reset_rule_counts ⇒ void
private
Reset the run count on all loaded rules.
-
#rules_collection ⇒ Rules
Creates and caches a rules collection.
Constructor Details
#initialize(path, logger = Logger.new(STDOUT)) ⇒ Engine
Creates an instance of the rule engine
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
#path ⇒ Array<String> (readonly)
The paths this engine consulted for rules
18 19 20 |
# File 'lib/noteikumi/engine.rb', line 18 def path @path end |
Instance Method Details
#create_state ⇒ State
Creates a new state that has an associated with this Noteikumi::Engine
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
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 |
#inspect ⇒ String
:nodoc:
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
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.(part) end end |
#process_state(state) ⇒ Array<Result>
the rule set is processed once only
Given a state object process all the loaded rules
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_counts ⇒ void
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 |