Class: Hanami::Assets::Configuration
- Inherits:
-
Object
- Object
- Hanami::Assets::Configuration
- Defined in:
- lib/hanami/assets/configuration.rb
Overview
Framework configuration
Constant Summary collapse
- DEFAULT_SCHEME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'http'.freeze
- DEFAULT_HOST =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'localhost'.freeze
- DEFAULT_PORT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'2300'.freeze
- DEFAULT_PUBLIC_DIRECTORY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'public'.freeze
- DEFAULT_MANIFEST =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'assets.json'.freeze
- DEFAULT_PREFIX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'/assets'.freeze
- URL_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'/'.freeze
- HTTP_SCHEME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'http'.freeze
- HTTP_PORT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'80'.freeze
- HTTPS_SCHEME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'https'.freeze
- HTTPS_PORT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'443'.freeze
- DEFAULT_SUBRESOURCE_INTEGRITY_ALGORITHM =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
:sha256
- SUBRESOURCE_INTEGRITY_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
' '.freeze
Instance Attribute Summary collapse
-
#cdn(value = nil) ⇒ Object
readonly
CDN mode.
-
#compile(value = nil) ⇒ Object
readonly
Compile mode.
-
#host(value = nil) ⇒ Object
readonly
URL host for the application.
-
#javascript_compressor(value = nil) ⇒ Object
readonly
JavaScript compressor.
-
#manifest(value = nil) ⇒ Object
readonly
Manifest path from public directory.
-
#nested(value = nil) ⇒ Object
readonly
Support for nested path.
-
#port(value = nil) ⇒ Object
readonly
URL port for the application.
-
#prefix(value = nil) ⇒ Object
readonly
URL port for the application.
-
#public_directory(value = nil) ⇒ Object
readonly
Application public directory.
- #public_manifest ⇒ Object readonly private
-
#root(value = nil) ⇒ Object
readonly
Sources root.
-
#scheme(value = nil) ⇒ Object
readonly
URL scheme for the application.
-
#sources ⇒ Object
readonly
private
Application’s assets sources.
-
#stylesheet_compressor(value = nil) ⇒ Object
readonly
Stylesheet compressor.
-
#subresource_integrity(*values) ⇒ Object
readonly
Subresource integrity mode.
Class Method Summary collapse
-
.for(base) ⇒ Hanami::Assets::Configuration
private
Return a copy of the configuration of the framework instance associated with the given class.
Instance Method Summary collapse
-
#asset_path(source) ⇒ Object
private
Relative URL.
-
#asset_url(source) ⇒ Object
private
Absolute URL.
- #base_directories ⇒ Object
-
#crossorigin?(source) ⇒ Boolean
private
Check if the given source is linked via Cross-Origin policy.
-
#css_compressor ⇒ Hanami::Assets::Compressors::Stylesheet
private
Load Stylesheet compressor.
-
#destination_directory ⇒ Object
private
Destination directory.
- #duplicate ⇒ Object private
-
#files ⇒ Object
private
Application’s assets.
-
#find(file) ⇒ Object
private
Find a file from sources.
-
#fingerprint(value = nil) ⇒ Object
Fingerprint mode.
-
#initialize(&blk) ⇒ Hanami::Assets::Configuration
constructor
private
Return a new instance.
-
#js_compressor ⇒ Hanami::Assets::Compressors::Javascript
private
Load Javascript compressor.
-
#load! ⇒ Object
private
Load the configuration.
-
#manifest_path ⇒ Object
private
Absolute manifest path.
- #reset! ⇒ Object private
- #source(file) ⇒ Object private
-
#subresource_integrity_algorithms ⇒ Object
An array of crypographically secure hashing algorithms to use for generating asset subresource integrity checks.
-
#subresource_integrity_value(source) ⇒ Object
private
Subresource integrity attribute.
Constructor Details
#initialize(&blk) ⇒ Hanami::Assets::Configuration
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a new instance
99 100 101 102 |
# File 'lib/hanami/assets/configuration.rb', line 99 def initialize(&blk) reset! instance_eval(&blk) if block_given? end |
Instance Attribute Details
#cdn(value = nil) ⇒ Object
CDN mode
Determine if the helpers should always generate absolute URL. This is useful in production mode.
165 166 167 168 169 170 171 |
# File 'lib/hanami/assets/configuration.rb', line 165 def cdn(value = nil) if value.nil? @cdn else @cdn = !!value # rubocop:disable Style/DoubleNegation end end |
#compile(value = nil) ⇒ Object
Compile mode
Determine if compile assets from sources to destination. Usually this is turned off in production mode.
110 111 112 113 114 115 116 |
# File 'lib/hanami/assets/configuration.rb', line 110 def compile(value = nil) if value.nil? @compile else @compile = value end end |
#host(value = nil) ⇒ Object
URL host for the application
This is used to generate absolute URL from helpers.
289 290 291 292 293 294 295 |
# File 'lib/hanami/assets/configuration.rb', line 289 def host(value = nil) if value.nil? @host else @host = value end end |
#javascript_compressor(value = nil) ⇒ Object
JavaScript compressor
Determine which compressor to use for JavaScript files during deploy.
By default it’s nil
, that means it doesn’t compress JavaScripts at deploy time.
It accepts a Symbol
or an object that respond to #compress(file)
.
The following symbols are accepted:
* <tt>:builtin</tt> - Ruby based implementation of jsmin. It doesn't require any external gem.
* <tt>:yui</tt> - YUI Compressor, it depends on <tt>yui-compressor</tt> gem and it requires Java 1.4+
* <tt>:uglifier</tt> - UglifyJS, it depends on <tt>uglifier</tt> gem and it requires Node.js
* <tt>:closure</tt> - Google Closure Compiler, it depends on <tt>closure-compiler</tt> gem and it requires Java
216 217 218 219 220 221 222 |
# File 'lib/hanami/assets/configuration.rb', line 216 def javascript_compressor(value = nil) if value.nil? @javascript_compressor else @javascript_compressor = value end end |
#manifest(value = nil) ⇒ Object
Manifest path from public directory
359 360 361 362 363 364 365 |
# File 'lib/hanami/assets/configuration.rb', line 359 def manifest(value = nil) if value.nil? @manifest else @manifest = value.to_s end end |
#nested(value = nil) ⇒ Object
Support for nested path
135 136 137 138 139 140 141 |
# File 'lib/hanami/assets/configuration.rb', line 135 def nested(value = nil) if value.nil? @nested else @nested = !!value # rubocop:disable Style/DoubleNegation end end |
#port(value = nil) ⇒ Object
URL port for the application
This is used to generate absolute URL from helpers.
302 303 304 305 306 307 308 |
# File 'lib/hanami/assets/configuration.rb', line 302 def port(value = nil) if value.nil? @port else @port = value.to_s end end |
#prefix(value = nil) ⇒ Object
URL port for the application
This is used to generate absolute or relative URL from helpers.
315 316 317 318 319 320 321 |
# File 'lib/hanami/assets/configuration.rb', line 315 def prefix(value = nil) if value.nil? @prefix else @prefix = Utils::PathPrefix.new(value) end end |
#public_directory(value = nil) ⇒ Object
Application public directory
338 339 340 341 342 343 344 |
# File 'lib/hanami/assets/configuration.rb', line 338 def public_directory(value = nil) if value.nil? @public_directory else @public_directory = Pathname.new(::File.(value)) end end |
#public_manifest ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
91 92 93 |
# File 'lib/hanami/assets/configuration.rb', line 91 def public_manifest @public_manifest end |
#root(value = nil) ⇒ Object
Sources root
326 327 328 329 330 331 332 333 |
# File 'lib/hanami/assets/configuration.rb', line 326 def root(value = nil) if value.nil? @root else @root = Pathname.new(value).realpath sources.root = @root end end |
#scheme(value = nil) ⇒ Object
URL scheme for the application
This is used to generate absolute URL from helpers.
276 277 278 279 280 281 282 |
# File 'lib/hanami/assets/configuration.rb', line 276 def scheme(value = nil) if value.nil? @scheme else @scheme = value end end |
#sources ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Application’s assets sources
379 380 381 |
# File 'lib/hanami/assets/configuration.rb', line 379 def sources @sources ||= Hanami::Assets::Config::Sources.new(root) end |
#stylesheet_compressor(value = nil) ⇒ Object
Stylesheet compressor
Determine which compressor to use for Stylesheet files during deploy.
By default it’s nil
, that means it doesn’t compress Stylesheets at deploy time.
It accepts a Symbol
or an object that respond to #compress(file)
.
The following symbols are accepted:
* <tt>:builtin</tt> - Ruby based compressor. It doesn't require any external gem. It's fast, but not an efficient compressor.
* <tt>:yui</tt> - YUI-Compressor, it depends on <tt>yui-compressor</tt> gem and requires Java 1.4+
* <tt>:sass</tt> - Sass, it depends on <tt>sassc</tt> gem
263 264 265 266 267 268 269 |
# File 'lib/hanami/assets/configuration.rb', line 263 def stylesheet_compressor(value = nil) if value.nil? @stylesheet_compressor else @stylesheet_compressor = value end end |
#subresource_integrity(*values) ⇒ Object
Subresource integrity mode
Determine if the helpers should generate the integrity attribute for an asset. Usually this is turned on in production mode.
149 150 151 152 153 154 155 156 157 |
# File 'lib/hanami/assets/configuration.rb', line 149 def subresource_integrity(*values) if values.empty? @subresource_integrity elsif values.length == 1 @subresource_integrity = values.first else @subresource_integrity = values end end |
Class Method Details
.for(base) ⇒ Hanami::Assets::Configuration
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a copy of the configuration of the framework instance associated with the given class.
When multiple instances of Hanami::Assets are used in the same application, we want to make sure that a controller or an action will receive the expected configuration.
82 83 84 85 86 87 |
# File 'lib/hanami/assets/configuration.rb', line 82 def self.for(base) # TODO: this implementation is similar to Hanami::Controller::Configuration consider to extract it into Hanami::Utils namespace = Utils::String.namespace(base) framework = Utils::Class.load("#{namespace}::Assets") || Utils::Class.load!('Hanami::Assets') framework.configuration end |
Instance Method Details
#asset_path(source) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Relative URL
420 421 422 423 424 425 426 |
# File 'lib/hanami/assets/configuration.rb', line 420 def asset_path(source) if cdn asset_url(source) else compile_path(source) end end |
#asset_url(source) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Absolute URL
432 433 434 |
# File 'lib/hanami/assets/configuration.rb', line 432 def asset_url(source) "#{@base_url}#{compile_path(source)}" end |
#base_directories ⇒ Object
399 400 401 402 403 404 405 406 |
# File 'lib/hanami/assets/configuration.rb', line 399 def base_directories @base_directories ||= %w[ stylesheets javascripts images fonts ] end |
#crossorigin?(source) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the given source is linked via Cross-Origin policy. In other words, the given source, doesn’t satisfy the Same-Origin policy.
444 445 446 |
# File 'lib/hanami/assets/configuration.rb', line 444 def crossorigin?(source) !source.start_with?(@base_url) end |
#css_compressor ⇒ Hanami::Assets::Compressors::Stylesheet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load Stylesheet compressor
503 504 505 506 |
# File 'lib/hanami/assets/configuration.rb', line 503 def css_compressor require 'hanami/assets/compressors/stylesheet' Hanami::Assets::Compressors::Stylesheet.for(stylesheet_compressor) end |
#destination_directory ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Destination directory
It’s the combination of public_directory
and prefix
.
352 353 354 |
# File 'lib/hanami/assets/configuration.rb', line 352 def destination_directory @destination_directory ||= public_directory.join(*prefix.split(URL_SEPARATOR)) end |
#duplicate ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/hanami/assets/configuration.rb', line 510 def duplicate # rubocop:disable Metrics/AbcSize, Metrics/MethodLength Configuration.new.tap do |c| c.root = root c.scheme = scheme c.host = host c.port = port c.prefix = prefix c.subresource_integrity = subresource_integrity c.cdn = cdn c.compile = compile c.nested = nested c.public_directory = public_directory c.manifest = manifest c.sources = sources.dup c.javascript_compressor = javascript_compressor c.stylesheet_compressor = stylesheet_compressor end end |
#files ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Application’s assets
387 388 389 |
# File 'lib/hanami/assets/configuration.rb', line 387 def files sources.files end |
#find(file) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find a file from sources
412 413 414 |
# File 'lib/hanami/assets/configuration.rb', line 412 def find(file) @sources.find(file) end |
#fingerprint(value = nil) ⇒ Object
Fingerprint mode
Determine if the helpers should generate the fingerprinted path for an asset. Usually this is turned on in production mode.
124 125 126 127 128 129 130 |
# File 'lib/hanami/assets/configuration.rb', line 124 def fingerprint(value = nil) if value.nil? @fingerprint else @fingerprint = value end end |
#js_compressor ⇒ Hanami::Assets::Compressors::Javascript
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load Javascript compressor
486 487 488 489 |
# File 'lib/hanami/assets/configuration.rb', line 486 def js_compressor require 'hanami/assets/compressors/javascript' Hanami::Assets::Compressors::Javascript.for(javascript_compressor) end |
#load! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load the configuration
This MUST be executed before to accept the first HTTP request
560 561 562 563 564 565 566 567 568 569 |
# File 'lib/hanami/assets/configuration.rb', line 560 def load! if (fingerprint || subresource_integrity) && manifest_path.exist? @public_manifest = Config::Manifest.new( JSON.parse(manifest_path.read), manifest_path ) end @base_url = URI::Generic.build(scheme: scheme, host: host, port: url_port).to_s end |
#manifest_path ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Absolute manifest path
371 372 373 |
# File 'lib/hanami/assets/configuration.rb', line 371 def manifest_path public_directory.join(manifest) end |
#reset! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/hanami/assets/configuration.rb', line 531 def reset! # rubocop:disable Metrics/AbcSize, Metrics/MethodLength @scheme = DEFAULT_SCHEME @host = DEFAULT_HOST @port = DEFAULT_PORT @prefix = Utils::PathPrefix.new(DEFAULT_PREFIX) @subresource_integrity = false @cdn = false @fingerprint = false @compile = false @nested = false @base_url = nil @destination_directory = nil @public_manifest = Config::NullManifest.new(self) @javascript_compressor = nil @stylesheet_compressor = nil root Dir.pwd public_directory root.join(DEFAULT_PUBLIC_DIRECTORY) manifest DEFAULT_MANIFEST end |
#source(file) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
393 394 395 396 |
# File 'lib/hanami/assets/configuration.rb', line 393 def source(file) pathname = Pathname.new(file) pathname.absolute? ? pathname : find(file) end |
#subresource_integrity_algorithms ⇒ Object
An array of crypographically secure hashing algorithms to use for generating asset subresource integrity checks
452 453 454 455 456 457 458 459 460 |
# File 'lib/hanami/assets/configuration.rb', line 452 def subresource_integrity_algorithms if @subresource_integrity == true [DEFAULT_SUBRESOURCE_INTEGRITY_ALGORITHM] else # Using Array() allows us to accept Array or Symbol, and '|| nil' lets # us return an empty array when @subresource_integrity is `false` Array(@subresource_integrity || nil) end end |
#subresource_integrity_value(source) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Subresource integrity attribute
466 467 468 469 470 471 472 |
# File 'lib/hanami/assets/configuration.rb', line 466 def subresource_integrity_value(source) return unless subresource_integrity public_manifest.subresource_integrity_values( prefix.join(source) ).join(SUBRESOURCE_INTEGRITY_SEPARATOR) end |