Class: Astaire::InlineTemplates

Inherits:
ActionView::PathResolver
  • Object
show all
Defined in:
lib/astaire.rb

Instance Method Summary collapse

Constructor Details

#initializeInlineTemplates

Returns a new instance of InlineTemplates.



19
20
21
22
# File 'lib/astaire.rb', line 19

def initialize
  super
  @templates = {}
end

Instance Method Details

#add_controller(controller) ⇒ Object



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
# File 'lib/astaire.rb', line 24

def add_controller(controller)
  file = caller_files.first

  begin
    app, data =
      ::IO.read(file).gsub("\r\n", "\n").split(/^__END__$/, 2)
  rescue Errno::ENOENT
    app, data = nil
  end

  if data
    lines = app.count("\n") + 1
    template = nil
    data.each_line do |line|
      lines += 1
      if line =~ /^@@\s*(.*)/
        template = ''
        @templates["#{controller.controller_path}/#{$1}"] =
          [template, file, lines]
      elsif template
        template << line
      end
    end
  end
end

#caller_filesObject

Like Kernel#caller but excluding certain magic entries and without line / method information; the resulting array contains filenames only.



68
69
70
71
# File 'lib/astaire.rb', line 68

def caller_files
  caller_locations.
    map { |file,line| file }
end

#caller_locationsObject



73
74
75
76
77
# File 'lib/astaire.rb', line 73

def caller_locations
  caller(1).
    map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
    reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
end

#query(path, exts, formats) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/astaire.rb', line 50

def query(path, exts, formats)
  query = Regexp.escape(path)
  exts.each do |ext|
    query << '(' << ext.map {|e| e && Regexp.escape(".#{e}") }.join('|') << '|)'
  end

  templates = []
  @templates.select { |k,v| k =~ /^#{query}$/ }.each do |path, (source, file, lines)|
    handler, format = extract_handler_and_format(path, formats)
    templates << ActionView::Template.new(source, path, handler,
      :virtual_path => path, :format => format)
  end

  templates.sort_by {|t| -t.identifier.match(/^#{query}$/).captures.reject(&:blank?).size }
end