Module: LikeAVirgin

Defined in:
lib/like_a_virgin.rb,
lib/like_a_virgin/world.rb,
lib/like_a_virgin/options.rb,
lib/like_a_virgin/file_map.rb

Defined Under Namespace

Classes: FileMap, Options, World

Class Method Summary collapse

Class Method Details

.find_specsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/like_a_virgin.rb', line 62

def self.find_specs
  file_map = FileMap.new Rails.root, Dir[Rails.root + "**/*"]

  file_map.matches(%r"spec/(models|controllers|routing|views|helpers|lib)/.*\.rb") do |fm, file|
    fm[file] = file
  end

  file_map.matches(%r"spec/fixtures/.*\.yml") do |fm, file|
    model = File.basename(file, ".yml")
    fm[file] = "spec/models/#{model.singularize}_spec.rb"
    fm[file] = file_map.matches(%r"spec/views/#{model}/.*_spec\.rb")
  end

  file_map.matches(%r"app/models/.*\.rb") do |fm, file|
    model = File.basename(file, ".rb")
    fm[file] = "spec/models/#{model}_spec.rb"
  end

  file_map.matches(%r"app/views/") do |fm, file|
    fm[file] = "spec/views/#{file.sub("app/views/","")}_spec.rb"
  end

  file_map.matches(%r"app/controllers/.*\.rb") do |fm, file|
    controller = File.basename(file, ".rb")
    if controller == "application_controller"
      fm[file] = file_map.matches(%r"spec/controllers/.*_spec\.rb")
    else
      fm[file] = "spec/controllers/#{controller}_spec.rb"
    end
  end

  file_map.matches(%r"app/helpers/.*_helper\.rb") do |fm, file|
    helper = File.basename(file, ".rb")
    if helper == "application_helper"
      fm[file] = file_map.matches(%r"spec/(views|helpers)/.*_spec\.rb")
    else
      fm[file] = "spec/helpers/#{helper}_spec.rb"
      fm[file] = file_map.matches(%r"spec/views/#{helper.sub("_helper","")}/.*_spec\.rb")
    end
  end

  file_map.matches(%r"spec/shared/.*\.rb") do |fm, file|
    fm[file] = file_map.matches(%r"spec/(controllers|routing|views|helpers)/.*_spec\.rb")
  end

  file_map.matches(%r"config/(boot|environment(s/test)?).rb") do |fm, file|
    fm[file] = file_map.matches(%r"spec/(controllers|routing|views|helpers)/.*_spec\.rb")
  end

  file_map.matches(%r"lib/.*\.rb") do |fm, file|
    fm[file] = "spec/#{file}_spec.rb"
  end

  file_map["config/routes.rb"]    = file_map.matches(%r"spec/(controllers|routing|views|helpers)/.*_spec\.rb")
  file_map["spec/spec_helper.rb"] = file_map.matches(%r"spec/(controllers|routing|views|helpers)/.*_spec\.rb")
  file_map["config/database.yml"] = file_map.matches(%r"spec/models/.*_spec\.rb")

  file_map.pattern
end

.setup_notifier(fm) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/like_a_virgin.rb', line 46

def self.setup_notifier(fm)
  notifier = INotify::Notifier.new

  files = fm.keys
  dirs  = files.map{ |f| File.dirname f }.uniq
  dirs.each do |dir|
    notifier.watch(dir, :all_events) do |event|
      next unless event.flags.include?(:modify) || event.flags.include?(:move_to)
      next unless files.include? event.absolute_name
      @changing_files << fm[event.absolute_name]
    end
  end

  notifier
end

.setup_sigtrapObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/like_a_virgin.rb', line 32

def self.setup_sigtrap
  # restart
  Signal.trap(:INT) do
    LikeAVirgin.stop
    exit
  end

  # quit
  Signal.trap(:TSTP) do
    LikeAVirgin.stop
    exit
  end
end

.startObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/like_a_virgin.rb', line 8

def self.start
  setup_sigtrap
  @options = Options.new
  @changing_files = []
  @notifier = setup_notifier find_specs
  World.init(@options)

  @exit = false
  while !@exit
    @notifier.process while @changing_files.empty?
    changed_files = @changing_files.flatten.uniq.sort.dup
    break if changed_files.first.nil?
    @changing_files.clear
    World.emit changed_files
    sleep 1
  end
end

.stopObject



26
27
28
29
30
# File 'lib/like_a_virgin.rb', line 26

def self.stop
  @exit = true
  @notifier.try :close
  @changing_files << nil
end