adamh-asset_library 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,12 +27,13 @@ class AssetLibrary
27
27
  def assets_with_extra_suffix(extra_suffix)
28
28
  return nil unless config
29
29
 
30
- ret = []
31
- config[:files].each do |requested_file|
32
- ret.concat(assets_for_pattern(requested_file, extra_suffix))
33
- end
34
- ret.uniq!
35
- ret
30
+ GlobFu.find(
31
+ config[:files],
32
+ :suffix => config[:suffix],
33
+ :extra_suffix => extra_suffix,
34
+ :root => File.join(*([AssetLibrary.root, config[:base]].compact)),
35
+ :optional_suffix => config[:optional_suffix]
36
+ ).collect { |f| Asset.new(f) }
36
37
  end
37
38
 
38
39
  # Returns an Array of Assets to include.
data/lib/asset_library.rb CHANGED
@@ -1,3 +1,10 @@
1
+ begin
2
+ require 'glob_fu'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'glob_fu'
6
+ end
7
+
1
8
  require File.dirname(__FILE__) + '/asset_library/asset_module'
2
9
  require File.dirname(__FILE__) + '/asset_library/util'
3
10
 
@@ -1,64 +1,67 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  require 'set'
4
- require 'rglob'
5
4
 
6
5
  describe(AssetLibrary::AssetModule) do
7
6
  before(:each) do
8
- AssetLibrary.stub!(:root).and_return('/')
7
+ AssetLibrary.stub!(:root).and_return(prefix)
8
+ end
9
+
10
+ after(:each) do
11
+ wipe_fs
9
12
  end
10
13
 
11
14
  describe('#assets') do
12
15
  it('should include file1 and file2') do
13
16
  files = [ '/c/file1.css', '/c/file2.css' ]
14
17
  stub_fs(files)
15
- m(css_config(:files => ['file1', 'file2'])).assets.collect{|a| a.absolute_path}.should == files
18
+ m(css_config(:files => ['file1', 'file2'])).assets.collect{|a| a.absolute_path}.should == ["#{prefix}/c/file1.css", "#{prefix}/c/file2.css"]
16
19
  end
17
20
 
18
21
  it('should not include file2 if that does not exist') do
19
22
  files = [ '/c/file1.css' ]
20
23
  stub_fs(files)
21
- m(css_config(:files => ['file1', 'file2'])).assets.collect{|a| a.absolute_path}.should == files
24
+ m(css_config(:files => ['file1', 'file2'])).assets.collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file1.css" ]
22
25
  end
23
26
 
24
27
  it('should not include other files') do
25
28
  files = [ '/c/file1.css', '/c/file2.css' ]
26
29
  stub_fs(files)
27
- m(css_config(:files => ['file1'])).assets.collect{|a| a.absolute_path}.should == [ files.first ]
30
+ m(css_config(:files => ['file1'])).assets.collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file1.css" ]
28
31
  end
29
32
 
30
33
  it('should glob filenames') do
31
34
  files = [ '/c/file1.css', '/c/file2.css', '/c/other_file.css' ]
32
35
  stub_fs(files)
33
- m(css_config(:files => ['file*'])).assets.collect{|a| a.absolute_path}.should == files[0..1]
36
+ m(css_config(:files => ['file*'])).assets.collect{|a| a.absolute_path}.should == ["#{prefix}/c/file1.css", "#{prefix}/c/file2.css"]
34
37
  end
35
38
 
36
39
  it('should glob directories') do
37
40
  files = [ '/c/file1.css', '/c/a/file2.css', '/c/b/a/file3.css' ]
38
41
  stub_fs(files)
39
- m(css_config(:files => ['**/file*'])).assets.collect{|a| a.absolute_path}.should == files[1..2]
42
+ m(css_config(:files => ['**/file*'])).assets.collect{|a| a.absolute_path}.should == ["#{prefix}/c/a/file2.css", "#{prefix}/c/b/a/file3.css", "#{prefix}/c/file1.css"]
40
43
  end
41
44
 
42
45
  it('should use :optional_suffix when appropriate') do
43
- files = [ '/c/file1.css', '/c/file1.o.css' ]
46
+ files = [ '/c/file1.css', '/c/file1.css.o' ]
44
47
  stub_fs(files)
45
- m(css_config(:optional_suffix => 'o', :files => ['file1'])).assets.collect{|a| a.absolute_path}.should == files[1..1]
48
+ m(css_config(:optional_suffix => 'o', :files => ['file1'])).assets.collect{|a| a.absolute_path}.should == ["#{prefix}/c/file1.css.o"]
46
49
  end
47
50
 
48
- it('should not show :optional_suffix file if original is absent') do
49
- files = [ '/c/file1.o.css' ]
51
+ it('should show :optional_suffix file even if original is absent') do
52
+ files = [ '/c/file1.css.o' ]
50
53
  stub_fs(files)
51
- m(css_config(:optional_suffix => 'o', :files => ['file1'])).assets.collect{|a| a.absolute_path}.should == []
54
+ m(css_config(:optional_suffix => 'o', :files => ['file1'])).assets.collect{|a| a.absolute_path}.should == ["#{prefix}/c/file1.css.o"]
52
55
  end
