Class: Rulebow::Ruleset

Inherits:
Module
  • Object
show all
Defined in:
lib/rulebow/ruleset.rb

Overview

Rulesets provides namespace isolation for facts, rules and methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system, name) { ... } ⇒ Ruleset

Instantiate new ruleset.

Parameters:

  • system

    The system to which this ruleset belongs. [System]

  • name

    Name of the ruleset.

Yields:

  • Yields the script defining the ruleset rules.



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

#chainObject

Chain or dependencies.



41
42
43
# File 'lib/rulebow/ruleset.rb', line 41

def chain
  @chain
end

#docsObject (readonly)

Description of ruleset.



38
39
40
# File 'lib/rulebow/ruleset.rb', line 38

def docs
  @docs
end

#nameObject (readonly)

Ruleset name



35
36
37
# File 'lib/rulebow/ruleset.rb', line 35

def name
  @name
end

#rulesObject (readonly)

Array of defined rules.



53
54
55
# File 'lib/rulebow/ruleset.rb', line 53

def rules
  @rules
end

#scriptsObject (readonly)

Rule scripts.



47
48
49
# File 'lib/rulebow/ruleset.rb', line 47

def scripts
  @scripts
end

#sessionObject (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

#watchlistObject (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.

Returns:

  • (Fact)

    Returns



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.

Returns:

  • (String)

    Returns



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.

Examples:

rule env('PATH'=>/foo/) => :dosomething

Returns:

  • (Fact)

    Returns



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.

Examples:

fact :no_doc? do
  File.directory?('doc')
end

Returns:

  • (Symbol)

    Returns the name if name and condition is given.

  • (Fact)

    Returns a fact in only name or condition is given.



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.

Parameters:

  • globs

    List of file globs. [Array<String>]

Returns:

  • (Array<String>)

    Returns



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.

Parameters:

  • globs

    List of file globs. [Array<String>]

Returns:

  • (Array<String>)

    Returns



112
113
114
# File 'lib/rulebow/ruleset.rb', line 112

def ignore!(*globs)
  @watchlist.ignore!(globs)
end

#import(*globs) ⇒ void

TODO:

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

#inspectObject

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(message, options={})
  title = options.delete(:title) || 'Rulebow Notification'
  Notify.notify(title, message.to_s, options)
end

#parse_ruleset_name(name) ⇒ Array (private)

Parse out a ruleset’s name from it’s ruleset dependencies.

Parameters:

  • name

    ruleset name

Returns:



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

TODO:

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

TODO:

Allow for an expression array that conjoins them with AND logic.

Define a rule. Rules are procedures that are tiggered by logical facts.

Examples:

rule :rdocs? do |files|
  sh "rdoc --output doc/rdoc " + files.join(" ")
end

Returns:

  • (Rule)

    Returns



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_sObject

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.

Parameters:

  • globs

    List of file globs. [Array<String>]

Returns:

  • (Array<String>)

    Returns



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.

Parameters:

  • globs

    List of file globs. [Array<String>]

Returns:

  • (Array<String>)

    Returns



94
95
96
# File 'lib/rulebow/ruleset.rb', line 94

def watch!(*globs)
  @watchlist.accept!(globs)
end