Class: Watchr::Script::Rule

Inherits:
Struct
  • Object
show all
Defined in:
lib/watchr/script.rb,
lib/watchr/script.rb

Overview

Convenience type. Provides clearer and simpler access to rule properties.

Examples
rule = script.watch('lib/.*\.rb') { 'ohaie' }
rule.pattern      #=> 'lib/.*\.rb'
rule.action.call  #=> 'ohaie'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



60
61
62
# File 'lib/watchr/script.rb', line 60

def action
  @action
end

#batchObject

Returns the value of attribute batch

Returns:

  • (Object)

    the current value of batch



60
61
62
# File 'lib/watchr/script.rb', line 60

def batch
  @batch
end

#event_typesObject

Returns the value of attribute event_types

Returns:

  • (Object)

    the current value of event_types



60
61
62
# File 'lib/watchr/script.rb', line 60

def event_types
  @event_types
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



60
61
62
# File 'lib/watchr/script.rb', line 60

def options
  @options
end

#patternObject

Returns the value of attribute pattern

Returns:

  • (Object)

    the current value of pattern



60
61
62
# File 'lib/watchr/script.rb', line 60

def pattern
  @pattern
end

#predicateObject

Returns the value of attribute predicate

Returns:

  • (Object)

    the current value of predicate



60
61
62
# File 'lib/watchr/script.rb', line 60

def predicate
  @predicate
end

Instance Method Details

#call(data, event, path) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/watchr/script.rb', line 64

def call data, event, path
  # $stderr.print "call #{data} #{event} #{path}\n"
  if options[:batch]
    self.batch ||= Batch.new self
    batch.call data, event, path
  else
    if action.arity == 1 
      action.call data
    elsif action.arity == 2
      action.call data, event
    else
      action.call data, event, path
    end
    Script.learn path
  end
end

#match(path) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/watchr/script.rb', line 92

def match path
  # $stderr.print("match #{path}\n")
  pattern = self.pattern
  ( pattern.class == String ) and ( pattern = Regexp.new pattern )
  # p path, pattern, pattern.match(path)
  ( md = pattern.match(path) ) &&
    ( self.predicate == nil || self.predicate.call(md) )
end

#watch(path) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/watchr/script.rb', line 81

def watch path
  watch = nil
  pattern = self.pattern
  ( pattern.class == String ) and ( pattern = Regexp.new pattern )
  md = pattern.match(path)
  if md
    watch = self.predicate.nil? || self.predicate.call(md)
  end
  return watch
end