Class: Rulebow::Ruleset
- Inherits:
-
Module
- Object
- Module
- Rulebow::Ruleset
- Defined in:
- lib/rulebow/ruleset.rb
Overview
Rulesets provides namespace isolation for facts, rules and methods.
Instance Attribute Summary collapse
-
#chain ⇒ Object
Chain or dependencies.
-
#docs ⇒ Object
readonly
Description of ruleset.
-
#name ⇒ Object
readonly
Ruleset name.
-
#rules ⇒ Object
readonly
Array of defined rules.
-
#scripts ⇒ Object
readonly
Rule scripts.
-
#session ⇒ Object
readonly
Session object can be used to passing information around between rulesets.
-
#watchlist ⇒ Object
readonly
Files to watch for this ruleset.
Instance Method Summary collapse
-
#define_fact(fact) ⇒ Fact
private
Define a fact.
- #define_task(task) ⇒ Object private
-
#desc(description) ⇒ String
Provide a ruleset description.
-
#env(name_to_pattern) ⇒ Fact
Convenince method for defining environment variable facts.
-
#fact(name = nil, &condition) ⇒ Symbol, Fact
(also: #state)
Defines a fact.
-
#ignore(*globs) ⇒ Array<String>
Add paths to be ignored in file rules.
-
#ignore!(*globs) ⇒ Array<String>
Replace globs in ignore list.
-
#import(*globs) ⇒ void
Import from another file, or glob of files, relative to project root.
-
#initialize(system, name) { ... } ⇒ Ruleset
constructor
Instantiate new ruleset.
-
#inspect ⇒ Object
Better inspection string.
-
#notify(message, options = {}) ⇒ void
Issue notification.
-
#parse_ruleset_name(name) ⇒ Array
private
Parse out a ruleset’s name from it’s ruleset dependencies.
-
#require(feature = nil) ⇒ void
Any requires made within a ruleset will not be actually required until a rule is run.
-
#rule(expression, &block) ⇒ Rule
Define a rule.
-
#to_s ⇒ Object
TODO: Good idea?.
-
#watch(*globs) ⇒ Array<String>
Add paths to be watched.
-
#watch!(*globs) ⇒ Array<String>
Replace paths to be watched.
Constructor Details
#initialize(system, name) { ... } ⇒ Ruleset
Instantiate new ruleset.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rulebow/ruleset.rb', line 15 def initialize(system, name, &block) extend ShellUtils extend system extend self @scripts = [] @rules = [] @docs = [] @requires = [] @name, @chain = parse_ruleset_name(name) @session = system.session @watchlist = WatchList.new(:ignore=>system.ignore) module_eval(&block) if block end |
Instance Attribute Details
#chain ⇒ Object
Chain or dependencies.
41 42 43 |
# File 'lib/rulebow/ruleset.rb', line 41 def chain @chain end |
#docs ⇒ Object (readonly)
Description of ruleset.
38 39 40 |
# File 'lib/rulebow/ruleset.rb', line 38 def docs @docs end |
#name ⇒ Object (readonly)
Ruleset name
35 36 37 |
# File 'lib/rulebow/ruleset.rb', line 35 def name @name end |
#rules ⇒ Object (readonly)
Array of defined rules.
53 54 55 |
# File 'lib/rulebow/ruleset.rb', line 53 def rules @rules end |
#scripts ⇒ Object (readonly)
Rule scripts.
47 48 49 |
# File 'lib/rulebow/ruleset.rb', line 47 def scripts @scripts end |
#session ⇒ Object (readonly)
Session object can be used to passing information around between rulesets.
44 45 46 |
# File 'lib/rulebow/ruleset.rb', line 44 def session @session end |
#watchlist ⇒ Object (readonly)
Files to watch for this ruleset.
56 57 58 |
# File 'lib/rulebow/ruleset.rb', line 56 def watchlist @watchlist end |
Instance Method Details
#define_fact(fact) ⇒ Fact (private)
Define a fact.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/rulebow/ruleset.rb', line 256 def define_fact(fact) case fact when Fact fact when String, Regexp @watchlist.accept(fact) FileFact.new(fact) when Symbol Fact.new{ send(fact) } when true, false, nil Fact.new{ fact } else #when Proc Fact.new(&fact) end end |
#define_task(task) ⇒ Object (private)
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/rulebow/ruleset.rb', line 273 def define_task(task) case task when Symbol Proc.new do |*a| meth = method(task) if meth.arity == 0 meth.call else meth.call(*a) end end else task.to_proc end end |
#desc(description) ⇒ String
Provide a ruleset description.
126 127 128 |
# File 'lib/rulebow/ruleset.rb', line 126 def desc(description) @docs << description end |
#env(name_to_pattern) ⇒ Fact
Convenince method for defining environment variable facts.
202 203 204 205 206 207 208 |
# File 'lib/rulebow/ruleset.rb', line 202 def env(name_to_pattern) Fact.new do name_to_pattern.any? do |name, re| re === ENV[name.to_s] # or `all?` instead? end end end |
#fact(name = nil, &condition) ⇒ Symbol, Fact Also known as: state
Defines a fact. Facts define conditions that are used to trigger rules. Named facts are defined as methods to ensure that only one fact is ever defined for a given name. Calling fact again with the same name as a previously defined fact will redefine the condition of that fact.
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rulebow/ruleset.rb', line 173 def fact(name=nil, &condition) if name && conditon define_method(name) do Fact.new(&condition) # TODO: maybe we don't really need the cache after all end name else define_fact(name || condition) end end |
#ignore(*globs) ⇒ Array<String>
Add paths to be ignored in file rules.
103 104 105 |
# File 'lib/rulebow/ruleset.rb', line 103 def ignore(*globs) @watchlist.ignore(globs) unless globs.empty? end |
#ignore!(*globs) ⇒ Array<String>
Replace globs in ignore list.
112 113 114 |
# File 'lib/rulebow/ruleset.rb', line 112 def ignore!(*globs) @watchlist.ignore!(globs) end |
#import(*globs) ⇒ void
Should importing be relative to the importing file? Currently
it is relative to the project root.
This method returns an undefined value.
Import from another file, or glob of files, relative to project root.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rulebow/ruleset.rb', line 64 def import(*globs) globs.each do |glob| #if File.relative?(glob) # dir = Dir.pwd #session.root #File.dirname(caller[0]) # glob = File.join(dir, glob) #end Dir[glob].each do |file| next unless File.file?(file) # add warning next if @scripts.include?(file) @scripts << file module_eval(File.read(file), file) end end end |
#inspect ⇒ Object
Better inspection string.
238 239 240 241 242 243 244 |
# File 'lib/rulebow/ruleset.rb', line 238 def inspect if chain.empty? "#<Ruleset #{name}>" else "#<Ruleset #{name} " + chain.join(' ') + ">" end end |
#notify(message, options = {}) ⇒ void
This method returns an undefined value.
Issue notification.
221 222 223 224 |
# File 'lib/rulebow/ruleset.rb', line 221 def notify(, ={}) title = .delete(:title) || 'Rulebow Notification' Notify.notify(title, .to_s, ) end |
#parse_ruleset_name(name) ⇒ Array (private)
Parse out a ruleset’s name from it’s ruleset dependencies.
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/rulebow/ruleset.rb', line 294 def parse_ruleset_name(name) if Hash === name raise ArgumentError if name.size > 1 list = [name.values].flatten.map{ |b| b.to_sym } name = name.keys.first else list = [] end return name.to_sym, list end |
#require(feature = nil) ⇒ void
This feature has yet to be implemented.
This method returns an undefined value.
Any requires made within a ruleset will not be actually required until a rule is run.
232 233 234 235 |
# File 'lib/rulebow/ruleset.rb', line 232 def require(feature=nil) @requires << feature if feature @requires end |
#rule(expression, &block) ⇒ Rule
Allow for an expression array that conjoins them with AND logic.
Define a rule. Rules are procedures that are tiggered by logical facts.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/rulebow/ruleset.rb', line 141 def rule(expression, &block) case expression when Hash expression.each do |fact, task| fact = define_fact(fact) task = define_task(task) @rules << Rule.new(fact, &task) end else fact = define_fact(expression) @rules << Rule.new(fact, &block) end #rule = Rule.new(@_facts, get_rule_options, &procedure) #@rules << rule #clear_rule_options return @rules end |
#to_s ⇒ Object
TODO: Good idea?
247 248 249 |
# File 'lib/rulebow/ruleset.rb', line 247 def to_s name.to_s end |
#watch(*globs) ⇒ Array<String>
Add paths to be watched.
84 85 86 87 |
# File 'lib/rulebow/ruleset.rb', line 84 def watch(*globs) @watchlist.accept(globs) unless globs.empty? @watchlist end |
#watch!(*globs) ⇒ Array<String>
Replace paths to be watched.
94 95 96 |
# File 'lib/rulebow/ruleset.rb', line 94 def watch!(*globs) @watchlist.accept!(globs) end |