Class: Blossom::Application

Inherits:
Rack::Builder
  • Object
show all
Defined in:
lib/blossom.rb

Defined Under Namespace

Classes: Configuration

Instance Method Summary collapse

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_optionsObject



57
58
59
60
61
62
63
# File 'lib/blossom.rb', line 57

def coffee_options
  return \
    :cache   => @config.cache_content?,
    :static  => false,
    :ttl     => @config.content_max_age,
    :urls    => "/"
end

#compass_optionsObject




46
47
48
49
50
51
52
53
54
55
# File 'lib/blossom.rb', line 46

def compass_options
  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_filenameObject




105
106
# File 'lib/blossom.rb', line 105

def configuration_filename
filename("#@name.blossom") end

#configuration_hashObject



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!
  compass_options.each do |key, value|
    Compass.configuration.send("#{key}=", value)
  end

  Compass.configure_sass_plugin!
end

#custom_sinatra_codeObject



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_optionsObject



65
66
67
68
69
# File 'lib/blossom.rb', line 65

def haml_options
  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_dirnameObject



109
110
# File 'lib/blossom.rb', line 109

def public_dirname
filename(@config.public_directory) end

#sass_optionsObject



71
72
73
# File 'lib/blossom.rb', line 71

def sass_options
  Compass.sass_engine_options
end

#sinatra_appObject



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, haml_options
  app.set :sass, sass_options

  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)
          barename = basename.sub(/\.[^.]*$/, '')
          File.exist? File.join(settings.root, "#{barename}.#{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_filenameObject



107
108
# File 'lib/blossom.rb', line 107

def sinatra_code_filename
filename("#@name.sinatra.rb") end

#use_rack_coffee!Object



142
143
144
145
146
147
148
149
# File 'lib/blossom.rb', line 142

def use_rack_coffee!
  if defined? Rack::Coffee
    use Rack::Coffee, coffee_options
    Blossom.info "Using CoffeeScript."
  else
    Blossom.info "Not using CoffeeScript."
  end
end

#use_rack_normalize_domain!Object



133
134
135
136
137
138
139
140
# File 'lib/blossom.rb', line 133

def use_rack_normalize_domain!
  if @config.strip_www?
    use Rack::NormalizeDomain
    Blossom.info "Normalizing domains by removing initial www."
  else
    Blossom.info "Not normalizing domains."
  end
end