Class: Wake::Script::Rule

Inherits:
Struct
  • Object
show all
Defined in:
lib/wake/script.rb,
lib/wake/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



64
65
66
# File 'lib/wake/script.rb', line 64

def action
  @action
end

#batchObject

Returns the value of attribute batch

Returns:

  • (Object)

    the current value of batch



64
65
66
# File 'lib/wake/script.rb', line 64

def batch
  @batch
end

#event_typesObject

Returns the value of attribute event_types

Returns:

  • (Object)

    the current value of event_types



64
65
66
# File 'lib/wake/script.rb', line 64

def event_types
  @event_types
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



64
65
66
# File 'lib/wake/script.rb', line 64

def options
  @options
end

#patternObject

Returns the value of attribute pattern

Returns:

  • (Object)

    the current value of pattern



64
65
66
# File 'lib/wake/script.rb', line 64

def pattern
  @pattern
end

#predicateObject

Returns the value of attribute predicate

Returns:

  • (Object)

    the current value of predicate



64
65
66
# File 'lib/wake/script.rb', line 64

def predicate
  @predicate
end

Instance Method Details

#call(data, event, path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wake/script.rb', line 68

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
    res = nil
    if action.arity == 1 
      res = action.call data
    elsif action.arity == 2
      res = action.call data, event
    else
      res = action.call data, event, path
    end
    Script.learn path
    res
  end
end

#match(path) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/wake/script.rb', line 98

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



87
88
89
90
91
92
93
94
95
96
# File 'lib/wake/script.rb', line 87

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