Class: Box::File

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

Class Method Summary collapse

Class Method Details

.caller_files(keep = 3) ⇒ Object



3
4
5
6
# File 'lib/box.rb', line 3

def self.caller_files(keep = 3)
  ## Here there be dragons.
  caller(1).map { |line| line.split(/:(?=\d|in )/, 3)[0,1] }.flatten
end

.inline_templatesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/box.rb', line 8

def self.inline_templates
  prioritized_callers = caller_files.reverse

  ## ARGH, loops! I'm sorry.
  loop do
    file = prioritized_callers.shift

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

    break if @data || prioritized_callers.empty?
  end

  if @data
    file_names = @data.scan(/^@@\s*(\S*)\s*$/).flatten
    file_contents = @data.split(/\n^@@.*$\n/)
    file_contents.shift
  end

  unless file_names.nil?
    Hash[file_names.zip(file_contents)]
  else
    {}
  end
end