ramaze-asset 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gems +6 -0
- data/.gitignore +14 -0
- data/.rvmrc +1 -0
- data/LICENSE +19 -0
- data/README.md +64 -0
- data/Rakefile +15 -0
- data/example.rb +54 -0
- data/lib/.gitkeep +0 -0
- data/lib/ramaze/.gitkeep +0 -0
- data/lib/ramaze/asset.rb +15 -0
- data/lib/ramaze/asset/css.rb +44 -0
- data/lib/ramaze/asset/environment.rb +482 -0
- data/lib/ramaze/asset/error.rb +5 -0
- data/lib/ramaze/asset/file_group.rb +385 -0
- data/lib/ramaze/asset/javascript.rb +41 -0
- data/lib/ramaze/asset/spec/bacon/color_output.rb +39 -0
- data/lib/ramaze/asset/spec/helper.rb +18 -0
- data/lib/ramaze/asset/version.rb +5 -0
- data/lib/vendor/cssmin.rb +110 -0
- data/lib/vendor/jsmin.rb +280 -0
- data/pkg/.gitkeep +0 -0
- data/ramaze-asset.gemspec +24 -0
- data/spec/.gitkeep +0 -0
- data/spec/fixtures/public/css/github.css +126 -0
- data/spec/fixtures/public/css/reset.css +123 -0
- data/spec/fixtures/public/js/mootools_core.js +5515 -0
- data/spec/fixtures/public/js/mootools_more.js +13455 -0
- data/spec/helper.rb +4 -0
- data/spec/ramaze_asset/css.rb +69 -0
- data/spec/ramaze_asset/environment.rb +148 -0
- data/spec/ramaze_asset/file_group.rb +118 -0
- data/spec/ramaze_asset/javascript.rb +69 -0
- data/task/build.rake +35 -0
- data/task/clean.rake +6 -0
- data/task/test.rake +8 -0
- metadata +148 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create 1.9.2@ramaze_asset
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011, Yorick Peterse
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Ramaze::Asset
|
2
|
+
|
3
|
+
Ramaze::Asset is an asset manager that can be used with [Ramaze][ramaze]. Out of
|
4
|
+
the box it's capable of serving Javascript and CSS files but you can very easily
|
5
|
+
add your own types.
|
6
|
+
|
7
|
+
## Requirements
|
8
|
+
|
9
|
+
All you need is Ramaze. It doesn't really matter what version you're using but
|
10
|
+
2011.07.25 or newer is recommended.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Install the Gem:
|
15
|
+
|
16
|
+
$ gem install ramaze-asset
|
17
|
+
|
18
|
+
Then require it somewhere in app.rb or wherever you like:
|
19
|
+
|
20
|
+
require 'ramaze/asset'
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Ramaze::Asset uses so called "environments" to manage assets. Each environment
|
25
|
+
has it's own set of assets. In order to create such an environment you'll need
|
26
|
+
to initialize Ramaze::Asset::Environment and supply it with a directory to store
|
27
|
+
your minified files in:
|
28
|
+
|
29
|
+
require 'ramaze/asset'
|
30
|
+
|
31
|
+
env = Ramaze::Asset::Environment.new(:cache_path => __DIR__('public/minified'))
|
32
|
+
|
33
|
+
Once an environment has been created you can add files by calling the instance
|
34
|
+
method ``serve()``:
|
35
|
+
|
36
|
+
env.serve(:javascript, ['js/foobar'], :minify => true, :name => 'foobar')
|
37
|
+
|
38
|
+
Building the minified files and generating the HTML can be done as following:
|
39
|
+
|
40
|
+
env.build(:javascript)
|
41
|
+
|
42
|
+
tags = env.build_html(:javascript)
|
43
|
+
|
44
|
+
p tags # => "<script src="minified/foobar.min.js" type="text/javascript">"
|
45
|
+
|
46
|
+
When loading files it's important to remember that these files should be
|
47
|
+
specified relative to one of your public directories. Ramaze::Asset does not
|
48
|
+
move them into a public directory itself nor does it add any routes. It's your
|
49
|
+
job to make sure that the assets are located in one of your root directories'
|
50
|
+
public directories. If ``Ramaze.options.roots`` contains a root directory called
|
51
|
+
"A" then the assets should be located in ``A/public``. You can customize this by
|
52
|
+
adding/removing paths to ``Ramaze.options.roots`` and
|
53
|
+
``Ramaze.options.publics``.
|
54
|
+
|
55
|
+
More information can be found in the source of each file, don't worry, they're
|
56
|
+
documented well enough for most people to be able to understand how to use
|
57
|
+
Ramaze::Asset.
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
Ramaze::Asset is licensed under the MIT license. A copy of this license can be
|
62
|
+
found in the file "LICENSE".
|
63
|
+
|
64
|
+
[ramaze]: http://ramaze.net/
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path('../lib/ramaze/asset', __FILE__)
|
2
|
+
|
3
|
+
module Ramaze
|
4
|
+
module Asset
|
5
|
+
Gemspec = Gem::Specification::load(
|
6
|
+
File.expand_path('../ramaze-asset.gemspec', __FILE__)
|
7
|
+
)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
task_dir = File.expand_path('../task', __FILE__)
|
12
|
+
|
13
|
+
Dir.glob("#{task_dir}/*.rake").each do |f|
|
14
|
+
import(f)
|
15
|
+
end
|
data/example.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'ramaze'
|
2
|
+
require __DIR__('lib/ramaze/asset')
|
3
|
+
|
4
|
+
# Ramaze::Asset needs a root directory that contains a public directory.
|
5
|
+
Ramaze.options.roots.push(__DIR__('spec/fixtures'))
|
6
|
+
|
7
|
+
# Create the environment and tell it to minify files.
|
8
|
+
AssetEnv = Ramaze::Asset::Environment.new(
|
9
|
+
:cache_path => __DIR__('spec/fixtures/public/minified/'),
|
10
|
+
:minify => true
|
11
|
+
)
|
12
|
+
|
13
|
+
class MainController < Ramaze::Controller
|
14
|
+
map '/'
|
15
|
+
|
16
|
+
def index
|
17
|
+
output = <<-TXT
|
18
|
+
#{AssetEnv.build_html(:javascript)}
|
19
|
+
#{AssetEnv.build_html(:css)}
|
20
|
+
|
21
|
+
<script type="text/javascript">
|
22
|
+
window.addEvent('domready', function()
|
23
|
+
{
|
24
|
+
alert("Hello Ramaze::Asset");
|
25
|
+
});
|
26
|
+
</script>
|
27
|
+
TXT
|
28
|
+
|
29
|
+
return output
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
AssetEnv.serve(
|
34
|
+
:javascript,
|
35
|
+
['js/mootools_core'],
|
36
|
+
:controller => MainController
|
37
|
+
)
|
38
|
+
|
39
|
+
AssetEnv.serve(
|
40
|
+
:javascript,
|
41
|
+
['js/mootools_more'],
|
42
|
+
:controller => MainController
|
43
|
+
)
|
44
|
+
|
45
|
+
AssetEnv.serve(
|
46
|
+
:css,
|
47
|
+
['css/reset', 'css/github'],
|
48
|
+
:controller => MainController
|
49
|
+
)
|
50
|
+
|
51
|
+
AssetEnv.build(:javascript)
|
52
|
+
AssetEnv.build(:css)
|
53
|
+
|
54
|
+
Ramaze.start(:root => Ramaze.options.roots)
|
data/lib/.gitkeep
ADDED
File without changes
|
data/lib/ramaze/.gitkeep
ADDED
File without changes
|
data/lib/ramaze/asset.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'ramaze'
|
3
|
+
|
4
|
+
require __DIR__('../vendor/jsmin')
|
5
|
+
require __DIR__('../vendor/cssmin')
|
6
|
+
|
7
|
+
require __DIR__('asset/version')
|
8
|
+
require __DIR__('asset/error')
|
9
|
+
require __DIR__('asset/environment')
|
10
|
+
|
11
|
+
require __DIR__('asset/css')
|
12
|
+
require __DIR__('asset/javascript')
|
13
|
+
|
14
|
+
Ramaze::Asset::Environment.register_type(:css, Ramaze::Asset::CSS)
|
15
|
+
Ramaze::Asset::Environment.register_type(:javascript, Ramaze::Asset::Javascript)
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require __DIR__('../../vendor/cssmin')
|
2
|
+
|
3
|
+
module Ramaze
|
4
|
+
module Asset
|
5
|
+
##
|
6
|
+
# File group for CSS files, these files are minified using CSSMin.
|
7
|
+
#
|
8
|
+
# @author Yorick Peterse
|
9
|
+
# @since 0.1
|
10
|
+
#
|
11
|
+
class CSS < Ramaze::Asset::FileGroup
|
12
|
+
extension '.css'
|
13
|
+
|
14
|
+
##
|
15
|
+
# Minifies the output and returns the result as a string.
|
16
|
+
#
|
17
|
+
# @author Yorick Peterse
|
18
|
+
# @since 0.1
|
19
|
+
# @param [String] input The input to minify.
|
20
|
+
# @return [String]
|
21
|
+
#
|
22
|
+
def minify(input)
|
23
|
+
return CSSMin.minify(input)
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Builds a single <link> tag.
|
28
|
+
#
|
29
|
+
# @author Yorick Peterse
|
30
|
+
# @since 0.1
|
31
|
+
# @param [Ramaze::Gestalt] gestalt An instance of Ramaze::Gestalt used to
|
32
|
+
# build the tags.
|
33
|
+
# @param [String] path The relative path to the file for the tag.
|
34
|
+
#
|
35
|
+
def html_tag(gestalt, path)
|
36
|
+
gestalt.link(
|
37
|
+
:rel => 'stylesheet',
|
38
|
+
:href => path,
|
39
|
+
:type => 'text/css'
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end # CSS
|
43
|
+
end # Asset
|
44
|
+
end # Ramaze
|
@@ -0,0 +1,482 @@
|
|
1
|
+
require __DIR__('file_group')
|
2
|
+
require __DIR__('css')
|
3
|
+
require __DIR__('javascript')
|
4
|
+
|
5
|
+
module Ramaze
|
6
|
+
module Asset
|
7
|
+
##
|
8
|
+
# The Environment class can be used to create isolated environments of which
|
9
|
+
# each serves it's own set of assets and has it's own cache path. Creating a
|
10
|
+
# new environment can be done by initializing the class:
|
11
|
+
#
|
12
|
+
# env = Ramaze::Asset::Environment.new(:cache_path => '...')
|
13
|
+
#
|
14
|
+
# It's important to remember that the cache path will *not* be created if it
|
15
|
+
# doesn't exist.
|
16
|
+
#
|
17
|
+
# Once an environment has been created you can tell it to serve files by
|
18
|
+
# calling the serve() method:
|
19
|
+
#
|
20
|
+
# env.serve(:javascript, ['js/mootools/core'], :minify => true)
|
21
|
+
#
|
22
|
+
# The first parameter is the type of file to serve. Out of the box
|
23
|
+
# Ramaze::Asset serves Javascript (:javascript) and CSS (:css) files. The
|
24
|
+
# second parameter is an array of files relative to one of Ramaze's public
|
25
|
+
# directories (with or without extension). The last parameter is a hash
|
26
|
+
# containing various options.
|
27
|
+
#
|
28
|
+
# Once you've added a set of files you'll need to minify them (if there are
|
29
|
+
# any files to minify) followed by generating the HTML tags. This is done in
|
30
|
+
# two separate steps for each type of file you want to build:
|
31
|
+
#
|
32
|
+
# # Minify all Javascript files and generate the HTML
|
33
|
+
# env.build(:javascript)
|
34
|
+
# env.build_html(:javascript)
|
35
|
+
#
|
36
|
+
# # Do the same for CSS files
|
37
|
+
# env.build(:css)
|
38
|
+
# env.build_html(:css)
|
39
|
+
#
|
40
|
+
# It's best to handle the minifying of files while booting up your
|
41
|
+
# application, this way HTTP requests won't be delayed the first time a set
|
42
|
+
# of files is minified. Note that files are only minified the first time OR
|
43
|
+
# when their content has changed.
|
44
|
+
#
|
45
|
+
# ## Registering Types
|
46
|
+
#
|
47
|
+
# Ramaze::Asset is built in such a way that it's relatively easy to register
|
48
|
+
# file types to serve. This can be done by calling
|
49
|
+
# Ramaze::Asset::Environment.register_type() as following:
|
50
|
+
#
|
51
|
+
# Ramaze::Asset::Environment.register_type(:less, Ramaze::Asset::Less)
|
52
|
+
#
|
53
|
+
# The first parameter is the type of file and will be using in methods such
|
54
|
+
# as Ramaze::Asset::Environment#serve(), the second parameter is a class
|
55
|
+
# that extends Ramaze::Asset::FileGroup. This class should define two
|
56
|
+
# methods, minify() and html_tag(). For more information on these methods
|
57
|
+
# see Ramaze::Asset::FileGroup#minify() and
|
58
|
+
# Ramaze::Asset::FileGroup#html_tag().
|
59
|
+
#
|
60
|
+
# ## Asset Groups
|
61
|
+
#
|
62
|
+
# Asset groups are a way of grouping assets together and load them all at
|
63
|
+
# the same time. This can be useful if you want to supply packages such as
|
64
|
+
# Mootools or jQuery without requiring the user to specify the paths to all
|
65
|
+
# the individual files.
|
66
|
+
#
|
67
|
+
# Adding an asset group can be done by calling
|
68
|
+
# Ramaze::Asset::Environment#register_asset_group:
|
69
|
+
#
|
70
|
+
# env = Ramaze::Asset::Environment.new(...)
|
71
|
+
#
|
72
|
+
# env.register_asset_group(:mootools) do |env|
|
73
|
+
# env.serve(:javascript, ['js/mootools/core', 'js/mootools/more'])
|
74
|
+
# env.serve(:css, ['css/foobar'])
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# The group's block is yielded onto the environment it was added to, thus
|
78
|
+
# the "env" parameter is required.
|
79
|
+
#
|
80
|
+
# Loading this group can be done by calling
|
81
|
+
# Ramaze::Asset::AssetManager#load_asset_group and specifying the name of
|
82
|
+
# the group to load:
|
83
|
+
#
|
84
|
+
# env.load_asset_group(:mootools)
|
85
|
+
#
|
86
|
+
# @author Yorick Peterse
|
87
|
+
# @since 0.1
|
88
|
+
#
|
89
|
+
class Environment
|
90
|
+
# Hash containing all the file groups for the current environment.
|
91
|
+
attr_reader :files
|
92
|
+
|
93
|
+
# Hash containing all the file group types and their classes.
|
94
|
+
Types = {}
|
95
|
+
|
96
|
+
##
|
97
|
+
# Registers a new type of file to serve. See Ramaze::Asset::FileGroup for
|
98
|
+
# more information about the structure of the class used for the file
|
99
|
+
# type.
|
100
|
+
#
|
101
|
+
# @example
|
102
|
+
# class Foobar < Ramaze::Asset::FileGroup
|
103
|
+
# extension '.foobar'
|
104
|
+
#
|
105
|
+
# def minify(input)
|
106
|
+
# return input
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# def html_tag(gestalt, path)
|
110
|
+
# gestalt.p(path)
|
111
|
+
# end
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
# Ramaze::Asset::Environment.register_type(:foobar, Foobar)
|
115
|
+
#
|
116
|
+
# @author Yorick Peterse
|
117
|
+
# @since 0.1
|
118
|
+
# @param [#to_sym] name The name of the type such as :js or :css.
|
119
|
+
# @param [Class] klass The klass to initialize for a file group.
|
120
|
+
#
|
121
|
+
def self.register_type(name, klass)
|
122
|
+
name = name.to_sym
|
123
|
+
|
124
|
+
if Types.key?(name)
|
125
|
+
raise(
|
126
|
+
Ramaze::Asset::AssetError,
|
127
|
+
"The type \"#{name}\" already exists"
|
128
|
+
)
|
129
|
+
end
|
130
|
+
|
131
|
+
Types[name] = klass
|
132
|
+
end
|
133
|
+
|
134
|
+
##
|
135
|
+
# Creates a new instance of the environment and prepares it.
|
136
|
+
#
|
137
|
+
# @author Yorick Peterse
|
138
|
+
# @since 0.1
|
139
|
+
# @param [Hash] options A hash containing various options to customize
|
140
|
+
# the environment.
|
141
|
+
# @option options :cache_path A directory to use for saving all minified
|
142
|
+
# files. This directory should be a public directory so that the files in
|
143
|
+
# it can be served by either Ramaze or your webserver.
|
144
|
+
# @option options :minify When set to false no files will be minified
|
145
|
+
# regardless of their individual settings. Useful while developing the
|
146
|
+
# application. When set to true all files will be minified *unless* a
|
147
|
+
# group turned the option off.
|
148
|
+
#
|
149
|
+
def initialize(options = {})
|
150
|
+
@options = {
|
151
|
+
:cache_path => nil,
|
152
|
+
:minify => false
|
153
|
+
}.merge(options)
|
154
|
+
|
155
|
+
if !File.directory?(@options[:cache_path])
|
156
|
+
raise(
|
157
|
+
Ramaze::Asset::AssetError,
|
158
|
+
"The cache directory #{@options[:cache_path]} doesn't exist"
|
159
|
+
)
|
160
|
+
end
|
161
|
+
|
162
|
+
@files = {}
|
163
|
+
@added_files = {}
|
164
|
+
@asset_groups = {}
|
165
|
+
@file_group_options = {
|
166
|
+
:paths => [],
|
167
|
+
:cache_path => @options[:cache_path]
|
168
|
+
}
|
169
|
+
|
170
|
+
# Get all the public directories to serve files from.
|
171
|
+
Ramaze.options.roots.each do |root|
|
172
|
+
Ramaze.options.publics.each do |pub|
|
173
|
+
pub = File.join(root, pub)
|
174
|
+
|
175
|
+
if File.directory?(pub)
|
176
|
+
@file_group_options[:paths].push(pub)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
if @file_group_options[:paths].empty?
|
182
|
+
raise(
|
183
|
+
Ramaze::Asset::AssetError,
|
184
|
+
'No existing public directories were found'
|
185
|
+
)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
##
|
190
|
+
# Adds a new asset group to the current environment.
|
191
|
+
#
|
192
|
+
# @example
|
193
|
+
# env = Ramaze::Asset::AssetManager.new(:cache_path => '...')
|
194
|
+
#
|
195
|
+
# env.register_asset_group(:mootools) do |env|
|
196
|
+
# env.serve(
|
197
|
+
# :javascript,
|
198
|
+
# ['js/mootools/core', 'js/mootools/more'],
|
199
|
+
# :minify => true,
|
200
|
+
# :name => 'mootools'
|
201
|
+
# )
|
202
|
+
# end
|
203
|
+
#
|
204
|
+
# @author Yorick Peterse
|
205
|
+
# @since 0.1
|
206
|
+
# @param [Symbol] name The name of the asset group.
|
207
|
+
# @param [Block] block A block that will be yield on the current
|
208
|
+
# instance. This block defines what assets should be loaded.
|
209
|
+
#
|
210
|
+
def register_asset_group(name, &block)
|
211
|
+
name = name.to_sym unless name.is_a?(Symbol)
|
212
|
+
|
213
|
+
if @asset_groups.key?(name)
|
214
|
+
raise(
|
215
|
+
Ramaze::Asset::AssetError,
|
216
|
+
"The asset group \"#{name}\" already exists"
|
217
|
+
)
|
218
|
+
end
|
219
|
+
|
220
|
+
@asset_groups[name] = block
|
221
|
+
end
|
222
|
+
|
223
|
+
##
|
224
|
+
# Loads the given asset group.
|
225
|
+
#
|
226
|
+
# @example
|
227
|
+
# env = Ramaze::Asset::AssetManager.new(:cache_path => '...')
|
228
|
+
#
|
229
|
+
# env.register_asset_group(:mootools) do |env|
|
230
|
+
# env.serve(...)
|
231
|
+
# end
|
232
|
+
#
|
233
|
+
# env.load_asset_group(:mootools)
|
234
|
+
#
|
235
|
+
# @author Yorick Peterse
|
236
|
+
# @since 0.1
|
237
|
+
# @param [Symbol] name The name of the asset group to load.
|
238
|
+
# @yield self
|
239
|
+
#
|
240
|
+
def load_asset_group(name)
|
241
|
+
name = name.to_sym unless name.is_a?(Symbol)
|
242
|
+
|
243
|
+
if !@asset_groups.key?(name)
|
244
|
+
raise(
|
245
|
+
Ramaze::Asset::AssetError,
|
246
|
+
"The asset group \"#{name}\" doesn't exist"
|
247
|
+
)
|
248
|
+
end
|
249
|
+
|
250
|
+
@asset_groups[name].call(self)
|
251
|
+
end
|
252
|
+
|
253
|
+
##
|
254
|
+
# Adds a set of Javascript files to the environment.
|
255
|
+
#
|
256
|
+
# @example
|
257
|
+
# env = Ramaze::Asset::Environment.new(__DIR__('public'))
|
258
|
+
# env.javascript(
|
259
|
+
# ['mootools/core', 'mootools/more'],
|
260
|
+
# :controller => :global,
|
261
|
+
# :minify => true,
|
262
|
+
# :name => 'mootools'
|
263
|
+
# )
|
264
|
+
#
|
265
|
+
# @author Yorick Peterse
|
266
|
+
# @since 0.1
|
267
|
+
# @see Ramaze::Asset::FileGroup
|
268
|
+
# @param [#to_sym] type The type of files to serve.
|
269
|
+
# @param [Array] files An array of files to serve from one of the public
|
270
|
+
# directories.
|
271
|
+
# @param [Hash] options A hash containing various options to customize
|
272
|
+
# the file group.
|
273
|
+
# @option options :controller The controller to serve the files for, set
|
274
|
+
# to :global to serve the files for *all* controllers. By default files
|
275
|
+
# are loaded globally.
|
276
|
+
# @option options :methods An array of methods that belong to the
|
277
|
+
# controller set in :controller. When setting these methods the files
|
278
|
+
# will only be served when those methods are executed. This option is
|
279
|
+
# completely ignored if :controller is set to :global.
|
280
|
+
#
|
281
|
+
def serve(type, files, options = {})
|
282
|
+
type = type.to_sym
|
283
|
+
|
284
|
+
if !Types.key?(type)
|
285
|
+
raise(
|
286
|
+
Zen::Asset::AssetError,
|
287
|
+
"The type \"#{type}\" doesn't exist"
|
288
|
+
)
|
289
|
+
end
|
290
|
+
|
291
|
+
@files[type] ||= {:global => {:__all => []}}
|
292
|
+
@added_files[type] ||= []
|
293
|
+
|
294
|
+
options, controller, methods = prepare_options(options)
|
295
|
+
file_group = Types[type].new(files, options)
|
296
|
+
|
297
|
+
store_group(type, file_group, controller, methods)
|
298
|
+
end
|
299
|
+
|
300
|
+
##
|
301
|
+
# Builds all the files for the given type.
|
302
|
+
#
|
303
|
+
# @author Yorick Peterse
|
304
|
+
# @since 0.1
|
305
|
+
# @param [#to_sym] ty]e The type of files to build.
|
306
|
+
# @see Ramaze::Asset::FileGroup#build()
|
307
|
+
#
|
308
|
+
def build(type)
|
309
|
+
type = type.to_sym
|
310
|
+
|
311
|
+
if @files.nil? or !@files.key?(type)
|
312
|
+
raise(Ramaze::Asset::AssetError, "The type \"#{type}\" doesn't exist")
|
313
|
+
end
|
314
|
+
|
315
|
+
@files[type].each do |controller, methods|
|
316
|
+
methods.each do |method, groups|
|
317
|
+
groups.each do |group|
|
318
|
+
group.build
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
##
|
325
|
+
# Builds the HTML tags for all the files of the given type.
|
326
|
+
#
|
327
|
+
# @author Yorick Peterse
|
328
|
+
# @since 0.1
|
329
|
+
# @param [#to_sym] type The type of files to build the HTML for.
|
330
|
+
# @return [String]
|
331
|
+
#
|
332
|
+
def build_html(type)
|
333
|
+
html = ''
|
334
|
+
type = type.to_sym
|
335
|
+
|
336
|
+
if @files.nil? or !@files.key?(type)
|
337
|
+
raise(
|
338
|
+
Ramaze::Asset::AssetError,
|
339
|
+
"The type \"#{type}\" doesn't exist"
|
340
|
+
)
|
341
|
+
end
|
342
|
+
|
343
|
+
controller, method = current_action
|
344
|
+
|
345
|
+
# Build all the global tags
|
346
|
+
@files[type][:global][:__all].each do |group|
|
347
|
+
html += group.build_html
|
348
|
+
end
|
349
|
+
|
350
|
+
# Build the ones for the current controller
|
351
|
+
if !controller.nil? and @files[type].key?(controller)
|
352
|
+
if method.nil?
|
353
|
+
method = :__all
|
354
|
+
elsif !method.nil? and !@files[type][controller].key?(method)
|
355
|
+
method = :__all
|
356
|
+
end
|
357
|
+
|
358
|
+
@files[type][controller][method].each do |group|
|
359
|
+
html += group.build_html
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
return html
|
364
|
+
end
|
365
|
+
|
366
|
+
##
|
367
|
+
# Resets the environment by removing all the loaded files from it.
|
368
|
+
#
|
369
|
+
# @author Yorick Peterse
|
370
|
+
# @since 0.1
|
371
|
+
#
|
372
|
+
def reset!
|
373
|
+
@files.each do |type, controllers|
|
374
|
+
@files[type] = {:global => {:__all => []}}
|
375
|
+
end
|
376
|
+
|
377
|
+
@added_files.each do |type, files|
|
378
|
+
@added_files[type] = []
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
private
|
383
|
+
|
384
|
+
##
|
385
|
+
# Assigns the file groups to the correct hash in the @files array. This
|
386
|
+
# method ignores files that have already been loaded.
|
387
|
+
#
|
388
|
+
# @author Yorick Peterse
|
389
|
+
# @since 0.1
|
390
|
+
# @param [Symbol] key A key in the @files hash to store the results in.
|
391
|
+
# @param [Ramaze::Asset::Javascript|Ramaze::Asset::CSS] file_group The
|
392
|
+
# file group to store.
|
393
|
+
# @param [Symbol] controller The controller to store the file group in.
|
394
|
+
# @param [Array] methods An array of methods to store the file group in.
|
395
|
+
#
|
396
|
+
def store_group(key, file_group, controller, methods)
|
397
|
+
# Remove all files from the group that have already been loaded.
|
398
|
+
file_group.files.each_with_index do |file, index|
|
399
|
+
if @added_files.key?(key) and @added_files[key].include?(file)
|
400
|
+
file_group.files.delete_at(index)
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
return if file_group.files.empty?
|
405
|
+
|
406
|
+
@files[key][controller] ||= {:__all => []}
|
407
|
+
|
408
|
+
# Add the group to each method.
|
409
|
+
methods.each do |m|
|
410
|
+
@files[key][controller][m] ||= []
|
411
|
+
@files[key][controller][m].push(file_group)
|
412
|
+
end
|
413
|
+
|
414
|
+
@added_files[key] += file_group.files
|
415
|
+
end
|
416
|
+
|
417
|
+
##
|
418
|
+
# Gets the class and method of the current request.
|
419
|
+
#
|
420
|
+
# @author Yorick Peterse
|
421
|
+
# @since 0.1
|
422
|
+
# @return [Array] Array containing the controller and method. The first
|
423
|
+
# item is the controller, the second the method.
|
424
|
+
#
|
425
|
+
def current_action
|
426
|
+
begin
|
427
|
+
controller = Ramaze::Current.action.node.to_s.to_sym
|
428
|
+
method = Ramaze::Current.action.method.to_s.to_sym
|
429
|
+
rescue
|
430
|
+
controller = nil
|
431
|
+
method = nil
|
432
|
+
end
|
433
|
+
|
434
|
+
# When called in a layout the current action is different than the one
|
435
|
+
# we're interested in.
|
436
|
+
if !method.nil? and method.empty? and Ramaze::Current.actions[-2]
|
437
|
+
method = Ramaze::Current.actions[-2].method.to_s.to_sym
|
438
|
+
end
|
439
|
+
|
440
|
+
return [controller, method]
|
441
|
+
end
|
442
|
+
|
443
|
+
##
|
444
|
+
# Prepares the options for Ramaze::Asset::Environment#javascript() and
|
445
|
+
# Ramaze::Asset::Environment#css().
|
446
|
+
#
|
447
|
+
# @author Yorick Peterse
|
448
|
+
# @since 0.1
|
449
|
+
# @param [Hash] options A hash with options.
|
450
|
+
# @return [Array]
|
451
|
+
#
|
452
|
+
def prepare_options(options)
|
453
|
+
if @options[:minify] === true
|
454
|
+
if !options.key?(:minify)
|
455
|
+
options[:minify] = true
|
456
|
+
end
|
457
|
+
else
|
458
|
+
options[:minify] = false
|
459
|
+
end
|
460
|
+
|
461
|
+
controller = options.delete(:controller) || :global
|
462
|
+
methods = options.delete(:methods) || [:__all]
|
463
|
+
options = options.merge(@file_group_options)
|
464
|
+
|
465
|
+
|
466
|
+
if !controller.is_a?(Symbol)
|
467
|
+
controller = controller.to_s.to_sym
|
468
|
+
end
|
469
|
+
|
470
|
+
if !methods.respond_to?(:each_with_index)
|
471
|
+
methods = [methods]
|
472
|
+
end
|
473
|
+
|
474
|
+
methods.each_with_index do |method, index|
|
475
|
+
methods[index] = method.to_sym
|
476
|
+
end
|
477
|
+
|
478
|
+
return [options, controller, methods]
|
479
|
+
end
|
480
|
+
end # Environment
|
481
|
+
end # Asset
|
482
|
+
end # Ramaze
|