Class: Auto

Inherits:
Object show all
Defined in:
lib/auto.rb

Direct Known Subclasses

Autocoverage, Autocyclo, Autoflog, Autometric

Constant Summary collapse

WINDOZE =
/win32/ =~ RUBY_PLATFORM
SEP =
WINDOZE ? '&' : ';'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout) ⇒ Auto

Returns a new instance of Auto.



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/auto.rb', line 61

def initialize(output = $stdout)
  @output = output
  @sleep = 2
  @modified = nil
  @interrupted = @wants_to_quit = false
  @working_dirs = %w( lib )

  @file_masks = {
    :lib => /^lib\/.*\.rb$/
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/auto.rb', line 142

def method_missing(id, *args)
  if match = id.to_s.match(/_files$/)
    find_files(@file_masks[match.pre_match.to_sym])
  else
    old_method_missing(id, *args)
  end
end

Class Attribute Details

.discoveriesObject

Returns the value of attribute discoveries.



15
16
17
# File 'lib/auto.rb', line 15

def discoveries
  @discoveries
end

.hooksObject

Returns the value of attribute hooks.



15
16
17
# File 'lib/auto.rb', line 15

def hooks
  @hooks
end

Instance Attribute Details

#file_masksObject

Returns the value of attribute file_masks.



59
60
61
# File 'lib/auto.rb', line 59

def file_masks
  @file_masks
end

#modifiedObject

Returns the value of attribute modified.



59
60
61
# File 'lib/auto.rb', line 59

def modified
  @modified
end

#outputObject

Returns the value of attribute output.



59
60
61
# File 'lib/auto.rb', line 59

def output
  @output
end

#sleepObject

Returns the value of attribute sleep.



59
60
61
# File 'lib/auto.rb', line 59

def sleep
  @sleep
end

#wants_to_quitObject

Returns the value of attribute wants_to_quit.



59
60
61
# File 'lib/auto.rb', line 59

def wants_to_quit
  @wants_to_quit
end

#working_dirsObject

Returns the value of attribute working_dirs.



59
60
61
# File 'lib/auto.rb', line 59

def working_dirs
  @working_dirs
end

Class Method Details

.add_discovery(&proc) ⇒ Object



29
30
31
# File 'lib/auto.rb', line 29

def add_discovery &proc
  discoveries << proc
end

.add_hook(name, &blk) ⇒ Object



25
26
27
# File 'lib/auto.rb', line 25

def add_hook name, &blk
  hooks[name] << blk
end

.autodiscoverObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/auto.rb', line 33

def autodiscover
  style = []

  $:.push(*Dir["vendor/plugins/*/lib"])
  paths = $:.dup

  begin
    require 'rubygems'
    paths.push(*Gem.latest_load_paths)
  rescue LoadError => e
    # do nothing
  end

  paths.each do |d|
    f = File.join(d, 'autometric', 'discover.rb')
    load f if File.exist? f
  end

  discoveries.map { |proc| proc.call }.flatten.compact.sort.uniq
end

.run(*args) ⇒ Object



54
55
56
# File 'lib/auto.rb', line 54

def run *args
  new(*args).run
end

Instance Method Details

#find_files(masks = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/auto.rb', line 98

def find_files(masks = nil)
  masks ||= @file_masks.collect{|k,v| v if @working_dirs.include? k.to_s}.compact
  files = []
  
  Find.find '.' do |f|
    filename = f.sub(/^\.\//, '')
    files << f if [masks].flatten.any? { |mask| filename =~ mask }
  end
  
  return files
end

#hook(name) ⇒ Object



73
74
75
76
77
# File 'lib/auto.rb', line 73

def hook(name)
  self.class.hooks[name].inject(false) do |handled,plugin|
    plugin[self] || handled
  end
end

#last_modified_timeObject



94
95
96
# File 'lib/auto.rb', line 94

def last_modified_time
  find_files.collect {|f| File::mtime(f) }.max
end

#old_method_missingObject



140
# File 'lib/auto.rb', line 140

alias_method :old_method_missing, :method_missing

#resetObject



79
80
81
# File 'lib/auto.rb', line 79

def reset
  @modified = last_modified_time
end

#runObject



110
111
112
113
114
115
116
117
118
# File 'lib/auto.rb', line 110

def run
  hook :run

  reset
  loop do # ^c handler
    run_metric
    wait_for_changes
  end
end

#run_metricObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/auto.rb', line 120

def run_metric
  @wants_to_quit ||= false
  cmd = make_cmd

  hook :run_command
  @output.puts cmd
  
  @results = []
  line = []

  IO.popen(cmd) do |stdout|
    @results = stdout.read
  end
  
  @output << @results
  hook :ran_command

  handle_results(@results)
end

#wait_for_changesObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/auto.rb', line 83

def wait_for_changes
  loop do
    mtime = last_modified_time
    if @modified.nil? || mtime > @modified
      @modified = mtime
      break
    end
    Kernel::sleep(@sleep)
  end
end