Module: Wee::Utils

Defined in:
lib/wee/utils.rb,
lib/wee/utils/helper.rb,
lib/wee/utils/autoreload.rb

Defined Under Namespace

Classes: LRUCache

Class Method Summary collapse

Class Method Details

.app_for(component = nil, options = {}, &block) ⇒ Object

component

The component class for which to create an application object. If a block is given, this will be invoked to create a new component object.

options

A Hash. Following keys are accepted:

:application

The application class to use (default: Wee::Application)

:session

The session class to use (default: Wee::Session)

:page_cache_capacity

The size of the sessions page_store (default: 10)

:id_gen

The id generator to use for session-ids (default: Md5IdGenerator.new)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wee/utils/helper.rb', line 22

def self.app_for(component=nil, options={}, &block)
  raise "either component or block must be given" if component.nil? and block.nil?  

  defaults = {
    :application => Wee::Application,
    :session => Wee::Session,
    :page_cache_capacity => 10,
    :id_gen => Wee::Md5IdGenerator.new
  }
  options = defaults.update(options)

  options[:application].new {|app|
    app.default_request_handler {
      options[:session].new {|sess|
        if component
          sess.component_runner = Wee::ComponentRunner.new(component.new)
        else
          sess.component_runner = Wee::ComponentRunner.new(block.call)
        end
        if sess.respond_to?(:page_store=)
          # This is so that you can use a Pageless session, which does not have
          # a page_store.
          sess.page_store = Wee::Utils::LRUCache.new(options[:page_cache_capacity])
        end
      }
    }
    app.id_generator = options[:id_gen]
  }
end

.autoreload(check_interval = 10) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/wee/utils/autoreload.rb', line 4

def autoreload(check_interval=10)
  Thread.new(Time.now) {|start_time|
    file_mtime = {}
    loop do
      sleep check_interval 
      $LOADED_FEATURES.each do |feature|
        $LOAD_PATH.each do |lp|
          file = File.join(lp, feature)
          if (File.exists?(file) and 
            File.stat(file).mtime > (file_mtime[file] || start_time))
            file_mtime[file] = File.stat(file).mtime
            STDERR.puts "reload #{ file }"
            begin
              load(file)
            rescue Exception => e
              STDERR.puts e.inspect
            end
          end
        end
      end
    end
  }
end

.autoreload_glob(glob, check_interval = 1) ⇒ Object

Note that this method will load any file that matches the glob and in modified since the method call, regardless whether it’s loaded by the current application or not. The glob is expanded only once at the initial method call.



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

def autoreload_glob(glob, check_interval=1)
  files = Dir.glob(glob)
  file_mtime = {}
  start_time = Time.now 
  Thread.new {
    loop do
      sleep check_interval 
      files.each do |file|
        if (File.exists?(file) and 
          File.stat(file).mtime > (file_mtime[file] || start_time))
          file_mtime[file] = File.stat(file).mtime
          STDERR.puts "reload #{ file }"
          begin
            load(file)
          rescue Exception => e
            STDERR.puts e.inspect
          end
        end
      end
    end
  }
end