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.



25
26
27
28
29
30
31
32
33
34
# File 'lib/blossom.rb', line 25

def initialize(root)
  super()

  @root = root

  determine_name!
  load_configuration!
  configure!
  build_rack!
end

Instance Method Details

#build_rack!Object



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

def build_rack!
  run sinatra_app
end

#compass_optionsObject




38
39
40
41
42
43
44
45
46
47
# File 'lib/blossom.rb', line 38

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          => Dir.pwd
end

#configuration_filenameObject




87
88
# File 'lib/blossom.rb', line 87

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

#configuration_hashObject



75
76
77
78
79
80
81
82
83
# File 'lib/blossom.rb', line 75

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




101
102
103
104
105
106
107
# File 'lib/blossom.rb', line 101

def configure!
  compass_options.each do |key, value|
    Compass.configuration.send("#{key}=", value)
  end

  Compass.configure_sass_plugin!
end

#custom_sinatra_codeObject



179
180
181
# File 'lib/blossom.rb', line 179

def custom_sinatra_code
  File.read(sinatra_code_filename) rescue nil
end

#determine_name!Object




59
60
61
62
63
64
65
66
67
68
69
# File 'lib/blossom.rb', line 59

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



96
97
# File 'lib/blossom.rb', line 96

def filename(*components)
File.join(@root, *components) end

#glob(glob) ⇒ Object



94
95
# File 'lib/blossom.rb', line 94

def glob(glob)
Dir[filename(glob)].map { |name| File.basename(name) } end

#haml_optionsObject



49
50
51
# File 'lib/blossom.rb', line 49

def haml_options
  return :format => :html5, :attr_wrapper => '"'
end

#load_configuration!Object



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

def load_configuration!
  @config = Configuration.new(configuration_hash)
end

#public_dirnameObject



91
92
# File 'lib/blossom.rb', line 91

def public_dirname
filename(@config.public_directory) end

#sass_optionsObject



53
54
55
# File 'lib/blossom.rb', line 53

def sass_options
  Compass.sass_engine_options
end

#sinatra_appObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
# File 'lib/blossom.rb', line 113

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
  app.set :scss, 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 {
        basename = File.basename(request.path_info)
        File.exist? File.join(settings.root, "#{basename}.#{suffix}")
      }
    end

    def file_exists? suffix
      condition {
        basename = File.basename(request.path_info)
        barename = basename.sub(/\.[^.]*$/, '')
        File.exist? File.join(settings.root, "#{barename}.#{suffix}")
      }
    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 do
    content_type :css
    scss params[:name].to_sym
  end

  app.get "/:name.css", :file_exists? => :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



89
90
# File 'lib/blossom.rb', line 89

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