ramaze-asset 0.2

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.
data/spec/helper.rb ADDED
@@ -0,0 +1,4 @@
1
+ require File.expand_path('../../lib/ramaze/asset', __FILE__)
2
+ require File.expand_path('../../lib/ramaze/asset/spec/helper', __FILE__)
3
+
4
+ Ramaze.options.roots.push(__DIR__('fixtures'))
@@ -0,0 +1,69 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ describe('Ramaze::Asset::CSS') do
4
+ behaves_like :asset_manager
5
+
6
+ before do
7
+ @file = '/css/github'
8
+ @file_1 = '/css/reset'
9
+ @cache = __DIR__('../fixtures/public/minified')
10
+ @public = __DIR__('../fixtures/public')
11
+ end
12
+
13
+ it('Build a single file') do
14
+ group = Ramaze::Asset::CSS.new(
15
+ [@file],
16
+ :cache_path => @cache,
17
+ :paths => [@public]
18
+ )
19
+
20
+ group.build_html.should \
21
+ === %Q{<link rel="stylesheet" href="#{@file}.css" type="text/css" />}
22
+
23
+ # Now minify it
24
+ group = Ramaze::Asset::CSS.new(
25
+ [@file],
26
+ :cache_path => @cache,
27
+ :paths => [@public],
28
+ :minify => true,
29
+ :name => 'github'
30
+ )
31
+
32
+ group.build
33
+
34
+ File.size?(File.join(@cache, 'github.min.css')).should != false
35
+
36
+ group.build_html.should \
37
+ === '<link rel="stylesheet" href="/minified/github.min.css" ' \
38
+ 'type="text/css" />'
39
+ end
40
+
41
+ it('Build multiple files') do
42
+ group = Ramaze::Asset::CSS.new(
43
+ [@file, @file_1],
44
+ :cache_path => @cache,
45
+ :paths => [@public]
46
+ )
47
+
48
+ group.build_html.should \
49
+ === %Q{<link rel="stylesheet" href="#{@file}.css" type="text/css" />} +
50
+ %Q{<link rel="stylesheet" href="#{@file_1}.css" type="text/css" />}
51
+
52
+ # Now minify it
53
+ group = Ramaze::Asset::CSS.new(
54
+ [@file, @file_1],
55
+ :cache_path => @cache,
56
+ :paths => [@public],
57
+ :minify => true,
58
+ :name => 'github'
59
+ )
60
+
61
+ group.build
62
+
63
+ File.size?(File.join(@cache, 'github.min.css')).should != false
64
+
65
+ group.build_html.should \
66
+ === '<link rel="stylesheet" href="/minified/github.min.css" ' \
67
+ 'type="text/css" />'
68
+ end
69
+ end
@@ -0,0 +1,148 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ SpecEnv = Ramaze::Asset::Environment.new(
4
+ :cache_path => __DIR__('../fixtures/public/minified')
5
+ )
6
+
7
+ class SpecEnvironment < Ramaze::Controller
8
+ map '/'
9
+
10
+ def index
11
+ SpecEnv.build_html(:javascript)
12
+ end
13
+
14
+ def specific_method
15
+ SpecEnv.build_html(:javascript)
16
+ end
17
+ end
18
+
19
+ class SpecEnvironment2 < Ramaze::Controller
20
+ map '/2'
21
+
22
+ def index
23
+ SpecEnv.build_html(:javascript)
24
+ end
25
+ end
26
+
27
+ describe('Ramaze::Asset::Environment') do
28
+ behaves_like :asset_manager
29
+
30
+ after do
31
+ SpecEnv.reset!
32
+ end
33
+
34
+ it('Raise for an invalid cache path') do
35
+ should.raise?(Ramaze::Asset::AssetError) do
36
+ env = Ramaze::Asset::Environment.new(:cache_path => 'foobar')
37
+ end
38
+ end
39
+
40
+ it('Raise when no public directories are set') do
41
+ old_publics = Ramaze.options.publics.dup
42
+ Ramaze.options.publics = []
43
+
44
+ should.raise?(Ramaze::Asset::AssetError) do
45
+ env = Ramaze::Asset::Environment.new(
46
+ :cache_path => __DIR__('../fixtures/public')
47
+ )
48
+ end
49
+
50
+ Ramaze.options.publics = old_publics
51
+ end
52
+
53
+ it('Generate the possible paths') do
54
+ path = __DIR__('../fixtures/public')
55
+ env = Ramaze::Asset::Environment.new(:cache_path => path)
56
+
57
+ env.instance_variable_get(:@file_group_options)[:paths].include?(path) \
58
+ .should === true
59
+ end
60
+
61
+ it('Register a new type') do
62
+ Ramaze::Asset::Environment.register_type(:struct, Struct)
63
+
64
+ Ramaze::Asset::Environment::Types.key?(:struct).should === true
65
+ Ramaze::Asset::Environment::Types[:struct].should == Struct
66
+
67
+ should.raise?(Ramaze::Asset::AssetError) do
68
+ Ramaze::Asset::Environment.register_type(:struct, Struct)
69
+ end
70
+
71
+ Ramaze::Asset::Environment::Types.delete(:struct)
72
+ end
73
+
74
+ it('Globally disable minifying of files') do
75
+ env = Ramaze::Asset::Environment.new(
76
+ :cache_path => __DIR__('../fixtures/public/minified'),
77
+ :minify => false
78
+ )
79
+
80
+ env.serve(:javascript, ['js/mootools_core'], :minify => true)
81
+ env.serve(:css, ['css/github'])
82
+
83
+ env.files[:javascript][:global][:__all][0].options[:minify].should === false
84
+ env.files[:css][:global][:__all][0].options[:minify].should === false
85
+ end
86
+
87
+ it('Serve a file globally') do
88
+ SpecEnv.serve(:javascript, ['js/mootools_core'], :controller => :global)
89
+
90
+ body1 = get('/').body
91
+ body2 = get('/2').body
92
+
93
+ body1.empty?.should === false
94
+ body1.should === body2
95
+
96
+ body1.include?('js/mootools_core.js').should === true
97
+ end
98
+
99
+ it('Serve a file for a specific controller') do
100
+ SpecEnv.serve(
101
+ :javascript,
102
+ ['/js/mootools_core'],
103
+ :controller => SpecEnvironment
104
+ )
105
+
106
+ body1 = get('/').body
107
+ body2 = get('/2').body
108
+
109
+ body1.should != body2
110
+
111
+ body1.include?('js/mootools_core.js').should === true
112
+ body2.include?('js/mootools_core.js').should === false
113
+ end
114
+
115
+ it('Serve a file for a specific method') do
116
+ SpecEnv.serve(
117
+ :javascript,
118
+ ['/js/mootools_core'],
119
+ :controller => SpecEnvironment,
120
+ :methods => [:specific_method]
121
+ )
122
+
123
+ body1 = get('/').body
124
+ body2 = get('/specific_method').body
125
+
126
+ body1.should != body2
127
+
128
+ body1.include?('/js/mootools_core.js').should === false
129
+ body2.include?('/js/mootools_core.js').should === true
130
+ end
131
+
132
+ it('Add an asset group and load it') do
133
+ SpecEnv.register_asset_group(:spec) do |env|
134
+ env.serve(:css, ['css/github', 'css/reset'])
135
+ end
136
+
137
+ SpecEnv.files[:css].nil?.should === true
138
+
139
+ should.not.raise?(Ramaze::Asset::AssetError) do
140
+ SpecEnv.load_asset_group(:spec)
141
+ end
142
+
143
+ files = SpecEnv.files[:css][:global][:__all][0].files
144
+
145
+ files.include?('/css/github.css').should === true
146
+ files.include?('/css/reset.css').should === true
147
+ end
148
+ end
@@ -0,0 +1,118 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ class SpecFileGroup < Ramaze::Asset::FileGroup
4
+ extension '.js'
5
+
6
+ def minify(input)
7
+ return input
8
+ end
9
+
10
+ def html_tag(gestalt, path)
11
+ gestalt.p(path)
12
+ end
13
+ end
14
+
15
+ class SpecFileGroup2 < Ramaze::Asset::FileGroup
16
+ extension '.js'
17
+ end
18
+
19
+ describe('Ramaze::Asset::FileGroup') do
20
+ behaves_like :asset_manager
21
+
22
+ before do
23
+ @file = '/js/mootools_core'
24
+ @file_1 = '/js/mootools_more'
25
+ @cache = __DIR__('../fixtures/public/minified')
26
+ @public = __DIR__('../fixtures/public')
27
+ end
28
+
29
+ it('Initialize with invalid data') do
30
+ should.raise?(Ramaze::Asset::AssetError) do
31
+ Ramaze::Asset::FileGroup.new([])
32
+ end
33
+
34
+ should.raise?(Ramaze::Asset::AssetError) do
35
+ Ramaze::Asset::FileGroup.new([@file], :cache_path => __DIR__('foobar'))
36
+ end
37
+
38
+ # No extension specified
39
+ should.raise?(Ramaze::Asset::AssetError) do
40
+ Ramaze::Asset::FileGroup.new([@file], :cache_path => @public)
41
+ end
42
+ end
43
+
44
+ it('Generate a name for the minified file') do
45
+ name = Digest::SHA1.new.hexdigest(@file + '.js') + '.min.js'
46
+ group = SpecFileGroup.new(
47
+ [@file],
48
+ :cache_path => @cache,
49
+ :paths => [@public],
50
+ :minify => true
51
+ )
52
+
53
+ group.options[:name].should === name
54
+ group.extension[:source].should === '.js'
55
+ group.extension[:minified].should === '.min.js'
56
+ end
57
+
58
+ it('Build a single file') do
59
+ group = SpecFileGroup.new(
60
+ [@file],
61
+ :cache_path => @cache,
62
+ :paths => [@public]
63
+ )
64
+
65
+ group.build_html.should === "<p>#{@file}.js</p>"
66
+
67
+ # Minify it this time.
68
+ group = SpecFileGroup.new(
69
+ [@file],
70
+ :cache_path => @cache,
71
+ :paths => [@public],
72
+ :name => 'spec',
73
+ :minify => true
74
+ )
75
+
76
+ group.build
77
+
78
+ content = File.read(File.join(@public, @file + '.js'))
79
+ minified = File.read(File.join(@cache, 'spec.min.js'))
80
+
81
+ File.size?(File.join(@cache, 'spec.min.js')).should != false
82
+ minified.include?(content).should === true
83
+
84
+ group.build_html.should === '<p>/minified/spec.min.js</p>'
85
+ end
86
+
87
+ it('Build multiple files') do
88
+ group = SpecFileGroup.new(
89
+ [@file, @file_1],
90
+ :cache_path => @cache,
91
+ :paths => [@public]
92
+ )
93
+
94
+ group.build_html.should === "<p>#{@file}.js</p><p>#{@file_1}.js</p>"
95
+
96
+ # Minify it this time.
97
+ group = SpecFileGroup.new(
98
+ [@file, @file_1],
99
+ :cache_path => @cache,
100
+ :paths => [@public],
101
+ :name => 'spec',
102
+ :minify => true
103
+ )
104
+
105
+ group.build
106
+
107
+ content = File.read(File.join(@public, @file + '.js'))
108
+ content_1 = File.read(File.join(@public, @file_1 + '.js'))
109
+ minified = File.read(File.join(@cache, 'spec.min.js'))
110
+
111
+ File.size?(File.join(@cache, 'spec.min.js')).should != false
112
+
113
+ minified.include?(content).should === true
114
+ minified.include?(content_1).should === true
115
+
116
+ group.build_html.should === "<p>/minified/spec.min.js</p>"
117
+ end
118
+ end
@@ -0,0 +1,69 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ describe('Ramaze::Asset::Javascript') do
4
+ behaves_like :asset_manager
5
+
6
+ before do
7
+ @file = '/js/mootools_core'
8
+ @file_1 = '/js/mootools_more'
9
+ @cache = __DIR__('../fixtures/public/minified')
10
+ @public = __DIR__('../fixtures/public')
11
+ end
12
+
13
+ it('Build a single file') do
14
+ group = Ramaze::Asset::Javascript.new(
15
+ [@file],
16
+ :cache_path => @cache,
17
+ :paths => [@public]
18
+ )
19
+
20
+ group.build_html.should \
21
+ === %Q{<script src="#{@file}.js" type="text/javascript"></script>}
22
+
23
+ # Now minify it
24
+ group = Ramaze::Asset::Javascript.new(
25
+ [@file],
26
+ :cache_path => @cache,
27
+ :paths => [@public],
28
+ :minify => true,
29
+ :name => 'mootools'
30
+ )
31
+
32
+ group.build
33
+
34
+ File.size?(File.join(@cache, 'mootools.min.js')).should != false
35
+
36
+ group.build_html.should \
37
+ === '<script src="/minified/mootools.min.js" ' \
38
+ 'type="text/javascript"></script>'
39
+ end
40
+
41
+ it('Build multiple files') do
42
+ group = Ramaze::Asset::Javascript.new(
43
+ [@file, @file_1],
44
+ :cache_path => @cache,
45
+ :paths => [@public]
46
+ )
47
+
48
+ group.build_html.should \
49
+ === %Q{<script src="#{@file}.js" type="text/javascript"></script>} +
50
+ %Q{<script src="#{@file_1}.js" type="text/javascript"></script>}
51
+
52
+ # Now minify it
53
+ group = Ramaze::Asset::Javascript.new(
54
+ [@file, @file_1],
55
+ :cache_path => @cache,
56
+ :paths => [@public],
57
+ :minify => true,
58
+ :name => 'mootools'
59
+ )
60
+
61
+ group.build
62
+
63
+ File.size?(File.join(@cache, 'mootools.min.js')).should != false
64
+
65
+ group.build_html.should \
66
+ === '<script src="/minified/mootools.min.js" ' \
67
+ 'type="text/javascript"></script>'
68
+ end
69
+ end
data/task/build.rake ADDED
@@ -0,0 +1,35 @@
1
+ # Task group used for building various elements such as the Gem and the
2
+ # documentation.
3
+ namespace :build do
4
+ desc 'Builds the documentation using YARD'
5
+ task :doc do
6
+ gem_path = File.expand_path('../../', __FILE__)
7
+ command = "yard doc #{gem_path}/lib -m markdown -M rdiscount -o #{gem_path}/doc "
8
+ command += "-r #{gem_path}/README.md --private --protected"
9
+
10
+ sh(command)
11
+ end
12
+
13
+ desc 'Builds a new Gem'
14
+ task :gem do
15
+ gem_path = File.expand_path('../../', __FILE__)
16
+ gemspec_path = File.join(
17
+ gem_path,
18
+ "#{Ramaze::Asset::Gemspec.name}-" \
19
+ "#{Ramaze::Asset::Gemspec.version.version}.gem"
20
+ )
21
+
22
+ pkg_path = File.join(
23
+ gem_path,
24
+ 'pkg',
25
+ "#{Ramaze::Asset::Gemspec.name}-" \
26
+ "#{Ramaze::Asset::Gemspec.version.version}.gem"
27
+ )
28
+
29
+ # Build and install the gem
30
+ sh('gem', 'build' , File.join(gem_path, 'ramaze-asset.gemspec'))
31
+ sh('mv' , gemspec_path, pkg_path)
32
+ sh('gem', 'install' , pkg_path)
33
+ end
34
+ end # namespace :build
35
+