Class: Vitrine::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/vitrine.rb

Overview

A little idiosyncrastic asset server. Does very simple things:

  • sensible detector for default pages (they render from Sinatra view templates)

  • automatic compilation of CoffeeScript and SASS assets - just request them with .js and .css

and Vitrine will find them and compile them for you on the spot

Instance Method Summary collapse

Instance Method Details

#get_layoutObject



96
97
98
99
# File 'lib/vitrine.rb', line 96

def get_layout
  layouts = Dir.glob(File.join(settings.views, 'layout.*'))
  layouts.any? ? :layout : false
end

#log(msg) ⇒ Object



101
102
103
# File 'lib/vitrine.rb', line 101

def log(msg)
  $stderr.puts(msg) unless settings.silent?
end

#render_template(extensionless_path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vitrine.rb', line 56

def render_template(extensionless_path)
  # Find the related view
  specific_view = extensionless_path + ".*"
  view_index = extensionless_path + "/index.*"
  
  # Catch-all template for HTML5 apps using pushState
  catch_all = "/catch_all.*"
  
  possible_globs = [specific_view, view_index, catch_all]
  
  # Glob for all the possibilites
  possibilites = possible_globs.map do | pattern |
    Dir.glob(File.join(settings.views, pattern))
  end.flatten.reject do | e |
    File.basename(e) =~ /^\./ # except invisibles and self-links
  end
  
  # Try the first template that has been found
  template_path = possibilites.shift
  
  # If nothing is found try downstream or bail
  unless template_path
    err = possible_globs.map{|e| e.inspect }.join(', ')
    if @app
      return forward
    else
      halt 404, "No template found - tried #{err}, and no downstream Rack handler present"
    end
  end
  
  relative_path = Pathname.new(template_path).relative_path_from(Pathname.new(settings.views))
  
  log "-> #{extensionless_path.inspect} : Rendering via template #{relative_path.to_s.inspect}"
  
  locals = {}
  # Auto-pick the template engine out of the extension
  template_engine = File.extname(template_path).gsub(/^\./, '')
  render(template_engine, File.read(template_path), :layout => get_layout, :locals => locals)
end

#render_template_or_static(extensionless_path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vitrine.rb', line 43

def render_template_or_static(extensionless_path)
  probable_html = extensionless_path + "/index.html"
  
  html_path = File.join(settings.public_folder, probable_html)
  if File.exist? html_path
    # Might want to investigate...
    # https://github.com/elitheeli/sinatra-index/blob/master/lib/sinatra-index.rb
    send_file html_path
  else
    render_template(extensionless_path)
  end
end