Class: Tanuki::Loader

Inherits:
Object show all
Defined in:
lib/tanuki/loader.rb

Overview

Tanuki::Loader deals with framework paths resolving, and object and template loading.

Class Method Summary collapse

Class Method Details

.class_path(klass, root) ⇒ Object

Returns the path to a source file in root containing class klass.



9
10
11
12
# File 'lib/tanuki/loader.rb', line 9

def class_path(klass, root)
  path = const_to_path(klass, root)
  File.join(path, path.match("#{File::SEPARATOR}([^#{File::SEPARATOR}]*)$")[1] << '.rb')
end

.combined_app_rootObject

Returns a glob pattern of all common roots.



21
22
23
24
25
26
# File 'lib/tanuki/loader.rb', line 21

def combined_app_root
  local_app_root = File.expand_path(File.join('..', '..', '..', 'app'), __FILE__)
  app_root = "{#{context_app_root = @context.app_root},#{@context.gen_root}"
  app_root << ",#{local_app_root}" if local_app_root != context_app_root
  app_root << '}'
end

.combined_class_path(klass) ⇒ Object

Returns the path to a source file containing class klass. Seatches across all common roots.



16
17
18
# File 'lib/tanuki/loader.rb', line 16

def combined_class_path(klass)
  class_path(klass, @app_root ||= combined_app_root)
end

.context=(ctx) ⇒ Object

Assigns a context to Tanuki::Loader. This context should have common framework paths defined in it.



30
31
32
# File 'lib/tanuki/loader.rb', line 30

def context=(ctx)
  @context = ctx
end

.has_template?(templates, klass, sym) ⇒ Boolean

Checks if templates contain a compiled template sym for class klass.

Returns:

  • (Boolean)


35
36
37
# File 'lib/tanuki/loader.rb', line 35

def has_template?(templates, klass, sym)
  templates.include? "#{klass}##{sym}"
end

.run_template(templates, obj, sym, *args, &block) ⇒ Object

Runs template sym with optional args and block from object obj.

Template is recompiled from source on two conditions:

  • template source modification time is older than compiled template modification time,

  • Tanuki::TemplateCompiler source modification time is older than compiled template modification time.



44
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
# File 'lib/tanuki/loader.rb', line 44

def run_template(templates, obj, sym, *args, &block)
  owner, st_path = *template_owner(obj.class, sym)
  if st_path
    ct_path = compiled_template_path(owner, sym)
    ct_file_exists = File.file?(ct_path)
    ct_file_mtime = ct_file_exists ? File.mtime(ct_path) : nil
    st_file = File.new(st_path, 'r:UTF-8')
    if !ct_file_exists || st_file.mtime > ct_file_mtime || File.mtime(COMPILER_PATH) > ct_file_mtime
      no_refresh = compile_template(st_file, ct_path, ct_file_mtime, owner, sym)
    else
      no_refresh = true
    end
    method_name = "#{sym}_view".to_sym
    owner.instance_eval do
      unless (method_exists = instance_methods(false).include? method_name) && no_refresh
        remove_method method_name if method_exists
        load ct_path
      end
    end
    templates["#{owner}##{sym}"] = nil
    templates["#{obj.class}##{sym}"] = nil
    obj.send(method_name, *args, &block)
  else
    raise "undefined template `#{sym}' for #{obj.class}"
  end
end