53
56
 
54
57
  it('should ignore :optional_suffix when suffixed file is not present') do
55
58
  stub_fs([ '/c/file1.css' ])
56
- m(css_config(:optional_suffix => 'o', :files => ['file1'])).assets.collect{|a| a.absolute_path}.should == [ '/c/file1.css' ]
59
+ m(css_config(:optional_suffix => 'o', :files => ['file1'])).assets.collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file1.css" ]
57
60
  end
58
61
 
59
62
  it('should pick files with :extra_suffix') do
60
63
  stub_fs([ '/c/file1.e.css' ])
61
- m(css_config(:files => ['file1'])).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == [ '/c/file1.e.css' ]
64
+ m(css_config(:files => ['file1'])).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file1.e.css" ]
62
65
  end
63
66
 
64
67
  it('should ignore non-suffixed files when :extra_suffix is set') do
@@ -68,22 +71,22 @@ describe(AssetLibrary::AssetModule) do
68
71
 
69
72
  it('should use extra suffixes with format') do
70
73
  stub_fs([ '/c/file1.e1.css', '/c/file1.e2.css' ])
71
- m(css_config(:files => ['file1'], :formats => { :f1 => [ 'e1', 'e2' ] })).assets_with_format(:f1).collect{|a| a.absolute_path}.should == [ '/c/file1.e1.css', '/c/file1.e2.css' ]
74
+ m(css_config(:files => ['file1'], :formats => { :f1 => [ 'e1', 'e2' ] })).assets_with_format(:f1).collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file1.e1.css", "#{prefix}/c/file1.e2.css" ]
72
75
  end
73
76
 
74
77
  it('should ignore extra suffixes unspecified in format') do
75
78
  stub_fs([ '/c/file1.e1.css', '/c/file1.e2.css' ])
76
- m(css_config(:files => ['file1'], :formats => { :f1 => [ 'e1' ] })).assets_with_format(:f1).collect{|a| a.absolute_path}.should == [ '/c/file1.e1.css' ]
79
+ m(css_config(:files => ['file1'], :formats => { :f1 => [ 'e1' ] })).assets_with_format(:f1).collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file1.e1.css" ]
77
80
  end
78
81
 
79
82
  it('should allow nil suffixes in format') do
80
83
  stub_fs([ '/c/file1.css', '/c/file1.e1.css' ])
81
- m(css_config(:files => ['file1'], :formats => { :f1 => [nil, 'e1'] })).assets_with_format(:f1).collect{|a| a.absolute_path}.should == ['/c/file1.css', '/c/file1.e1.css' ]
84
+ m(css_config(:files => ['file1'], :formats => { :f1 => [nil, 'e1'] })).assets_with_format(:f1).collect{|a| a.absolute_path}.should == ["#{prefix}/c/file1.css", "#{prefix}/c/file1.e1.css" ]
82
85
  end
83
86
 
84
87
  it('should combine :extra_suffix with :optional_suffix') do
85
- stub_fs([ '/c/file1.e.css', '/c/file1.e.o.css' ])
86
- m(css_config(:files => ['file1'], :optional_suffix => 'o')).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == [ '/c/file1.e.o.css' ]
88
+ stub_fs([ '/c/file1.e.css', '/c/file1.e.css.o' ])
89
+ m(css_config(:files => ['file1'], :optional_suffix => 'o')).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file1.e.css.o" ]
87
90
  end
88
91
 
89
92
  it('should ignore too many dots when globbing') do
@@ -93,17 +96,17 @@ describe(AssetLibrary::AssetModule) do
93
96
 
94
97
  it('should pick files with :extra_suffix when globbing') do
95
98
  stub_fs([ '/c/file1.e.css', '/c/file2.css' ])
96
- m(css_config(:files => ['file*'])).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == [ '/c/file1.e.css' ]
99
+ m(css_config(:files => ['file*'])).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file1.e.css" ]
97
100
  end
98
101
 
99
102
  it('should pick files with :optional_suffix when globbing') do
100
- stub_fs([ '/c/file.css', '/c/file.o.css' ])
101
- m(css_config(:optional_suffix => 'o', :files => ['file*'])).assets.collect{|a| a.absolute_path}.should == [ '/c/file.o.css' ]
103
+ stub_fs([ '/c/file.css', '/c/file.css.o' ])
104
+ m(css_config(:optional_suffix => 'o', :files => ['file*'])).assets.collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file.css.o" ]
102
105
  end
103
106
 
104
107
  it('should pick files with both :extra_suffix and :optional_suffix when globbing') do
105
- stub_fs([ '/c/file.css', '/c/file.e.css', '/c/file.e.o.css' ])
106
- m(css_config(:optional_suffix => 'o', :files => ['file*'])).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == [ '/c/file.e.o.css' ]
108
+ stub_fs([ '/c/file.css', '/c/file.e.css', '/c/file.e.css.o' ])
109
+ m(css_config(:optional_suffix => 'o', :files => ['file*'])).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == [ "#{prefix}/c/file.e.css.o" ]
107
110
  end
