Class: CubaApi::Reloader

Inherits:
Object
  • Object
show all
Defined in:
lib/cuba_api/reloader_rack.rb

Class Method Summary collapse

Class Method Details

.doit(basedir, baseconstant) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/cuba_api/reloader_rack.rb', line 26

def self.doit( basedir, baseconstant )
  if @max_last_modified
    parse( basedir, baseconstant ) do |f|
      maybe_remove_constant( f, basedir, baseconstant )
      puts "[CubaAPI::Reloader] #{f}: #{load f}"
    end
  else
    parse( basedir, baseconstant ) {}
  end
end

.maybe_remove_constant(f, basedir, baseconstant) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cuba_api/reloader_rack.rb', line 14

def self.maybe_remove_constant( f, basedir, baseconstant )
  c = baseconstant
  cname = nil
  f.sub( /#{basedir}/, '' ).split( /\/|\./ ).each do |name|
    if name != 'rb'
      ( c = c.const_get( cname ) ) rescue nil
      cname = name.split('_').each { |a| a.capitalize! }.join.to_sym
    end
  end
  c.send( :remove_const, cname ) rescue nil
end

.parse(basedir, baseconstant) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/cuba_api/reloader_rack.rb', line 4

def self.parse( basedir, baseconstant )
  Dir[ File.join( basedir, '**', '*.rb' ) ].each do |f| 
    last_modified = File.mtime( f ).to_f
    if ! File.directory?( f ) && last_modified > @max_last_modified.to_f
      @max_last_modified = last_modified
      yield f
    end
  end
end