class Ergo::Book

Ergo rulebook stores defined states and rules.

TODO: There are some minor namespace issues with this implementation.

We don't necessarily want a rule block to be able to
call #rule. However, the scoping is a bit complicated,
so it's an acceptable niggle for now.

Attributes

name[R]

Book name

rules[R]

Array of defined rules.

scripts[R]

Rule scripts.

session[R]

Current session.

states[R]

Array of defined states.

Public Class Methods

new(system, name, &block) click to toggle source

Instantiate new system.

Arguments

system - The system to which this book belongs. [System]
name   - Name of the book.

Yields the script defining the books rules.

# File lib/ergo/book.rb, line 20
def initialize(system, name, &block)
  extend ShellUtils
  extend system
  extend self

  @scripts = []
  @rules   = []
  @states  = {}

  @name    = name.to_s
  @ignore  = system.ignore
  @session = system.session

  clear_rule_options

  module_eval(&block) if block
end

Public Instance Methods

bookmark(*names)
Alias for: mark
desc(description) click to toggle source

Set rule description. The next rule defined will get the most recently defined description attached to it.

Returns [String]

# File lib/ergo/book.rb, line 177
def desc(description)
  @_desc = description
end
env(name_to_pattern) click to toggle source

Define an environment state.

Examples

env('PATH'=>/foo/)

Returns [State]

# File lib/ergo/book.rb, line 135
def env(name_to_pattern)
  State.new do
    name_to_pattern.any? do |name, re|
      re === ENV[name.to_s]  # or `all?` instead?
    end
  end
end
file(pattern) click to toggle source

Define a file state.

Returns [FileState]

# File lib/ergo/book.rb, line 125
def file(pattern)
  FileState.new(pattern)
end
ignore(*globs) click to toggle source

Add paths to be ignored in file rules.

globs - List of file globs. [Array<String>]

Returns [Array<String>]

# File lib/ergo/book.rb, line 78
def ignore(*globs)
  @ignore.concat(globs) unless globs.empty?
  @ignore
end
ignore!(*globs) click to toggle source

Replace globs in ignore list.

globs - List of file globs. [Array<String>]

Returns [Array<String>]

# File lib/ergo/book.rb, line 88
def ignore!(*globs)
  @ignore.replace(globs)
  @ignore
end
import(*globs) click to toggle source

Import from another file, or glob of files, relative to project root.

TODO: Should importing be relative the importing file?

Returns nothing.

# File lib/ergo/book.rb, line 58
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
mark(*names) click to toggle source

Bookmark the rule.

Returns nothing.

# File lib/ergo/book.rb, line 184
def mark(*names)
  @_mark.concat(names)
end
Also aliased as: bookmark
notify(message, options={}) click to toggle source

Issue notification.

Returns nothing.

# File lib/ergo/book.rb, line 199
def notify(message, options={})
  title = options.delete(:title) || 'Fire Notification'
  Notify.notify(title, message.to_s, options)
end
private(*methods) click to toggle source
Calls superclass method
# File lib/ergo/book.rb, line 191
def private(*methods)
  @_priv = true
  super(*methods)   # TODO: why doesn't this work as expected?
end
rule(state, &procedure) click to toggle source

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

Examples

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

Returns [Rule]

# File lib/ergo/book.rb, line 152
def rule(state, &procedure)
  case state
  when String, Regexp
    state = file(state)
  when Symbol
    # TODO: Is this really the best idea?
    #@states[state.to_sym]
  end
  rule = Rule.new(state, get_rule_options, &procedure)
  @rules << rule
  clear_rule_options
  rule
end
state(name=nil, &condition) click to toggle source

Define a named state. States define conditions that are used to trigger rules. Named states are kept in a hash table to ensure that only one state is ever defined for a given name. Calling state again with the same name as a previously defined state will redefine the condition of that state.

Examples

state :no_rdocs? do
  files = Dir.glob('lib/**/*.rb')
  FileUtils.uptodate?('doc', files) ? files : false
end

Returns nil if state name is given. [nil] Returns State in no name is given. [State]

# File lib/ergo/book.rb, line 106
def state(name=nil, &condition)
  if name
    if condition
      @states[name.to_sym] = condition
      define_method(name) do |*args|
        state = @states[name.to_sym]
        State.new{ states[name.to_sym].call(*args) }
      end
    else
      raise ArgumentError
    end
  else
    State.new{ condition.call(*args) }
  end
end
state?(name, *args) click to toggle source

Check a name state.

Returns [Array,Boolean]

# File lib/ergo/book.rb, line 169
def state?(name, *args)
  @states[name.to_sym].call(*args)
end

Private Instance Methods

clear_rule_options() click to toggle source
# File lib/ergo/book.rb, line 210
def clear_rule_options
  @_mark = [name].compact
  @_desc = nil
  @_priv = false
end
get_rule_options() click to toggle source
# File lib/ergo/book.rb, line 206
def get_rule_options     
  { :desc=>@_desc, :mark=>@_mark, :private=>@_priv }
end