Class: Sinatra::AssetPack::Options
- Inherits:
-
Object
- Object
- Sinatra::AssetPack::Options
- Includes:
- Configurator
- Defined in:
- lib/sinatra/assetpack/options.rb
Overview
Assets.
Common usage
SinatraApp.assets {
# dsl stuff here
}
a = SinatraApp.assets
Getting options:
a.js_compression
a.output_path
Served:
a.served # { '/js' => '/var/www/project/app/js', ... }
# (URL path => local path)
Packages:
a.packages # { 'app.css' => #<Package>, ... }
# (name.type => package instance)
Build:
a.build! { |path| puts "Building #{path}" }
Lookup:
a.local_path_for('/images/bg.gif')
a.served?('/images/bg.gif')
a.glob(['/js/*.js', '/js/vendor/**/*.js'])
# Returns a HashArray of (local => remote)
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Sinatra::Base instance.
-
#packages ⇒ Object
readonly
Hash, keys are “foo.js”, values are Packages.
-
#served ⇒ Object
readonly
Stuff.
Instance Method Summary collapse
- #build!(&blk) ⇒ Object
-
#cache!(&blk) ⇒ Object
Caches the packages.
-
#clear_ignores! ⇒ Object
Makes nothing ignored.
-
#css(name, *args) ⇒ Object
Adds some CSS packages.
- #css_compression(name = nil, options = nil) ⇒ Object
-
#dyn_local_file_for(requested_file, from) ⇒ Object
Returns the local file for a given URI path.
- #expires(*args) ⇒ Object
-
#files(match = nil) ⇒ Object
Returns the files as a hash.
-
#glob(match, options = {}) ⇒ Object
Returns an array of URI paths of those matching given globs.
-
#ignore(spec) ⇒ Object
Ignores a given path spec.
-
#ignored?(fn) ⇒ Boolean
Checks if a given path is ignored.
-
#initialize(app, &blk) ⇒ Options
constructor
A new instance of Options.
-
#js(name, *args) ⇒ Object
Adds some JS packages.
- #js_compression(name = nil, options = nil) ⇒ Object
- #js_or_css(type, name, *args) ⇒ Object
-
#local_file_for(path) ⇒ Object
Returns the local file for a given URI path.
-
#reset! ⇒ Object
Undo defaults.
-
#serve(path, options = {}) ⇒ Object
DSL methods.
- #served?(path) ⇒ Boolean
-
#write(path, output, mtime = nil) {|path| ... } ⇒ Object
Writes ‘public/#path` based on contents of `output`.
Methods included from Configurator
Constructor Details
#initialize(app, &blk) ⇒ Options
Returns a new instance of Options.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/sinatra/assetpack/options.rb', line 43 def initialize(app, &blk) unless app.root? raise Error, "Please set :root in your Sinatra app." end @app = app @js_compression = :jsmin @css_compression = :simple begin @output_path = app.public rescue NoMethodError @output_path = app.public_folder end @js_compression_options = Hash.new @css_compression_options = Hash.new @ignored = Array.new reset! # Defaults! serve '/css', :from => 'app/css' serve '/js', :from => 'app/js' serve '/images', :from => 'app/images' ignore '.*' ignore '_*' blk.arity <= 0 ? instance_eval(&blk) : yield(self) if block_given? end |
Instance Attribute Details
#app ⇒ Object (readonly)
Sinatra::Base instance
148 149 150 |
# File 'lib/sinatra/assetpack/options.rb', line 148 def app @app end |
#packages ⇒ Object (readonly)
Hash, keys are “foo.js”, values are Packages
149 150 151 |
# File 'lib/sinatra/assetpack/options.rb', line 149 def packages @packages end |
#served ⇒ Object (readonly)
Stuff
186 187 188 |
# File 'lib/sinatra/assetpack/options.rb', line 186 def served @served end |
Instance Method Details
#build!(&blk) ⇒ Object
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/sinatra/assetpack/options.rb', line 188 def build!(&blk) session = Rack::Test::Session.new app get = lambda { |path| response = session.get(path) out = response.body mtime = Time.parse(response.headers['Last-Modified']) if response.headers['Last-Modified'] [ out, mtime ] } packages.each { |_, pack| out, mtime = get[pack.path] write pack.path, out, mtime, &blk write pack.production_path, out, mtime, &blk } files.each { |path, local| out, mtime = get[path] write path, out, mtime, &blk write BusterHelpers.add_cache_buster(path, local), out, mtime, &blk } end |
#cache!(&blk) ⇒ Object
Caches the packages.
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/sinatra/assetpack/options.rb', line 215 def cache!(&blk) return false if app.reload_templates session = Rack::Test::Session.new app packages.each { |_, pack| yield pack.path if block_given? session.get(pack.path) } true end |
#clear_ignores! ⇒ Object
Makes nothing ignored. Use this if you don’t want to ignore dotfiles and underscore files.
104 105 106 |
# File 'lib/sinatra/assetpack/options.rb', line 104 def clear_ignores! @ignored = Array.new end |
#css(name, *args) ⇒ Object
Adds some CSS packages.
css :app, [ '/css/screen.css', ... ]
css :app, '/css/app.css', [ '/css/screen.css', ... ]
127 128 129 |
# File 'lib/sinatra/assetpack/options.rb', line 127 def css(name, *args) #path, files=[]) js_or_css :css, name, *args end |
#css_compression(name = nil, options = nil) ⇒ Object
177 178 179 180 181 |
# File 'lib/sinatra/assetpack/options.rb', line 177 def css_compression(name=nil, =nil) @css_compression = name unless name.nil? @css_compression_options = if .is_a?(Hash) @css_compression end |
#dyn_local_file_for(requested_file, from) ⇒ Object
Returns the local file for a given URI path. (for dynamic files) Returns nil if a file is not found. TODO: consolidate with local_file_for
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/sinatra/assetpack/options.rb', line 249 def dyn_local_file_for(requested_file, from) file = requested_file extension = File.extname(requested_file) # Remove extension file.gsub!(/#{extension}$/, "") # Remove cache-buster (/js/app.28389 => /js/app) file.gsub!(/\.[0-9]+$/, "") matches = Dir[File.join(app.root, from, "#{file}.*")] # Fix for filenames with dots (can't do this with glob) matches.select! { |f| f =~ /#{file}\.[^.]+$/ } # Sort static file match, weighting exact file extension matches matches.sort! do |f, _| (File.basename(f) == "#{file}#{extension}" || File.extname(f) == extension) ? -1 : 1 end matches.first end |
#expires(*args) ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/sinatra/assetpack/options.rb', line 163 def expires(*args) if args.empty? @expires else @expires = args end end |
#files(match = nil) ⇒ Object
Returns the files as a hash.
284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/sinatra/assetpack/options.rb', line 284 def files(match=nil) # All # A buncha tuples tuples = @served.map { |prefix, local_path| path = File.(File.join(@app.root, local_path)) spec = File.join(path, '**', '*') Dir[spec].map { |f| [ to_uri(f, prefix, path), f ] unless File.directory?(f) } }.flatten.compact Hash[*tuples] end |
#glob(match, options = {}) ⇒ Object
Returns an array of URI paths of those matching given globs.
glob('spec')
glob(['spec1', 'spec2' ...])
glob('spec', preserve: true)
If ‘preserve` is set to true, it will preserve any specs that are not wildcards that don’t match anything.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/sinatra/assetpack/options.rb', line 308 def glob(match, ={}) match = [*match] # Force array-ness paths = match.map { |spec| if [:preserve] && !spec.include?('*') spec else files.keys.select { |f| File.fnmatch?(spec, f) }.sort end }.flatten paths = paths.uniq tuples = paths.map { |key| [key, files[key]] } Hash[*tuples.flatten] end |
#ignore(spec) ⇒ Object
Ignores a given path spec.
93 94 95 96 97 98 99 100 101 |
# File 'lib/sinatra/assetpack/options.rb', line 93 def ignore(spec) if spec[0] == '/' @ignored << "#{spec}" @ignored << "#{spec}/**" else @ignored << "**/#{spec}" @ignored << "**/#{spec}/**" end end |
#ignored?(fn) ⇒ Boolean
Checks if a given path is ignored.
109 110 111 |
# File 'lib/sinatra/assetpack/options.rb', line 109 def ignored?(fn) @ignored.any? { |spec| File.fnmatch spec, fn } end |
#js(name, *args) ⇒ Object
Adds some JS packages.
js :foo, [ '/js/vendor/jquery.*.js', ... ]
js :foo, '/js/foo.js', [ '/js/vendor/jquery.*.js', ... ]
118 119 120 |
# File 'lib/sinatra/assetpack/options.rb', line 118 def js(name, *args) js_or_css :js, name, *args end |
#js_compression(name = nil, options = nil) ⇒ Object
171 172 173 174 175 |
# File 'lib/sinatra/assetpack/options.rb', line 171 def js_compression(name=nil, =nil) @js_compression = name unless name.nil? @js_compression_options = if .is_a?(Hash) @js_compression end |
#js_or_css(type, name, *args) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/sinatra/assetpack/options.rb', line 131 def js_or_css(type, name, *args) # Account for "css :name, '/path/to/css', [ files ]" if args[0].is_a?(String) && args[1].respond_to?(:each) path, files = args # Account for "css :name, [ files ]" elsif args[0].respond_to?(:each) path = "/assets/#{name}.#{type}" # /assets/foobar.css by default files = args[0] else raise ArgumentError end @packages["#{name}.#{type}"] = Package.new(self, name, type, path, files) end |
#local_file_for(path) ⇒ Object
Returns the local file for a given URI path. Returns nil if a file is not found.
233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/sinatra/assetpack/options.rb', line 233 def local_file_for(path) path = path.squeeze('/') uri, local = served.detect { |uri, local| path[0...uri.size] == uri } if local path = path[uri.size..-1] path = File.join app.root, local, path path if File.exists?(path) end end |
#reset! ⇒ Object
Undo defaults.
87 88 89 90 |
# File 'lib/sinatra/assetpack/options.rb', line 87 def reset! @served = Hash.new @packages = Hash.new end |
#serve(path, options = {}) ⇒ Object
DSL methods
79 80 81 82 83 84 |
# File 'lib/sinatra/assetpack/options.rb', line 79 def serve(path, ={}) raise Error unless [:from] return unless File.directory?(File.join(app.root, [:from])) @served[path] = [:from] end |
#served?(path) ⇒ Boolean
227 228 229 |
# File 'lib/sinatra/assetpack/options.rb', line 227 def served?(path) !! local_file_for(path) end |
#write(path, output, mtime = nil) {|path| ... } ⇒ Object
Writes ‘public/#path` based on contents of `output`.
269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/sinatra/assetpack/options.rb', line 269 def write(path, output, mtime=nil) require 'fileutils' path = File.join(@output_path, path) yield path if block_given? FileUtils.mkdir_p File.dirname(path) File.open(path, 'wb') { |f| f.write output } if mtime File.utime mtime, mtime, path end end |