108
111
  end
109
112
 
@@ -115,52 +118,49 @@ describe(AssetLibrary::AssetModule) do
115
118
 
116
119
  it('should concatenate individual file contents') do
117
120
  stub_fs([ '/c/file1.css', '/c/file2.css' ])
118
- m(css_config(:files => ['file*'])).contents.read.should == '/c/file1.css/c/file2.css'
121
+ m(css_config(:files => ['file*'])).contents.read.should == "/c/file1.css\n/c/file2.css\n"
119
122
  end
120
123
  end
121
124
 
122
125
  describe('#cache_asset') do
123
126
  it('should use options[:cache]') do
124
- m(css_config).cache_asset.absolute_path.should == '/c/cache.css'
127
+ m(css_config).cache_asset.absolute_path.should == "#{prefix}/c/cache.css"
125
128
  end
126
129
 
127
130
  it('should use :format if set') do
128
- m(css_config).cache_asset(:e).absolute_path.should == '/c/cache.e.css'
131
+ m(css_config).cache_asset(:e).absolute_path.should == "#{prefix}/c/cache.e.css"
129
132
  end
130
133
  end
131
134
 
132
135
  describe('#write_cache') do
133
136
  it('should write to cache.css') do
134
- File.should_receive(:open).with('/c/cache.css', 'w')
137
+ File.should_receive(:open).with("#{prefix}/c/cache.css", 'w')
135
138
  m(css_config).write_cache
136
139
  end
137
140
 
138
141
  it('should write cache contents to cache') do
139
- f = StringIO.new
140
- File.stub!(:open).with('/c/cache.css', 'w').and_yield(f)
141
142
  stub_fs([ '/c/file1.css', '/c/file2.css' ])
142
143
  m(css_config(:files => ['file*'])).write_cache
143
- f.rewind
144
- f.read.should == '/c/file1.css/c/file2.css'
144
+ File.open("#{prefix}/c/cache.css") { |f| f.read.should == "/c/file1.css\n/c/file2.css\n" }
145
145
  end
146
146
 
147
147
  it('should use :format to determine CSS output file') do
148
- File.should_receive(:open).with('/c/cache.e.css', 'w')
148
+ File.should_receive(:open).with("#{prefix}/c/cache.e.css", 'w')
149
149
  m(css_config).write_cache(:e)
150
150
  end
151
151
  end
152
152
 
153
153
  describe('#write_all_caches') do
154
154
  it('should write cache.css (no :format)') do
155
- File.should_receive(:open).with('/c/cache.css', 'w')
155
+ File.should_receive(:open).with("#{prefix}/c/cache.css", 'w')
156
156
  m(css_config).write_all_caches
157
157
  end
158
158
 
159
159
  it('should write no-format and all format files') do
160
160
  formats = { :e1 => [], :e2 => [] }
161
- File.should_receive(:open).with('/c/cache.css', 'w')
161
+ File.should_receive(:open).with("#{prefix}/c/cache.css", 'w')
162
162
  formats.keys.each do |format|
163
- File.should_receive(:open).with("/c/cache.#{format}.css", 'w')
163
+ File.should_receive(:open).with("#{prefix}/c/cache.#{format}.css", 'w')
164
164
  end
165
165
  m(css_config(:formats => formats)).write_all_caches
166
166
  end
@@ -190,19 +190,27 @@ describe(AssetLibrary::AssetModule) do
190
190
  }.merge(options)
191
191
  end
192
192
 
193
- def stub_fs(filenames)
194
- filenames = Set.new(filenames)
195
- File.stub!(:exist?).and_return do |path|
196
- filenames.include?(path)
197
- end
193
+ def prefix
194
+ @prefix ||= File.dirname(__FILE__) + '/deleteme'
195
+ end
198
196
 
199
- filenames.each do |path|
200
- File.stub!(:open).with(path, 'r').and_yield(StringIO.new(path))
197
+ def stub_fs(filenames)
198
+ wipe_fs
199
+ FileUtils.mkdir(prefix)
200
+
201
+ filenames.each do |file|
202
+ path = File.join(prefix, file)
203
+ dir = File.dirname(path)
204
+ unless File.exist?(dir)
205
+ FileUtils.mkdir_p(dir)
206
+ end
207
+ File.open(path, 'w') { |f| f.write("#{file}\n") }
201
208
  end
209
+ end
202
210
 
203
- Dir.stub!(:glob).and_return do |path|
204
- glob = RGlob::Glob.new(path)
205
- filenames.select { |f| glob.match(f) }
211
+ def wipe_fs
212
+ if File.exist?(prefix)
213
+ FileUtils.rm_r(prefix)
206
214
  end
207
215
  end
208
216
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adamh-asset_library
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - adamh
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-18 00:00:00 -07:00
12
+ date: 2009-08-19 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: adamh-glob_fu
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.4
24
+ version:
16
25
  description:
17
26
  email: adam@adamhooper.com
18
27
  executables: []