assets-library-for-hanami 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 681a19e804b492f011ac2bd645e49012f0e45794
4
+ data.tar.gz: cbc4219a3dacd15a6160423132477a16e9667179
5
+ SHA512:
6
+ metadata.gz: cf26e01b0d1a5d73175192f0e0c0e538aa64478b3f6b08029a0e1a57c1fa7909bbc7c90151eb1bc12c90c4c4104e78e771e07c95e2c2b7ff37e82aa0d1850bd0
7
+ data.tar.gz: 4b127255d85db4eeb3f96cbf14230a88910f6bb0be88b2fdd1abeb2a847767af40af707ae9d55c903a73136ce87d71e33080460accc17a59f75dd23dc058fbcd
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+
3
+ # Simplecov test coverage report
4
+ coverage
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec-helper
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - "2.0"
5
+ - "2.1"
6
+ - "2.2"
7
+
8
+ before_install: gem install bundler
9
+
10
+ script: bundle exec rspec
11
+
12
+ git:
13
+ submodules: false
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in assets-library-for-hanami.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec', require: false
7
+ gem 'simplecov', require: false
8
+ gem 'coveralls', require: false
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Martin Rubi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,192 @@
1
+ # CabezaDeTermo::Assets::Library
2
+
3
+ A framework to declare bundles of assets in your application and collect them resolving the dependencies.
4
+ This framework does not publish, compile, nor transform the assets in any way. All it does is resolve the dependencies of the assets and collect them into a collection. This collection of assets can then be included in your template, or passed to another framework to transform or compile the assets.
5
+
6
+ This framework was written to be used in [**Hanami**](http://hanamirb.org/) applications, but it can be used anywhere actually.
7
+
8
+ ## Status
9
+
10
+ [![Gem Version](https://badge.fury.io/rb/assets-library-for-hanami.svg)](https://badge.fury.io/rb/assets-library-for-hanami)
11
+ [![Build Status](https://travis-ci.org/cabeza-de-termo/assets-library-for-hanami.svg?branch=master)](https://travis-ci.org/cabeza-de-termo/assets-library-for-hanami)
12
+ [![Coverage Status](https://coveralls.io/repos/cabeza-de-termo/assets-library-for-hanami/badge.svg?branch=master&service=github)](https://coveralls.io/github/cabeza-de-termo/assets-library-for-hanami?branch=master)
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'assets-library-for-hanami', '~> 3.0'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install assets-library-for-hanami
29
+
30
+ ## Using this framework in an Hanami application
31
+
32
+ To define the assets bundles, require these files in your Hanami application.rb file:
33
+
34
+ ```ruby
35
+ require 'cabeza-de-termo/assets/library'
36
+ require 'cabeza-de-termo/assets/rendering-scope-adaptors/hanami-rendering-scope'
37
+ ```
38
+
39
+ and then add the CabezaDeTermo::Assets::Library.definition to your Application class:
40
+
41
+ ```ruby
42
+ module Web
43
+ class Application < Hanami::Application
44
+ configure do
45
+ ...
46
+
47
+ # Define the asset bundles
48
+ CabezaDeTermo::Assets::Library.definition do
49
+ # Css
50
+ bundle :'bootstrap-css' do
51
+ include '/vendor/bootstrap/css/bootstrap.min.css' # include this asset in the bundle
52
+ end
53
+
54
+ # Js
55
+ bundle :jquery do
56
+ include '/vendor/jquery/jquery-1.11.3.min.js'
57
+ end
58
+
59
+ bundle :'bootstrap-js' do
60
+ require :jquery # declare that this bundle depends on the :jquery bundle
61
+ include '/vendor/bootstrap/js/bootstrap.min.js'
62
+ end
63
+ end
64
+
65
+ ...
66
+ end
67
+ end
68
+ end
69
+ ```
70
+
71
+ To define the assets to be included in the layout, you can add these methods to your Web::Views::ApplicationLayout class:
72
+
73
+
74
+ ```ruby
75
+ module Web
76
+ module Views
77
+ class ApplicationLayout
78
+ ...
79
+ # Define the stylesheet for this layout
80
+ def layout_stylesheets(assets_collector)
81
+ assets_collector.require :'bootstrap-css' # include the bundle :'bootstrap-css'
82
+ assets_collector.include 'layout.css' # include the asset layout.css'
83
+ end
84
+
85
+ # Define the javascripts for this layout
86
+ def layout_javascripts(assets_collector)
87
+ assets_collector.require :'bootstrap-js'
88
+ end
89
+ ...
90
+ end
91
+ end
92
+ end
93
+ ```
94
+
95
+ To define the assets to be included in the view, you can add these methods to your Web::Views::SomeView class:
96
+
97
+ ```ruby
98
+ module Web::Views::LandingPage
99
+ class Index
100
+ include Web::View
101
+
102
+ # Define the stylesheet for this view
103
+ def view_stylesheets(assets_collector)
104
+ assets_collector.include 'landing-page.css'
105
+ end
106
+
107
+ # Define the javascripts for this view
108
+ def view_javascripts(assets_collector)
109
+ assets_collector.include 'landing-page.js'
110
+ end
111
+ end
112
+ end
113
+ ```
114
+
115
+ To collect the assets, call this from your template or helper:
116
+
117
+ ```ruby
118
+ <% CabezaDeTermo::Assets::HanamiRenderingScope.each_stylesheet_from(self) do |css| %>
119
+ <%= "<link href=\"#{css}\" rel=\"stylesheet\" type=\"text/css\">" %>
120
+ <% end %>
121
+
122
+ <% CabezaDeTermo::Assets::HanamiRenderingScope.each_javascript_from(self) do |js| %>
123
+ <%= "<script src=\"#{js}\" type=\"text/javascript\"></script>" %>
124
+ <% end %>
125
+ ```
126
+
127
+ ## Using this framework in another application
128
+
129
+ To define the bundles, require this file:
130
+
131
+ ```ruby
132
+ require 'cabeza-de-termo/assets/library'
133
+ ```
134
+
135
+ and then add the CabezaDeTermo::Assets::Library.definition:
136
+
137
+ ```ruby
138
+ # Define the asset bundles
139
+ CabezaDeTermo::Assets::Library.definition do
140
+ # Css
141
+ bundle :'bootstrap-css' do
142
+ include '/vendor/bootstrap/css/bootstrap.min.css' # include this asset in the bundle
143
+ end
144
+
145
+ # Js
146
+ bundle :jquery do
147
+ include '/vendor/jquery/jquery-1.11.3.min.js'
148
+ end
149
+
150
+ bundle :'bootstrap-js' do
151
+ require :jquery # declare that this bundle depends on the :jquery bundle
152
+ include '/vendor/bootstrap/js/bootstrap.min.js'
153
+ end
154
+ end
155
+ ```
156
+
157
+ Then define an object that responds the methods:
158
+
159
+ ```ruby
160
+ class OtherFrameworkRenderingScope
161
+ # Delegate the :collect_stylesheets_with method to the actual view
162
+ def collect_stylesheets_with(assets_collector)
163
+ ...
164
+ end
165
+
166
+ # Delegate the :collect_javascripts_with method to the actual view
167
+ def collect_javascripts_with(assets_collector)
168
+ ...
169
+ end
170
+ end
171
+ ```
172
+
173
+ You can see the CabezaDeTermo::Assets::HanamiRenderingScope class as an example.
174
+ Then pass that rendering_scope to CabezaDeTermo::Assets::Library to collect the assets for a view:
175
+
176
+ ```ruby
177
+ <%= CabezaDeTermo::Assets::Library.stylesheets_for rendering_scope %>
178
+ <%= CabezaDeTermo::Assets::Library.javascripts_for rendering_scope %>
179
+ ```
180
+
181
+ ## Running the tests
182
+
183
+ - `bundle install`
184
+ - `bundle exec rspec`
185
+
186
+ ## Contributing
187
+
188
+ 1. Fork it ( https://github.com/cabeza-de-termo/assets-library-for-hanami/fork )
189
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
190
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
191
+ 4. Push to the branch (`git push origin my-new-feature`)
192
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cabeza-de-termo/assets/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "assets-library-for-hanami"
8
+ spec.version = CabezaDeTermo::Assets::Library::VERSION
9
+ spec.authors = ["Martin Rubi"]
10
+ spec.email = ["martinrubi@gmail.com"]
11
+
12
+ spec.summary = %q{Framework to organize and collect css and js assets in your Hanami application.}
13
+ spec.description = %q{This framework allows you to declare js and css bundles and assets, including dependencies to other bundles, and collect them from your View.}
14
+ spec.homepage = "https://github.com/cabeza-de-termo/assets-library-for-hanami"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.required_ruby_version = '>= 2.0'
23
+
24
+ spec.add_dependency "cdt-utilities", "~> 0.3"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.8"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
@@ -0,0 +1,19 @@
1
+ require 'cabeza-de-termo/assets/bundles/reference-base'
2
+
3
+ module CabezaDeTermo
4
+ module Assets
5
+ # A reference to an asset. The reference_id can be any string, such as
6
+ # '/vendor/bootstrap/css/bootstrap.min.css'
7
+ # or
8
+ # 'main_layout'
9
+ #
10
+ # or anything at all.
11
+ #
12
+ class AssetReference < ReferenceBase
13
+ # Add the asset reference to the assets_collector
14
+ def collect_assets_with(assets_collector)
15
+ assets_collector.include reference_id
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ require 'cabeza-de-termo/assets/bundles/reference-base'
2
+
3
+ module CabezaDeTermo
4
+ module Assets
5
+ # A reference to an assets bundle. The reference_id can be the bundle name, such as
6
+ # :jquery
7
+ # or
8
+ # :'bootstrap-css'
9
+ #
10
+ class BundleReference < ReferenceBase
11
+ # Add the assets referenced by the bundle to the assets_collector
12
+ def collect_assets_with(assets_collector)
13
+ assets_collector.require reference_id
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,62 @@
1
+ require 'cabeza-de-termo/assets/bundles/asset-reference'
2
+ require 'cabeza-de-termo/assets/bundles/bundle-reference'
3
+
4
+ module CabezaDeTermo
5
+ module Assets
6
+ # A list of assets and other bundles to be collected by an assets_collector.
7
+ # A bundle can include a reference to an asset, such as 'jquery.js', or require
8
+ # a reference to another bundle.
9
+ #
10
+ # The bundles are defined in the Library.definition, example:
11
+ #
12
+ # CabezaDeTermo::Assets::Library.definition do
13
+ # bundle :'bootstrap-js' do
14
+ # require :jquery
15
+ # include 'asset_2.js'
16
+ # end
17
+ # end
18
+ #
19
+ class Bundle
20
+ # Initialize the instance
21
+ def initialize
22
+ @references = []
23
+ end
24
+
25
+ # Answer the list of references defined for this bundle. References may be any combination
26
+ # of AssetReference and BundleReference.
27
+ def references
28
+ @references
29
+ end
30
+
31
+ # Include a reference to the asset. The asset can be any string, such as '/vendor/jquery.js'.
32
+ def include(asset)
33
+ @references << new_asset_reference_on(asset)
34
+ end
35
+
36
+ # Require a reference to another bundle named bundle_name.
37
+ # By requiring another bundle, the assets_collector will collect all the assets
38
+ # in the another bundle.
39
+ def require(bundle_name)
40
+ @references << new_assets_bundle_reference_on(bundle_name)
41
+ end
42
+
43
+ # Add all the referenced assets to the assets_collector. Add both the assets included
44
+ # in this bundle and the assets included in another bundles required by this bundle.
45
+ def collect_assets_with(assets_collector)
46
+ @references.each do |asset|
47
+ asset.collect_assets_with assets_collector
48
+ end
49
+ end
50
+
51
+ # Create a new AssetReference on the asset
52
+ def new_asset_reference_on(asset)
53
+ AssetReference.on asset
54
+ end
55
+
56
+ # Create a new BundleReference on the bundle_name
57
+ def new_assets_bundle_reference_on(bundle_name)
58
+ BundleReference.on bundle_name
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,41 @@
1
+ require 'cabeza-de-termo/assets/bundles/bundle'
2
+ require 'cabeza-de-termo/assets/errors/bundle-not-found-error'
3
+
4
+ module CabezaDeTermo
5
+ module Assets
6
+ # A collection of named bundles.
7
+ #
8
+ class BundlesLibrary
9
+ # Initialize the instance
10
+ def initialize
11
+ @bundles = Hash.new
12
+ end
13
+
14
+ # Answer whether the library has the bundle named name or not.
15
+ def has_asset_named?(name)
16
+ @bundles.key? name
17
+ end
18
+
19
+ # Answer the bundle named name or raise an error if not found.
20
+ def bundle_named(name)
21
+ raise_bundle_not_found_error(name) unless has_asset_named?(name)
22
+ @bundles[name]
23
+ end
24
+
25
+ # Define a bundle named name and allow to :include assets and :require another bundles on it.
26
+ def bundle(name, &block)
27
+ @bundles[name] = new_bundle
28
+ CdT.bind_block_evaluation_to @bundles[name], &block
29
+ end
30
+
31
+ # Answer a new bundle
32
+ def new_bundle
33
+ Bundle.new
34
+ end
35
+
36
+ def raise_bundle_not_found_error(name)
37
+ raise BundleNotFoundError.new("Asset bundle '#{name}' not found.")
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,28 @@
1
+ module CabezaDeTermo
2
+ module Assets
3
+ # Base class for classes that models a reference to an asset or bundle.
4
+ # These references are held by a Bundle object when defined in the Library:definition block.
5
+ #
6
+ class ReferenceBase
7
+ # Answer a new instance on the reference_id.
8
+ def self.on(reference_id)
9
+ new(reference_id)
10
+ end
11
+
12
+ # Initialize the instance on the reference_id.
13
+ def initialize(reference_id)
14
+ @reference_id = reference_id
15
+ end
16
+
17
+ # Answer the reference_id
18
+ def reference_id
19
+ @reference_id
20
+ end
21
+
22
+ # Add the referenced assets to the assets_collector
23
+ def collect_assets_with(assets_collector)
24
+ CdT.subclass_responsibility
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ module CabezaDeTermo
2
+ module Assets
3
+ # This object collects the assets from both an included asset and the assets included
4
+ # by a required bundle.
5
+ #
6
+ class Collector
7
+ # Initialize the instance
8
+ def initialize
9
+ @assets = []
10
+ end
11
+
12
+ # Answer the collected assets. The answer is a collection of strings.
13
+ def assets
14
+ @assets
15
+ end
16
+
17
+ # Add the asset to the collected assets.
18
+ # Ignore the asset if it was already included.
19
+ def include(asset)
20
+ return if @assets.include?(asset)
21
+ @assets << asset
22
+ end
23
+
24
+ # Add the assets included in the bundle named bundle_name.
25
+ def require(bundle_name)
26
+ bundle_named(bundle_name)
27
+ .collect_assets_with(self)
28
+ end
29
+
30
+ # Answer the bundle named bundle_name.
31
+ def bundle_named(bundle_name)
32
+ Library.bundle_named bundle_name
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,41 @@
1
+ require 'cabeza-de-termo/assets/collectors/collector'
2
+
3
+ module CabezaDeTermo
4
+ module Assets
5
+ # Collects assets from a rendering scope.
6
+ class RenderingScopeAssetsCollector
7
+ # Initialize the instance
8
+ def initialize
9
+ @assets_collector = new_assets_collector
10
+ end
11
+
12
+ # Answer the collection of assets collected into the Collector.
13
+ def collected_assets
14
+ @assets_collector.assets
15
+ end
16
+
17
+ # Collect the stylesheets from the layout and view on the rendering_scope.
18
+ def collect_stylesheets_from(rendering_scope)
19
+ rendering_scope.collect_stylesheets_with assets_collector
20
+ collected_assets
21
+ end
22
+
23
+ # Collect the javascripts from the layout and view on the rendering_scope.
24
+ def collect_javascripts_from(rendering_scope)
25
+ rendering_scope.collect_javascripts_with assets_collector
26
+ collected_assets
27
+ end
28
+
29
+ protected
30
+ # Answer a new Collector object to perform the assets recollection.
31
+ def new_assets_collector
32
+ Collector.new
33
+ end
34
+
35
+ # Answer the Collector instance.
36
+ def assets_collector
37
+ @assets_collector
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,10 @@
1
+ require 'cabeza-de-termo/assets/errors/error'
2
+
3
+ module CabezaDeTermo
4
+ module Assets
5
+ # Error raised when a bundle is not found.
6
+ #
7
+ class BundleNotFoundError < Error
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module CabezaDeTermo
2
+ module Assets
3
+ # Base class for this framework errors.
4
+ #
5
+ class Error < RuntimeError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,117 @@
1
+ require 'cdt/utilities/subclass-responsibility'
2
+ require 'cdt/utilities/bind'
3
+ require 'cabeza-de-termo/assets/collectors/rendering-scope-assets-collector'
4
+ require 'cabeza-de-termo/assets/bundles/bundles-library'
5
+ require 'cabeza-de-termo/assets/version'
6
+
7
+ module CabezaDeTermo
8
+ module Assets
9
+ # The entry point to the assets library defintion and recollection.
10
+ # To define asset bundles, declare the following in your application:
11
+ #
12
+ # require 'cabeza-de-termo/assets/library'
13
+ #
14
+ # module Web
15
+ # class Application < Hanami::Application
16
+ # configure do
17
+ # ...
18
+ # CabezaDeTermo::Assets::Library.definition do
19
+ # # Css
20
+ # bundle :'bootstrap-css' do
21
+ # include '/vendor/bootstrap/css/bootstrap.min.css'
22
+ # end
23
+ #
24
+ # # Js
25
+ # bundle :jquery do
26
+ # include '/vendor/jquery/jquery-1.11.3.min.js'
27
+ # end
28
+ #
29
+ # bundle :'bootstrap-js' do
30
+ # require :jquery
31
+ # include '/vendor/bootstrap/js/bootstrap.min.js'
32
+ # end
33
+ # end
34
+ # ...
35
+ # end
36
+ # end
37
+ # end
38
+ #
39
+ # To define which assets to include for a view, you must define a class that
40
+ # delegates the assets recollection to the actual view and layout. A example would
41
+ # be something like this:
42
+ #
43
+ # class RenderingScopeAdaptor
44
+ # def collect_stylesheets_with(assets_collector)
45
+ # @view.collect_stylesheets_with assets_collector
46
+ # end
47
+ #
48
+ # def collect_javascripts_with(assets_collector)
49
+ # @view.collect_javascripts_with assets_collector
50
+ # end
51
+ # end
52
+ #
53
+ # See the CabezaDeTermo::Assets::HanamiRenderingScope class for a
54
+ # concrete example.
55
+ #
56
+ # Finally, in you View or Helper object you must define the methods
57
+ # :collect_stylesheets_with(asset_collector) and :collect_javascripts_with(asset_collector).
58
+ #
59
+ # class MyView
60
+ # def collect_stylesheets_with(assets_collector)
61
+ # assets_collector.require :'bootstrap-css'
62
+ # assets_collector.include 'landing-page.css'
63
+ # end
64
+ #
65
+ # def collect_javascripts_with(assets_collector)
66
+ # assets_collector.require :'bootstrap-js'
67
+ # assets_collector.include 'landing-page.js'
68
+ # end
69
+ # end
70
+ #
71
+ # To collect the assets, call this from your template or helper:
72
+ #
73
+ # <%= CabezaDeTermo::Assets::Library.stylesheets_for rendering_scope %>
74
+ #
75
+ class Library
76
+
77
+ # The BundlesLibrary singleton
78
+ @bundles_library = BundlesLibrary.new
79
+
80
+ # Answer the BundlesLibrary singleton
81
+ def self.bundles_library
82
+ @bundles_library
83
+ end
84
+
85
+ # Answer the bundle named name from the BundlesLibrary. Raise an error if not found
86
+ def self.bundle_named(name)
87
+ bundles_library.bundle_named name
88
+ end
89
+
90
+ # Define the bundles on the BundlesLibrary
91
+ def self.definition(&block)
92
+ CdT.bind_block_evaluation_to bundles_library, &block
93
+ end
94
+
95
+ # Collect the stylesheets defined for the rendering scope.
96
+ # This method can be called directly from the template like this:
97
+ # <%= CabezaDeTermo::Assets::Library.stylesheets_for rendering_scope %>
98
+ # and then you can iterate on the collected stylesheets.
99
+ def self.stylesheets_for(rendering_scope)
100
+ newRenderingScopeAssetsCollector.collect_stylesheets_from rendering_scope
101
+ end
102
+
103
+ # Collect the javascripts defined for the rendering scope.
104
+ # This method can be called directly from the template like this:
105
+ # <%= CabezaDeTermo::Assets::Library.javascripts_for rendering_scope %>
106
+ # and then you can iterate on the collected javascripts.
107
+ def self.javascripts_for(rendering_scope)
108
+ newRenderingScopeAssetsCollector.collect_javascripts_from rendering_scope
109
+ end
110
+
111
+ # Answer a new instance of the collector of stylesheets of a rendering_scope
112
+ def self.newRenderingScopeAssetsCollector
113
+ RenderingScopeAssetsCollector.new
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,77 @@
1
+ module CabezaDeTermo
2
+ module Assets
3
+ # This class is a wrapper on a Hanami::Views::LayoutScope object to delegate the
4
+ # :collect_stylesheets_with and :collect_javascripts_with to the actual layout_scope.
5
+ class HanamiRenderingScope
6
+ # Class methods
7
+
8
+ # Answer a new adaptor on the layout_scope
9
+ def self.on(layout_scope)
10
+ new(layout_scope)
11
+ end
12
+
13
+ # Collect the stylesheets from the layout_scope
14
+ def self.stylesheets_from(layout_scope)
15
+ Library.stylesheets_for (self.on layout_scope)
16
+ end
17
+
18
+ # Collect the javascripts from the layout_scope
19
+ def self.javascripts_from(layout_scope)
20
+ Library.javascripts_for (self.on layout_scope)
21
+ end
22
+
23
+ # Collect the stylesheets from the layout_scope and iterate over each stylesheet
24
+ def self.each_stylesheet_from(layout_scope, &block)
25
+ stylesheets_from(layout_scope).each(&block)
26
+ end
27
+
28
+ # Collect the javascripts from the layout_scope and iterate over each javascript
29
+ def self.each_javascript_from(layout_scope, &block)
30
+ javascripts_from(layout_scope).each(&block)
31
+ end
32
+
33
+ # Instance methods
34
+
35
+ def initialize(layout_scope)
36
+ @layout_scope = layout_scope
37
+ end
38
+
39
+ # Delegate the :collect_stylesheets_with method to the actual Hanami::LayoutScope
40
+ def collect_stylesheets_with(assets_collector)
41
+ collect_stylesheets_from_layout_with assets_collector
42
+ collect_stylesheets_from_view_with assets_collector
43
+ end
44
+
45
+ # Delegate the :collect_javascripts_with method to the actual Hanami::LayoutScope
46
+ def collect_javascripts_with(assets_collector)
47
+ collect_javascripts_from_layout_with assets_collector
48
+ collect_javascripts_from_view_with assets_collector
49
+ end
50
+
51
+ protected
52
+ # Collect the stylesheets from the layout on the @layout_scope, if present.
53
+ def collect_stylesheets_from_layout_with(assets_collector)
54
+ return unless @layout_scope.respond_to?(:layout_stylesheets)
55
+ @layout_scope.layout_stylesheets assets_collector
56
+ end
57
+
58
+ # Collect the stylesheets from the view on the @layout_scope, if present.
59
+ def collect_stylesheets_from_view_with(assets_collector)
60
+ return unless @layout_scope.respond_to?(:view_stylesheets)
61
+ @layout_scope.view_stylesheets assets_collector
62
+ end
63
+
64
+ # Collect the javascripts from the layout on the @layout_scope, if present.
65
+ def collect_javascripts_from_layout_with(assets_collector)
66
+ return unless @layout_scope.respond_to?(:layout_javascripts)
67
+ @layout_scope.layout_javascripts assets_collector
68
+ end
69
+
70
+ # Collect the javascripts from the view on the @layout_scope, if present.
71
+ def collect_javascripts_from_view_with(assets_collector)
72
+ return unless @layout_scope.respond_to?(:view_javascripts)
73
+ @layout_scope.view_javascripts assets_collector
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,7 @@
1
+ module CabezaDeTermo
2
+ module Assets
3
+ class Library
4
+ VERSION = "3.0.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assets-library-for-hanami
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Rubi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cdt-utilities
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: This framework allows you to declare js and css bundles and assets, including
56
+ dependencies to other bundles, and collect them from your View.
57
+ email:
58
+ - martinrubi@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - hanami-assets-library.gemspec
71
+ - lib/cabeza-de-termo/assets/bundles/asset-reference.rb
72
+ - lib/cabeza-de-termo/assets/bundles/bundle-reference.rb
73
+ - lib/cabeza-de-termo/assets/bundles/bundle.rb
74
+ - lib/cabeza-de-termo/assets/bundles/bundles-library.rb
75
+ - lib/cabeza-de-termo/assets/bundles/reference-base.rb
76
+ - lib/cabeza-de-termo/assets/collectors/collector.rb
77
+ - lib/cabeza-de-termo/assets/collectors/rendering-scope-assets-collector.rb
78
+ - lib/cabeza-de-termo/assets/errors/bundle-not-found-error.rb
79
+ - lib/cabeza-de-termo/assets/errors/error.rb
80
+ - lib/cabeza-de-termo/assets/library.rb
81
+ - lib/cabeza-de-termo/assets/rendering-scope-adaptors/hanami-rendering-scope.rb
82
+ - lib/cabeza-de-termo/assets/version.rb
83
+ homepage: https://github.com/cabeza-de-termo/assets-library-for-hanami
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '2.0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.4.5.1
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Framework to organize and collect css and js assets in your Hanami application.
107
+ test_files: []