Class: CabezaDeTermo::Assets::Library

Inherits:
Object
  • Object
show all
Defined in:
lib/cabeza-de-termo/assets/library.rb,
lib/cabeza-de-termo/assets/version.rb

Overview

The entry point to the assets library defintion and recollection. To define asset bundles, declare the following in your application:

require ‘cabeza-de-termo/assets/library’

module Web

class Application < Hanami::Application
  configure do
  	...

CabezaDeTermo::Assets::Library.definition do # Css bundle :‘bootstrap-css’ do include ‘/vendor/bootstrap/css/bootstrap.min.css’ end

# Js bundle :jquery do include ‘/vendor/jquery/jquery-1.11.3.min.js’ end

bundle :‘bootstrap-js’ do require :jquery include ‘/vendor/bootstrap/js/bootstrap.min.js’ end end

  	...
  end
end

end

To define which assets to include for a view, you must define a class that delegates the assets recollection to the actual view and layout. A example would be something like this:

class RenderingScopeAdaptor def collect_stylesheets_with(assets_collector) @view.collect_stylesheets_with assets_collector end

def collect_javascripts_with(assets_collector) @view.collect_javascripts_with assets_collector end end

See the CabezaDeTermo::Assets::HanamiRenderingScope class for a concrete example.

Finally, in you View or Helper object you must define the methods :collect_stylesheets_with(asset_collector) and :collect_javascripts_with(asset_collector).

class MyView def collect_stylesheets_with(assets_collector) assets_collector.require :‘bootstrap-css’ assets_collector.include ‘landing-page.css’ end

def collect_javascripts_with(assets_collector) assets_collector.require :‘bootstrap-js’ assets_collector.include ‘landing-page.js’ end end

To collect the assets, call this from your template or helper:

<%= CabezaDeTermo::Assets::Library.stylesheets_for rendering_scope %>

Constant Summary collapse

VERSION =
"3.0.0"

Class Method Summary collapse

Class Method Details

.bundle_named(name) ⇒ Object

Answer the bundle named name from the BundlesLibrary. Raise an error if not found



86
87
88
# File 'lib/cabeza-de-termo/assets/library.rb', line 86

def self.bundle_named(name)
	bundles_library.bundle_named name
end

.bundles_libraryObject

Answer the BundlesLibrary singleton



81
82
83
# File 'lib/cabeza-de-termo/assets/library.rb', line 81

def self.bundles_library
	@bundles_library
end

.definition(&block) ⇒ Object

Define the bundles on the BundlesLibrary



91
92
93
# File 'lib/cabeza-de-termo/assets/library.rb', line 91

def self.definition(&block)
	CdT.bind_block_evaluation_to bundles_library, &block
end

.javascripts_for(rendering_scope) ⇒ Object

Collect the javascripts defined for the rendering scope. This method can be called directly from the template like this: <%= CabezaDeTermo::Assets::Library.javascripts_for rendering_scope %> and then you can iterate on the collected javascripts.



107
108
109
# File 'lib/cabeza-de-termo/assets/library.rb', line 107

def self.javascripts_for(rendering_scope)
	newRenderingScopeAssetsCollector.collect_javascripts_from rendering_scope
end

.newRenderingScopeAssetsCollectorObject

Answer a new instance of the collector of stylesheets of a rendering_scope



112
113
114
# File 'lib/cabeza-de-termo/assets/library.rb', line 112

def self.newRenderingScopeAssetsCollector
	RenderingScopeAssetsCollector.new
end

.stylesheets_for(rendering_scope) ⇒ Object

Collect the stylesheets defined for the rendering scope. This method can be called directly from the template like this: <%= CabezaDeTermo::Assets::Library.stylesheets_for rendering_scope %> and then you can iterate on the collected stylesheets.



99
100
101
# File 'lib/cabeza-de-termo/assets/library.rb', line 99

def self.stylesheets_for(rendering_scope)
	newRenderingScopeAssetsCollector.collect_stylesheets_from rendering_scope
end