Class: Blossom::Application
- Inherits:
-
Rack::Builder
- Object
- Rack::Builder
- Blossom::Application
- Defined in:
- lib/blossom.rb
Defined Under Namespace
Classes: Configuration
Instance Method Summary collapse
- #build_rack! ⇒ Object
- #coffee_options ⇒ Object
-
#compass_options ⇒ Object
——————————————————–.
-
#configuration_filename ⇒ Object
——————————————————–.
- #configuration_hash ⇒ Object
-
#configure! ⇒ Object
——————————————————–.
- #custom_sinatra_code ⇒ Object
-
#determine_name! ⇒ Object
——————————————————–.
- #filename(*components) ⇒ Object
- #glob(glob) ⇒ Object
- #haml_options ⇒ Object
-
#initialize(root) ⇒ Application
constructor
A new instance of Application.
- #load_configuration! ⇒ Object
- #public_dirname ⇒ Object
- #sass_options ⇒ Object
- #sinatra_app ⇒ Object
- #sinatra_code_filename ⇒ Object
- #use_rack_coffee! ⇒ Object
- #use_rack_normalize_domain! ⇒ Object
Constructor Details
#initialize(root) ⇒ Application
Returns a new instance of Application.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/blossom.rb', line 33 def initialize(root) super() @root = root determine_name! load_configuration! configure! build_rack! end |
Instance Method Details
#build_rack! ⇒ Object
127 128 129 130 131 |
# File 'lib/blossom.rb', line 127 def build_rack! use_rack_normalize_domain! use_rack_coffee! run sinatra_app end |
#coffee_options ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/blossom.rb', line 57 def return \ :cache => @config.cache_content?, :static => false, :ttl => @config.content_max_age, :urls => "/" end |
#compass_options ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/blossom.rb', line 46 def return \ :cache_dir => "tmp/sass-cache", :http_images_path => "/", :images_dir => @config.public_directory, :line_comments => false, :output_style => :compact, :project_path => @root, :sass_dir => "" end |
#configuration_filename ⇒ Object
105 106 |
# File 'lib/blossom.rb', line 105 def configuration_filename filename("#@name.blossom") end |
#configuration_hash ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/blossom.rb', line 93 def configuration_hash case result = YAML.load_file(configuration_filename) when false then {} # Empty file. when Hash then result else Blossom.fail "Bad configuration file: #{configuration_filename}" end rescue Errno::ENOENT {} end |
#configure! ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/blossom.rb', line 119 def configure! .each do |key, value| Compass.configuration.send("#{key}=", value) end Compass.configure_sass_plugin! end |
#custom_sinatra_code ⇒ Object
214 215 216 |
# File 'lib/blossom.rb', line 214 def custom_sinatra_code File.read(sinatra_code_filename) rescue nil end |
#determine_name! ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/blossom.rb', line 77 def determine_name! names = glob("*.blossom") case names.size when 0 Blossom.fail "Missing configuration file: NAME.blossom" when 1 @name = names[0].sub(/\.blossom$/, '') else Blossom.fail "Multiple configuration files: #{names * ', '}" end end |
#filename(*components) ⇒ Object
114 115 |
# File 'lib/blossom.rb', line 114 def filename(*components) File.join(@root, *components) end |
#glob(glob) ⇒ Object
112 113 |
# File 'lib/blossom.rb', line 112 def glob(glob) Dir[filename(glob)].map { |name| File.basename(name) } end |
#haml_options ⇒ Object
65 66 67 68 69 |
# File 'lib/blossom.rb', line 65 def return \ :format => :html5, :attr_wrapper => '"' end |
#load_configuration! ⇒ Object
89 90 91 |
# File 'lib/blossom.rb', line 89 def load_configuration! @config = Configuration.new(configuration_hash) end |
#public_dirname ⇒ Object
109 110 |
# File 'lib/blossom.rb', line 109 def public_dirname filename(@config.public_directory) end |
#sass_options ⇒ Object
71 72 73 |
# File 'lib/blossom.rb', line 71 def Compass. end |
#sinatra_app ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/blossom.rb', line 151 def sinatra_app app = Sinatra.new app.set :blossom, self app.set :root, @root app.set :index, @name.to_sym app.set :views, @root app.set :public_folder, public_dirname app.set :haml, app.set :sass, if custom_sinatra_code app.class_eval(custom_sinatra_code) end # Need variable here for lexical scoping. max_age = @config.max_age app.before { cache_control :max_age => max_age } app.register do def path_exists? suffix condition do basename = File.basename(request.path_info) File.exist? File.join(settings.root, "#{basename}.#{suffix}") end end def file_exists? *suffixes condition do suffix.respond_to? :any? or suffix = [suffix] suffix.any? { basename = File.basename(request.path_info) = basename.sub(/\.[^.]*$/, '') File.exist? File.join(settings.root, "#{}.#{suffix}") } end end end @config.public_extensions.each do |extension| app.get "/:name.#{extension}", :file_exists? => extension do send_file "#{params[:name]}.#{extension}" end end app.get "/:name.css", :file_exists? => [:scss, :sass] do content_type :css sass params[:name].to_sym end app.get "/:name", :path_exists? => :haml do haml params[:name].to_sym end app.get "/" do haml settings.index end app end |
#sinatra_code_filename ⇒ Object
107 108 |
# File 'lib/blossom.rb', line 107 def sinatra_code_filename filename("#@name.sinatra.rb") end |