class Gazr::Script

Constants

DEFAULT_EVENT_TYPE
Rule

Attributes

ec[R]
rules[R]

Public Class Methods

new(path = nil) click to toggle source
# File lib/gazr/script.rb, line 31
def initialize(path = nil)
  @path = path
  @rules = []
  @default_action = Proc.new {}
  @ec = EvalContext.new(self)
end

Public Instance Methods

action_for(path, event_type = DEFAULT_EVENT_TYPE) click to toggle source
# File lib/gazr/script.rb, line 64
def action_for(path, event_type = DEFAULT_EVENT_TYPE)
  path = rel_path(path).to_s
  rule = rules_for(path).detect {|rule| rule.event_type.nil? || rule.event_type == event_type }
  if rule
    data = path.match(rule.pattern)
    lambda { rule.action.call(data) }
  else
    lambda {}
  end
end
default_action(&action) click to toggle source
# File lib/gazr/script.rb, line 43
def default_action(&action)
  @default_action = action if action
  @default_action
end
parse!() click to toggle source
# File lib/gazr/script.rb, line 53
def parse!
  return unless @path
  reset
  @ec.instance_eval(@path.read, @path.to_s)
rescue Errno::ENOENT
  sleep(0.5)
  retry
ensure
  Gazr.debug('loaded script file %s' % @path.to_s.inspect)
end
path() click to toggle source
# File lib/gazr/script.rb, line 79
def path
  @path && Pathname(@path.respond_to?(:to_path) ? @path.to_path : @path.to_s).expand_path
end
patterns() click to toggle source
# File lib/gazr/script.rb, line 75
def patterns
  @rules.map {|r| r.pattern }
end
reset() click to toggle source
# File lib/gazr/script.rb, line 48
def reset
  @rules = []
  @default_action = Proc.new {}
end
watch(pattern, event_type = DEFAULT_EVENT_TYPE, &action) click to toggle source
# File lib/gazr/script.rb, line 38
def watch(pattern, event_type = DEFAULT_EVENT_TYPE, &action)
  @rules << Rule.new(pattern, event_type, action || @default_action)
  @rules.last
end

Private Instance Methods

rel_path(path) click to toggle source
# File lib/gazr/script.rb, line 89
def rel_path(path)
  Pathname(path).expand_path.relative_path_from(Pathname(Dir.pwd))
end
rules_for(path) click to toggle source
# File lib/gazr/script.rb, line 85
def rules_for(path)
  @rules.reverse.select {|rule| path.match(rule.pattern) }
end