Class: Lotus::Assets::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/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'.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.

Since:

  • 0.1.0

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

Since:

  • 0.1.0

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

Since:

  • 0.1.0

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

Since:

  • 0.1.0

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

Since:

  • 0.1.0

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

Since:

  • 0.1.0

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

Since:

  • 0.1.0

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

Since:

  • 0.1.0

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

Since:

  • 0.1.0

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

Since:

  • 0.1.0

'443'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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



91
92
93
# File 'lib/lotus/assets/configuration.rb', line 91

def initialize
  reset!
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



129
130
131
132
133
134
135
# File 'lib/lotus/assets/configuration.rb', line 129

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



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

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

#digest_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.1.0



83
84
85
# File 'lib/lotus/assets/configuration.rb', line 83

def digest_manifest
  @digest_manifest
end

#host(value = nil) ⇒ Object

URL host for the application

This is used to generate absolute URL from helpers.

Since:

  • 0.1.0



253
254
255
256
257
258
259
# File 'lib/lotus/assets/configuration.rb', line 253

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 'lotus/assets'

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

Custom Compressor

require 'lotus/assets'

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

Parameters:

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

    the compressor

See Also:

Since:

  • 0.1.0



180
181
182
183
184
185
186
# File 'lib/lotus/assets/configuration.rb', line 180

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



323
324
325
326
327
328
329
# File 'lib/lotus/assets/configuration.rb', line 323

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

#port(value = nil) ⇒ Object

URL port for the application

This is used to generate absolute URL from helpers.

Since:

  • 0.1.0



266
267
268
269
270
271
272
# File 'lib/lotus/assets/configuration.rb', line 266

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



279
280
281
282
283
284
285
# File 'lib/lotus/assets/configuration.rb', line 279

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



302
303
304
305
306
307
308
# File 'lib/lotus/assets/configuration.rb', line 302

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

#root(value = nil) ⇒ Object

Sources root

Since:

  • 0.1.0



290
291
292
293
294
295
296
297
# File 'lib/lotus/assets/configuration.rb', line 290

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



240
241
242
243
244
245
246
# File 'lib/lotus/assets/configuration.rb', line 240

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



343
344
345
# File 'lib/lotus/assets/configuration.rb', line 343

def sources
  @sources ||= Lotus::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>sass</tt> gem

Examples:

YUI Compressor

require 'lotus/assets'

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

Custom Compressor

require 'lotus/assets'

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

Parameters:

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

    the compressor

See Also:

Since:

  • 0.1.0



227
228
229
230
231
232
233
# File 'lib/lotus/assets/configuration.rb', line 227

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

Class Method Details

.for(base) ⇒ Lotus::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 Lotus::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



74
75
76
77
78
79
# File 'lib/lotus/assets/configuration.rb', line 74

def self.for(base)
  # TODO this implementation is similar to Lotus::Controller::Configuration consider to extract it into Lotus::Utils
  namespace = Utils::String.new(base).namespace
  framework = Utils::Class.load_from_pattern!("(#{namespace}|Lotus)::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



367
368
369
370
371
# File 'lib/lotus/assets/configuration.rb', line 367

def asset_path(source)
  cdn ?
    asset_url(source) :
    compile_path(source)
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



377
378
379
# File 'lib/lotus/assets/configuration.rb', line 377

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

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



410
411
412
413
# File 'lib/lotus/assets/configuration.rb', line 410

def css_compressor
  require 'lotus/assets/compressors/stylesheet'
  Lotus::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



316
317
318
# File 'lib/lotus/assets/configuration.rb', line 316

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

#digest(value = nil) ⇒ Object

Digest mode

Determine if the helpers should generate the digest path for an asset. Usually this is turned on in production mode.

Since:

  • 0.1.0



115
116
117
118
119
120
121
# File 'lib/lotus/assets/configuration.rb', line 115

def digest(value = nil)
  if value.nil?
    @digest
  else
    @digest = value
  end
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



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/lotus/assets/configuration.rb', line 417

def duplicate
  Configuration.new.tap do |c|
    c.root                  = root
    c.scheme                = scheme
    c.host                  = host
    c.port                  = port
    c.prefix                = prefix
    c.cdn                   = cdn
    c.compile               = compile
    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



351
352
353
# File 'lib/lotus/assets/configuration.rb', line 351

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



359
360
361
# File 'lib/lotus/assets/configuration.rb', line 359

def find(file)
  @sources.find(file)
end

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



393
394
395
396
# File 'lib/lotus/assets/configuration.rb', line 393

def js_compressor
  require 'lotus/assets/compressors/javascript'
  Lotus::Assets::Compressors::Javascript.for(javascript_compressor)
end

#load!Object

Load the configuration

This MUST be executed before to accept the first HTTP request

Since:

  • 0.1.0



460
461
462
463
464
465
466
467
468
469
# File 'lib/lotus/assets/configuration.rb', line 460

def load!
  if digest && manifest_path.exist?
    @digest_manifest = Config::DigestManifest.new(
      JSON.load(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



335
336
337
# File 'lib/lotus/assets/configuration.rb', line 335

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



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/lotus/assets/configuration.rb', line 436

def reset!
  @scheme                = DEFAULT_SCHEME
  @host                  = DEFAULT_HOST
  @port                  = DEFAULT_PORT

  @prefix                = Utils::PathPrefix.new(DEFAULT_PREFIX)
  @cdn                   = false
  @compile               = false
  @destination_directory = nil
  @digest_manifest       = Config::NullDigestManifest.new(self)

  @javascript_compressor = nil
  @stylesheet_compressor = nil

  root             Dir.pwd
  public_directory root.join(DEFAULT_PUBLIC_DIRECTORY)
  manifest         DEFAULT_MANIFEST
end