Module: Merb::Inspector::Manager

Defined in:
lib/merb_inspector/manager.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cachesObject

Returns the value of attribute caches.



9
10
11
# File 'lib/merb_inspector/manager.rb', line 9

def caches
  @caches
end

.storesObject

Returns the value of attribute stores.



8
9
10
# File 'lib/merb_inspector/manager.rb', line 8

def stores
  @stores
end

Class Method Details

.installObject

Install



75
76
77
# File 'lib/merb_inspector/manager.rb', line 75

def self.install
  mirror("public/stylesheets")
end

.load_builtin_inspectorsObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/merb_inspector/manager.rb', line 21

def self.load_builtin_inspectors
  inspector_dir = File.dirname(__FILE__) / "../../inspectors"
  Dir["#{inspector_dir}/*.rb"].sort.each do |file|
    begin
      require file
    rescue Exception => error
      message = "[MerbInspector] load error: #{error} (#{error.class})\n#{error.backtraces.first rescue nil}"
      Merb.logger.error message
    end
  end
end

.log(message) ⇒ Object



33
34
35
36
37
# File 'lib/merb_inspector/manager.rb', line 33

def self.log(message)
  path = Merb.root / "log" / "inspector.log"
  message = "[Inspector] %s" % message.to_s.strip
  File.open(path, "a+") {|f| f.puts message}
end

.lookup(object) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/merb_inspector/manager.rb', line 45

def self.lookup(object)
  # first, search object itself for mainly class stuff
  if stores.has_key?(object)
    log "lookup: %s => %s" % [object, stores[object]]
    return stores[object]
  end

  # second, search its class
  if caches.has_key?(object.class)
    log "lookup: %s => %s (cached)" % [object.class, caches[object.class] || 'nil']
    return caches[object.class]
  end

  # finally, search ancestors of the class
  klass = object.class.ancestors.find{|klass|
    log "lookup:   %s = %s ... %s" % [object.class, klass, stores[klass]]
    stores.has_key?(klass)
  }
  caches[object.class] = stores[klass]
  if klass
    log "lookup: %s => %s (registered)" % [object.class, caches[object.class]]
  else
    log "lookup: %s => nil (registered as negative cache)" % [object.class]
  end
  return stores[klass]
end

.mirror(dir) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/merb_inspector/manager.rb', line 79

def self.mirror(dir)
  source_dir = File.join(File.dirname(__FILE__), '..', '..', 'mirror', dir)
  target_dir = File.join(Merb.root, dir)
  FileUtils.mkdir_p(target_dir) unless File.exist?(target_dir)

  Dir[source_dir + "/*"].each do |src|
    time = File.mtime(src)
    file = File.basename(src)
    dst  = File.join(target_dir, file)

    next if File.directory?(src)
    next if File.exist?(dst) and File.mtime(dst) >= time
    FileUtils.copy(src, dst)
    File.utime(time, time, dst)
    command = File.exist?(dst) ? "update" : "install"
    log "#{command}: #{dir}/#{file}"
  end
end

.register(klass, inspector) ⇒ Object



39
40
41
42
43
# File 'lib/merb_inspector/manager.rb', line 39

def self.register(klass, inspector)
  action = stores.has_key?(klass) ? "overridden" : "registered"
  stores[klass] = inspector
  log "%s %s -> %s" % [action, klass, inspector]
end

.resetObject



12
13
14
15
16
17
18
19
# File 'lib/merb_inspector/manager.rb', line 12

def self.reset
  self.stores = Hash.new
  self.caches = Hash.new

  log = Merb.root / "log" / "inspector.log"
  File.unlink log if File.exist?(log)
  load_builtin_inspectors
end