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(file, from) ⇒ Object
Returns the local file for a given URI path.
-
#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
177 178 179 |
# File 'lib/sinatra/assetpack/options.rb', line 177 def served @served end |
Instance Method Details
#build!(&blk) ⇒ Object
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 |
# File 'lib/sinatra/assetpack/options.rb', line 179 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.
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/sinatra/assetpack/options.rb', line 206 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
168 169 170 171 172 |
# File 'lib/sinatra/assetpack/options.rb', line 168 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(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
240 241 242 243 244 245 246 247 248 |
# File 'lib/sinatra/assetpack/options.rb', line 240 def dyn_local_file_for(file, from) # Remove extension file = $1 if file =~ /^(.*)(\.[^\.]+)$/ # Remove cache-buster (/js/app.28389.js => /js/app) file = $1 if file =~ /^(.*)\.[0-9]+$/ Dir[File.join(app.root, from, "#{file}.*")].first end |
#files(match = nil) ⇒ Object
Returns the files as a hash.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/sinatra/assetpack/options.rb', line 266 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.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/sinatra/assetpack/options.rb', line 290 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]] } HashArray[*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
162 163 164 165 166 |
# File 'lib/sinatra/assetpack/options.rb', line 162 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.
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/sinatra/assetpack/options.rb', line 224 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
218 219 220 |
# File 'lib/sinatra/assetpack/options.rb', line 218 def served?(path) !! local_file_for(path) end |
#write(path, output, mtime = nil) {|path| ... } ⇒ Object
Writes ‘public/#path` based on contents of `output`.
251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/sinatra/assetpack/options.rb', line 251 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, 'w') { |f| f.write output } if mtime File.utime mtime, mtime, path end end |