class Ergo::Runner

Runner is the main class which controls execution.

Attributes

digests[R]

Returns [Hash]

Public Class Methods

new(options={}) click to toggle source

Initialize new Session instance.

Returns nothing.

# File lib/ergo/runner.rb, line 13
def initialize(options={})
  @script     = options[:script]
  @system     = options[:system]

  self.root   = options[:root]
  self.trial  = options[:trial]
  self.fresh  = options[:fresh]
  self.watch  = options[:watch]
  self.ignore = options[:ignore]
  
  @digests = {}
end

Public Instance Methods

fresh=(boolean) click to toggle source

Set whether to nullify digest and make a fresh run.

Returns [Boolean]

# File lib/ergo/runner.rb, line 60
def fresh=(boolean)
  @fresh = !! boolean
end
fresh?() click to toggle source

Nullify digest and make a fresh run?

Returns [Boolean]

# File lib/ergo/runner.rb, line 53
def fresh?
  @fresh
end
home() click to toggle source

Home directory.

Returns [String]

# File lib/ergo/runner.rb, line 121
def home
  @home ||= File.expand_path('~')
end
ignore() click to toggle source

File globs to ignore.

Returns [Ignore] instance.

# File lib/ergo/runner.rb, line 149
def ignore
  @ignore ||= Ignore.new(:root=>root)
end
ignore=(file) click to toggle source

Set ignore.

# File lib/ergo/runner.rb, line 154
def ignore=(file)
  @ignore = Ignore.new(:root=>root, :file=>file)
end
root() click to toggle source

Locate project root. This method ascends up the file system starting as the current working directory looking for a ‘.ergo` directory. When found, the directory in which it is found is returned as the root. It is also memoized, so repeated calls to this method will not repeat the search.

Returns [String]

# File lib/ergo/runner.rb, line 90
def root
  dir = root?
  raise RootError, "cannot locate project root" unless dir
  dir
end
root=(dir) click to toggle source

Set the root directory.

Returns [String]

# File lib/ergo/runner.rb, line 114
def root=(dir)
  @root = dir if dir
end
root?() click to toggle source
# File lib/ergo/runner.rb, line 97
def root?
  @root ||= (
    r = nil
    d = Dir.pwd
    while d != home && d != '/'
      if File.directory?('.ergo')
        break r = d
      end
      d = File.dirname(d)
    end
    r
  )
end
rules() click to toggle source

List of rules from the system.

Returns [Array<Rule>]

# File lib/ergo/runner.rb, line 161
def rules
  system.rules
end
run(*marks) click to toggle source

Run rules.

Returns nothing.

# File lib/ergo/runner.rb, line 168
def run(*marks)
  raise ArgumentError, "invalid bookmark" unless marks.all?{ |m| /\w+/ =~ m }

  if watch
    autorun(*marks)
  else
    monorun(*marks)
  end
end
script() click to toggle source

Rules script to load.

Returns List of file paths. [Array]

# File lib/ergo/runner.rb, line 135
def script
  @script || (@system ? nil : Dir[RULES_SCRIPT].first)
end
system() click to toggle source

Instance of {Ergo::System}.

Returns [System]

# File lib/ergo/runner.rb, line 128
def system
  @system ||= System.new(script)
end
trial=(bool) click to toggle source

Set trial run mode.

Arguments

bool - Flag for trial mode. [Boolean]

Returns ‘bool` flag. [Boolean]

# File lib/ergo/runner.rb, line 79
def trial=(bool)
  @trial = !!bool
end
trial?() click to toggle source

Is this trial-run only?

TODO: Trial mode is not implemented yet!

Returns [Boolean]

# File lib/ergo/runner.rb, line 69
def trial?
  @trial
end
watch() click to toggle source

Watch period, default is every 5 minutes.

Returns [Fixnum]

# File lib/ergo/runner.rb, line 29
def watch
  @watch
end
watch=(seconds) click to toggle source

Set watch seconds. Minimum watch time is 1 second. Setting watch before calling run creates a simple loop. It can eat up CPU cycles so use it wisely. A watch time of 4 seconds is a good time period. If you are patient go for 15 seconds or more.

Returns [Fixnum,nil]

# File lib/ergo/runner.rb, line 40
def watch=(seconds)
  if seconds
    seconds = seconds.to_i
    seconds = 1 if seconds < 1
    @watch = seconds
  else
    @watch = nil 
  end
end

Private Instance Methods

autorun(*marks) click to toggle source

Run rules periodically.

Returns nothing.

# File lib/ergo/runner.rb, line 198
def autorun(*marks)
  Dir.chdir(root) do
    fresh_digest(*marks) if fresh?

    trap("INT") { puts "\nPutting out the fire!"; exit }
    puts "Fire started! (pid #{Process.pid})"

    if marks.size > 0
      loop do
        run_bookmarks(*marks)
        sleep(watch)
      end
    else
      loop do
        run_rules
        sleep(watch)
      end
    end
  end
end
clear_digests() click to toggle source

Clear away all digests but the main digest.

Returns nothing.

# File lib/ergo/runner.rb, line 298
def clear_digests
  Digest.clear_digests
  @digests = {}
end
digest(name=nil) click to toggle source

get digest by name, if it doesn’t exit create a new one.

# File lib/ergo/runner.rb, line 269
def digest(name=nil)
  @digests[name] ||= Digest.new(:ignore=>ignore, :name=>name)
end
fresh_digest(*marks) click to toggle source

Start with a clean slate by remove the digest.

Returns nothing.

# File lib/ergo/runner.rb, line 284
def fresh_digest(*marks)
  if marks.empty?
    clear_digests
  else
    marks.each do |mark|
      d = @digests.delete(mark)
      d.remove if d
    end
  end
end
latest_digest(rule) click to toggle source

Get the most recent digest for a given rule.

Returns [Digest]

# File lib/ergo/runner.rb, line 276
def latest_digest(rule)
  name = Digest.latest(*rule.bookmarks)
  digest(name)
end
monorun(*marks) click to toggle source

Run rules once.

Returns nothing.

# File lib/ergo/runner.rb, line 183
def monorun(*marks)
  Dir.chdir(root) do
    fresh_digest(*marks) if fresh?

    if marks.size > 0
      run_bookmarks(*marks)
    else
      run_rules
    end
  end
end
run_bookmarks(*marks) click to toggle source

Run only those rules with a specific bookmark.

marks - Bookmark names. [Array<String>].

Returns nothing.

# File lib/ergo/runner.rb, line 250
def run_bookmarks(*marks)
  system.rules.each do |rule|
    case rule
    when Book
      book = rule
      book.rules.each do |rule|
        next unless marks.any?{ |mark| rule.mark?(mark) }
        rule.apply(latest_digest(rule))
      end
    else
      next unless marks.any?{ |mark| rule.mark?(mark) }
      rule.apply(latest_digest(rule))
    end
  end

  save_digests(*marks)
end
run_rules() click to toggle source

Run all rules (expect private rules).

Returns nothing.

# File lib/ergo/runner.rb, line 225
def run_rules
  system.rules.each do |rule|
    case rule
    when Book
      book = rule
      book.rules.each do |rule|
        next if rule.private?
        rule.apply(latest_digest(rule))
      end
    else
      next if rule.private?
      rule.apply(latest_digest(rule))
    end
  end

  clear_digests

  digest.save
end
save_digests(*bookmarks) click to toggle source

Save digests for given bookmarks.

Returns nothing.

# File lib/ergo/runner.rb, line 306
def save_digests(*bookmarks)
  bookmarks.each do |mark|
    digest(mark).save
  end
end