Class: Hanami::Assets::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/assets/configuration.rb

Overview

Framework configuration

Since:

  • 0.1.0

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.

Since:

  • 0.1.0

"http"
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.

Since:

  • 0.1.0

"localhost"
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.

Since:

  • 0.1.0

"2300"
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.

Since:

  • 0.1.0

"public"
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.

Since:

  • 0.1.0

"assets.json"
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.

Since:

  • 0.1.0

"/assets"
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.

Since:

  • 0.1.0

"/"
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.

Since:

  • 0.1.0

"http"
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.

Since:

  • 0.1.0

"80"
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.

Since:

  • 0.1.0

"https"
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.

Since:

  • 0.1.0

"443"
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.

Since:

  • 0.3.0

: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.

Since:

  • 0.3.0

" "

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Since:

  • 0.1.0



102
103
104
105
# File 'lib/hanami/assets/configuration.rb', line 102

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.

Since:

  • 0.1.0



168
169
170
171
172
173
174
# File 'lib/hanami/assets/configuration.rb', line 168

def cdn(value = nil)
  if value.nil?
    @cdn
  else
    @cdn = !!value
  end
end

#compile(value = nil) ⇒ Object

Compile mode

Determine if compile assets from sources to destination. Usually this is turned off in production mode.

Since:

  • 0.1.0



113
114
115
116
117
118
119
# File 'lib/hanami/assets/configuration.rb', line 113

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.

Since:

  • 0.1.0



294
295
296
297
298
299
300
# File 'lib/hanami/assets/configuration.rb', line 294

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

Examples:

YUI Compressor

require 'hanami/assets'

Hanami::Assets.configure do
  # ...
  javascript_compressor :yui
end.load!

Custom Compressor

require 'hanami/assets'

Hanami::Assets.configure do
  # ...
  javascript_compressor MyCustomJavascriptCompressor.new
end.load!

Parameters:

  • value (Symbol, #compress) (defaults to: nil)

    the compressor

See Also:

Since:

  • 0.1.0



220
221
222
223
224
225
226
# File 'lib/hanami/assets/configuration.rb', line 220

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

Since:

  • 0.1.0



364
365
366
367
368
369
370
# File 'lib/hanami/assets/configuration.rb', line 364

def manifest(value = nil)
  if value.nil?
    @manifest
  else
    @manifest = value.to_s
  end
end

#nested(value = nil) ⇒ Object

Support for nested path

Since:

  • 1.3.1



138
139
140
141
142
143
144
# File 'lib/hanami/assets/configuration.rb', line 138

def nested(value = nil)
  if value.nil?
    @nested
  else
    @nested = !!value
  end
end

#port(value = nil) ⇒ Object

URL port for the application

This is used to generate absolute URL from helpers.

Since:

  • 0.1.0



307
308
309
310
311
312
313
# File 'lib/hanami/assets/configuration.rb', line 307

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.

Since:

  • 0.1.0



320
321
322
323
324
325
326
# File 'lib/hanami/assets/configuration.rb', line 320

def prefix(value = nil)
  if value.nil?
    @prefix
  else
    @prefix = Utils::PathPrefix.new(value)
  end
end

#public_directory(value = nil) ⇒ Object

Application public directory

Since:

  • 0.1.0



343
344
345
346
347
348
349
# File 'lib/hanami/assets/configuration.rb', line 343

def public_directory(value = nil)
  if value.nil?
    @public_directory
  else
    @public_directory = Pathname.new(::File.expand_path(value))
  end
end

#public_manifestObject (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.

Since:

  • 0.4.0



94
95
96
# File 'lib/hanami/assets/configuration.rb', line 94

def public_manifest
  @public_manifest
end

#root(value = nil) ⇒ Object

Sources root

Since:

  • 0.1.0



331
332
333
334
335
336
337
338
# File 'lib/hanami/assets/configuration.rb', line 331

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.

Since:

  • 0.1.0



281
282
283
284
285
286
287
# File 'lib/hanami/assets/configuration.rb', line 281

def scheme(value = nil)
  if value.nil?
    @scheme
  else
    @scheme = value
  end
end

#sourcesObject

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

Since:

  • 0.1.0



384
385
386
# File 'lib/hanami/assets/configuration.rb', line 384

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

Examples:

YUI Compressor

require 'hanami/assets'

Hanami::Assets.configure do
  # ...
  stylesheet_compressor :yui
end.load!

Custom Compressor

require 'hanami/assets'

Hanami::Assets.configure do
  # ...
  stylesheet_compressor MyCustomStylesheetCompressor.new
end.load!

Parameters:

  • value (Symbol, #compress) (defaults to: nil)

    the compressor

See Also:

Since:

  • 0.1.0



268
269
270
271
272
273
274
# File 'lib/hanami/assets/configuration.rb', line 268

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.

Since:

  • 0.3.0



152
153
154
155
156
157
158
159
160
# File 'lib/hanami/assets/configuration.rb', line 152

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.

Parameters:

  • base (Class, Module)

    a controller or an action

Returns:

Since:

  • 0.1.0



84
85
86
87
88
89
90
# File 'lib/hanami/assets/configuration.rb', line 84

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

Since:

  • 0.1.0



425
426
427
428
429
430
431
# File 'lib/hanami/assets/configuration.rb', line 425

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

Since:

  • 0.1.0



437
438
439
# File 'lib/hanami/assets/configuration.rb', line 437

def asset_url(source)
  "#{@base_url}#{compile_path(source)}"
end

#base_directoriesObject

Since:

  • 1.3.0



404
405
406
407
408
409
410
411
# File 'lib/hanami/assets/configuration.rb', line 404

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.



449
450
451
# File 'lib/hanami/assets/configuration.rb', line 449

def crossorigin?(source)
  !source.start_with?(@base_url)
end

#css_compressorHanami::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

Returns:

Raises:

See Also:

Since:

  • 0.1.0



508
509
510
511
# File 'lib/hanami/assets/configuration.rb', line 508

def css_compressor
  require "hanami/assets/compressors/stylesheet"
  Hanami::Assets::Compressors::Stylesheet.for(stylesheet_compressor)
end

#destination_directoryObject

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.

Since:

  • 0.1.0



357
358
359
# File 'lib/hanami/assets/configuration.rb', line 357

def destination_directory
  @destination_directory ||= public_directory.join(*prefix.split(URL_SEPARATOR))
end

#duplicateObject

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.

Since:

  • 0.1.0



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/hanami/assets/configuration.rb', line 515

def duplicate
  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

#filesObject

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

Since:

  • 0.1.0



392
393
394
# File 'lib/hanami/assets/configuration.rb', line 392

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

Since:

  • 0.1.0



417
418
419
# File 'lib/hanami/assets/configuration.rb', line 417

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.

Since:

  • 0.1.0



127
128
129
130
131
132
133
# File 'lib/hanami/assets/configuration.rb', line 127

def fingerprint(value = nil)
  if value.nil?
    @fingerprint
  else
    @fingerprint = value
  end
end

#js_compressorHanami::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

Returns:

Raises:

See Also:

Since:

  • 0.1.0



491
492
493
494
# File 'lib/hanami/assets/configuration.rb', line 491

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

Since:

  • 0.1.0



565
566
567
568
569
570
571
572
573
574
# File 'lib/hanami/assets/configuration.rb', line 565

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_pathObject

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

Since:

  • 0.1.0



376
377
378
# File 'lib/hanami/assets/configuration.rb', line 376

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.

Since:

  • 0.1.0



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/hanami/assets/configuration.rb', line 536

def reset!
  @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.

Since:

  • 0.3.0



398
399
400
401
# File 'lib/hanami/assets/configuration.rb', line 398

def source(file)
  pathname = Pathname.new(file)
  pathname.absolute? ? pathname : find(file)
end

#subresource_integrity_algorithmsObject

An array of crypographically secure hashing algorithms to use for generating asset subresource integrity checks

Since:

  • 0.3.0



457
458
459
460
461
462
463
464
465
# File 'lib/hanami/assets/configuration.rb', line 457

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

Since:

  • 0.3.0



471
472
473
474
475
476
477
# File 'lib/hanami/assets/configuration.rb', line 471

def subresource_integrity_value(source)
  return unless subresource_integrity

  public_manifest.subresource_integrity_values(
    prefix.join(source)
  ).join(SUBRESOURCE_INTEGRITY_SEPARATOR)
end