adamh-asset_library 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -32,7 +32,7 @@ Once configured, asset_library provides two helper methods for your views:
32
32
 
33
33
  <%# outputs library.ie6.css (production) or its files (development) %>
34
34
  <!--[if lte IE 6]>
35
- <%= asset_library_stylesheet_tags(:stylesheets, 'ie6') %>
35
+ <%= asset_library_stylesheet_tags(:stylesheets, :ie6) %>
36
36
  <![endif]-->
37
37
 
38
38
  Both helpers behave differently depending on whether
@@ -92,7 +92,10 @@ should be in <tt>config/asset_library.yml</tt>.
92
92
  cache: library
93
93
  base: stylesheets
94
94
  suffix: css
95
- extra_suffixes: [ie6, ie7, ie8]
95
+ formats:
96
+ - ie6: [null, ie8, ie7, ie6]
97
+ - ie7: [null, ie8, ie7]
98
+ - ie8: [null, ie8]
96
99
  files:
97
100
  - reset
98
101
  - application
@@ -104,7 +107,9 @@ should be in <tt>config/asset_library.yml</tt>.
104
107
  # cache: cache_file
105
108
  # base: base_path_of_assets_in_web_root
106
109
  # suffix: suffix ("css" or "js")
107
- # extra_suffixes: ["other_bundle_suffix_1", "other_bundle_suffix_2"]
110
+ # formats:
111
+ # format1: ["extra_suffix_1", "extra_suffix_2"]
112
+ # format2: [null, "extra_suffix3"]
108
113
  # optional_suffix: optional_suffix
109
114
  # files:
110
115
  # - file1_relative_to_base
@@ -128,14 +133,14 @@ should be <tt>stylesheets</tt>.
128
133
  <tt>suffix</tt> is either "js" or "css", depending on whether you are including
129
134
  JavaScript or CSS files.
130
135
 
131
- <tt>extra_suffixes</tt> allows construction of parallel modules. If you specify
132
- <tt>extra_suffixes</tt> as ['ie6', 'ie7'], <tt>files</tt> as <tt>file1</tt> and
133
- <tt>file2</tt>, <tt>module_name</tt> as <tt>module</tt>, and <tt>suffix</tt> as
134
- <tt>css</tt>, then three modules will be created (ignoring nonexistent files):
135
-
136
- * <tt>module.css</tt>, the concatenation of <tt>file1.css</tt> and <tt>file2.css</tt>
137
- * <tt>module.ie6.css</tt>, the concatenation of <tt>file1.ie6.css</tt> and <tt>file2.ie6.css</tt>
138
- * <tt>module.ie7.css</tt>, the concatenation of <tt>file1.ie7.css</tt> and <tt>file2.ie7.css</tt>
136
+ <tt>formats</tt> allows construction of parallel modules. By default, for a
137
+ module named <tt>styles</tt> will use <tt>*.css</tt> (where <tt>*</tt> is
138
+ specified by the <tt>files</tt> option) to generate <tt>styles.css</tt>; but
139
+ filenames of the format <tt>*.suffix.css</tt> will be ignored in that search.
140
+ If a <tt>format</tt> called <tt>format1</tt> is specified as
141
+ <tt>[suffix1, suffix2]</tt>, then <tt>*.suffix1.css</tt> and
142
+ <tt>*.suffix2.css</tt> will be included, but not <tt>*.css</tt>. (To specify
143
+ <tt>*.css</tt>, put <tt>null</tt> in the format list.)
139
144
 
140
145
  <tt>optional_suffix</tt> will cause asset_library to check for the existence of
141
146
  files with <tt>optional_suffix</tt> suffixes, falling back to files without
@@ -152,9 +157,8 @@ directory, and they will be included instead, for optimal download speed.
152
157
  <tt>files</tt> is a list of files, relative to <tt>base</tt>. File globbing is
153
158
  allowed; <tt>**</tt> expands to "any nesting of directories". Files which do
154
159
  not exist will be excluded; globbing will include/exclude files with
155
- <tt>extra_suffixes</tt> as appropriate; and files without
156
- <tt>optional_suffix</tt> will not be output alongside files with the same name
157
- endowed with the suffix.
160
+ <tt>formats</tt> as appropriate; and files without <tt>optional_suffix</tt>
161
+ will not be output alongside files with the same name endowed with the suffix.
158
162
 
159
163
  == Copyright
160
164
 
@@ -12,7 +12,19 @@ class AssetLibrary
12
12
  #
13
13
  # Arguments:
14
14
  # extra_suffix: if set, finds files with the given extra suffix
15
- def assets(extra_suffix = nil)
15
+ def assets(format = nil)
16
+ if format
17
+ assets_with_format(format)
18
+ else
19
+ assets_with_extra_suffix(nil)
20
+ end
21
+ end
22
+
23
+ # Returns an Array of Assets to include.
24
+ #
25
+ # Arguments:
26
+ # extra_suffix: if set, finds files with the given extra suffix
27
+ def assets_with_extra_suffix(extra_suffix)
16
28
  return nil unless config
17
29
 
18
30
  ret = []
@@ -23,10 +35,23 @@ class AssetLibrary
23
35
  ret
24
36
  end
25
37
 
26
- def contents(extra_suffix = nil)
38
+ # Returns an Array of Assets to include.
39
+ #
40
+ # Calls assets_with_extra_suffix for each suffix in the given format
41
+ #
42
+ # Arguments:
43
+ # format: format specified in the config
44
+ def assets_with_format(format)
45
+ return nil unless config
46
+
47
+ extra_suffixes = config[:formats][format.to_sym]
48
+ extra_suffixes.inject([]) { |r, s| r.concat(assets_with_extra_suffix(s)) }
49
+ end
50
+
51
+ def contents(format = nil)
27
52
  s = StringIO.new
28
53
 
29
- assets(extra_suffix).each do |asset|
54
+ assets(format).each do |asset|
30
55
  File.open(asset.absolute_path, 'r') do |infile|
31
56
  s.write(infile.read)
32
57
  end
@@ -37,21 +62,21 @@ class AssetLibrary
37
62
  end
38
63
 
39
64
  # Returns an Asset representing the cache file
40
- def cache_asset(extra_suffix = nil)
41
- extra = extra_suffix ? ".#{extra_suffix}" : ''
65
+ def cache_asset(format = nil)
66
+ extra = format ? ".#{format}" : ''
42
67
  Asset.new(File.join(AssetLibrary.root, config[:base], "#{config[:cache]}#{extra}.#{config[:suffix]}"))
43
68
  end
44
69
 
45
- def write_cache(extra_suffix = nil)
46
- File.open(cache_asset(extra_suffix).absolute_path, 'w') do |outfile|
47
- outfile.write(contents(extra_suffix).read)
70
+ def write_cache(format = nil)
71
+ File.open(cache_asset(format).absolute_path, 'w') do |outfile|
72
+ outfile.write(contents(format).read)
48
73
  end
49
74
  end
50
75
 
51
76
  def write_all_caches
52
- cache_suffixes = [ nil ] + (config[:extra_suffixes] || [])
53
- cache_suffixes.each do |extra_suffix|
54
- write_cache(extra_suffix)
77
+ write_cache
78
+ (config[:formats] || {}).keys.each do |format|
79
+ write_cache(format)
55
80
  end
56
81
  end
57
82
 
@@ -87,9 +112,9 @@ class AssetLibrary
87
112
  def path_contains_extra_dot?(path, requested_file, extra_suffix)
88
113
  allowed_suffixes = []
89
114
 
90
- allowed_suffixes << "\\.#{Regexp.quote(extra_suffix)}" if extra_suffix
91
- allowed_suffixes << "(\\.#{Regexp.quote(config[:optional_suffix])})?" if config[:optional_suffix]
92
- allowed_suffixes << "\\.#{Regexp.quote(config[:suffix])}" if config[:suffix]
115
+ allowed_suffixes << "\\.#{Regexp.quote(extra_suffix.to_s)}" if extra_suffix
116
+ allowed_suffixes << "(\\.#{Regexp.quote(config[:optional_suffix].to_s)})?" if config[:optional_suffix]
117
+ allowed_suffixes << "\\.#{Regexp.quote(config[:suffix].to_s)}" if config[:suffix]
93
118
 
94
119
  basename = File.basename(path)
95
120
  requested_basename = File.basename(requested_file)
@@ -1,22 +1,22 @@
1
1
  class AssetLibrary
2
2
  module Helpers
3
- def asset_library_javascript_tags(module_key)
3
+ def asset_library_javascript_tags(module_key, format = nil)
4
4
  m = AssetLibrary.asset_module(module_key)
5
5
  if AssetLibrary.cache
6
6
  @@asset_library_javascript_tags_cache ||= {}
7
7
  @@asset_library_javascript_tags_cache[module_key] ||= script_tag(m.cache_asset.relative_url)
8
8
  else
9
- m.assets.collect{|a| script_tag(a.relative_url)}.join("\n")
9
+ m.assets(format).collect{|a| script_tag(a.relative_url)}.join("\n")
10
10
  end
11
11
  end
12
12
 
13
- def asset_library_stylesheet_tags(module_key, extra_suffix = nil)
13
+ def asset_library_stylesheet_tags(module_key, format = nil)
14
14
  m = AssetLibrary.asset_module(module_key)
15
15
  if AssetLibrary.cache
16
16
  @@asset_library_stylesheet_tags_cache ||= {}
17
- @@asset_library_stylesheet_tags_cache[[module_key, extra_suffix]] ||= style_tag(m.cache_asset(extra_suffix).relative_url)
17
+ @@asset_library_stylesheet_tags_cache[[module_key, format]] ||= style_tag(m.cache_asset(format).relative_url)
18
18
  else
19
- import_styles_tag(m.assets(extra_suffix).collect{|a| a.relative_url})
19
+ import_styles_tag(m.assets(format).collect{|a| a.relative_url})
20
20
  end
21
21
  end
22
22
 
@@ -58,17 +58,32 @@ describe(AssetLibrary::AssetModule) do
58
58
 
59
59
  it('should pick files with :extra_suffix') do
60
60
  stub_fs([ '/c/file1.e.css' ])
61
- m(css_config(:files => ['file1'])).assets('e').collect{|a| a.absolute_path}.should == [ '/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' ]
62
62
  end
63
63
 
64
64
  it('should ignore non-suffixed files when :extra_suffix is set') do
65
65
  stub_fs([ '/c/file1.css' ])
66
- m(css_config(:files => ['file1'])).assets('e').collect{|a| a.absolute_path}.should == []
66
+ m(css_config(:files => ['file1'])).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == []
67
+ end
68
+
69
+ it('should use extra suffixes with format') do
70
+ 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' ]
72
+ end
73
+
74
+ it('should ignore extra suffixes unspecified in format') do
75
+ 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' ]
77
+ end
78
+
79
+ it('should allow nil suffixes in format') do
80
+ 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' ]
67
82
  end
68
83
 
69
84
  it('should combine :extra_suffix with :optional_suffix') do
70
85
  stub_fs([ '/c/file1.e.css', '/c/file1.e.o.css' ])
71
- m(css_config(:files => ['file1'], :optional_suffix => 'o')).assets('e').collect{|a| a.absolute_path}.should == [ '/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' ]
72
87
  end
73
88
 
74
89
  it('should ignore too many dots when globbing') do
@@ -78,7 +93,7 @@ describe(AssetLibrary::AssetModule) do
78
93
 
79
94
  it('should pick files with :extra_suffix when globbing') do
80
95
  stub_fs([ '/c/file1.e.css', '/c/file2.css' ])
81
- m(css_config(:files => ['file*'])).assets('e').collect{|a| a.absolute_path}.should == [ '/c/file1.e.css' ]
96
+ m(css_config(:files => ['file*'])).assets_with_extra_suffix('e').collect{|a| a.absolute_path}.should == [ '/c/file1.e.css' ]
82
97
  end
83
98
 
84
99
  it('should pick files with :optional_suffix when globbing') do
@@ -88,7 +103,7 @@ describe(AssetLibrary::AssetModule) do
88
103
 
89
104
  it('should pick files with both :extra_suffix and :optional_suffix when globbing') do
90
105
  stub_fs([ '/c/file.css', '/c/file.e.css', '/c/file.e.o.css' ])
91
- m(css_config(:optional_suffix => 'o', :files => ['file*'])).assets('e').collect{|a| a.absolute_path}.should == [ '/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' ]
92
107
  end
93
108
  end
94
109
 
@@ -109,8 +124,8 @@ describe(AssetLibrary::AssetModule) do
109
124
  m(css_config).cache_asset.absolute_path.should == '/c/cache.css'
110
125
  end
111
126
 
112
- it('should use :extra_suffix if set') do
113
- m(css_config).cache_asset('e').absolute_path.should == '/c/cache.e.css'
127
+ it('should use :format if set') do
128
+ m(css_config).cache_asset(:e).absolute_path.should == '/c/cache.e.css'
114
129
  end
115
130
  end
116
131
 
@@ -129,25 +144,25 @@ describe(AssetLibrary::AssetModule) do
129
144
  f.read.should == '/c/file1.css/c/file2.css'
130
145
  end
131
146
 
132
- it('should use :extra_suffix to determine CSS output file') do
147
+ it('should use :format to determine CSS output file') do
133
148
  File.should_receive(:open).with('/c/cache.e.css', 'w')
134
- m(css_config).write_cache('e')
149
+ m(css_config).write_cache(:e)
135
150
  end
136
151
  end
137
152
 
138
153
  describe('#write_all_caches') do
139
- it('should write cache.css (no :extra_suffix)') do
154
+ it('should write cache.css (no :format)') do
140
155
  File.should_receive(:open).with('/c/cache.css', 'w')
141
156
  m(css_config).write_all_caches
142
157
  end
143
158
 
144
- it('should write no-extra_suffix and all extra_suffix files') do
145
- suffixes = [ 'e1', 'e2' ]
159
+ it('should write no-format and all format files') do
160
+ formats = { :e1 => [], :e2 => [] }
146
161
  File.should_receive(:open).with('/c/cache.css', 'w')
147
- suffixes.each do |suffix|
148
- File.should_receive(:open).with("/c/cache.#{suffix}.css", 'w')
162
+ formats.keys.each do |format|
163
+ File.should_receive(:open).with("/c/cache.#{format}.css", 'w')
149
164
  end
150
- m(css_config(:extra_suffixes => suffixes)).write_all_caches
165
+ m(css_config(:formats => formats)).write_all_caches
151
166
  end
152
167
  end
153
168
 
@@ -75,11 +75,11 @@ describe(AssetLibrary::Helpers) do
75
75
  h.asset_library_stylesheet_tags(:m).should == "<style type=\"text/css\">\n@import \"\/f.css?123\";\n</style>"
76
76
  end
77
77
 
78
- it('should append extra_suffix to the cache filename') do
78
+ it('should use formats to find cache filename') do
79
79
  m = mock
80
- m.should_receive(:assets).with('e').and_return([a('f.e.css')])
80
+ m.should_receive(:assets).with(:e).and_return([a('f.e.css')])
81
81
  AssetLibrary.stub!(:asset_module).and_return(m)
82
- h.asset_library_stylesheet_tags(:m, 'e').should == "<style type=\"text/css\">\n@import \"f.e.css?123\";\n</style>"
82
+ h.asset_library_stylesheet_tags(:m, :e).should == "<style type=\"text/css\">\n@import \"f.e.css?123\";\n</style>"
83
83
  end
84
84
 
85
85
  it('should output a single <script> tag with 30 @import') do
@@ -106,11 +106,11 @@ describe(AssetLibrary::Helpers) do
106
106
  h.asset_library_stylesheet_tags(:m).should == '<link rel="stylesheet" type="text/css" href="/cache.css?123" />'
107
107
  end
108
108
 
109
- it('should append extra_suffix to the cache filename') do
109
+ it('should use format for the cache filename') do
110
110
  m = mock
111
- m.should_receive(:cache_asset).with('e').and_return(a('/cache.e.css'))
111
+ m.should_receive(:cache_asset).with(:e).and_return(a('/cache.e.css'))
112
112
  AssetLibrary.stub!(:asset_module).and_return(m)
113
- h.asset_library_stylesheet_tags(:m, 'e').should == '<link rel="stylesheet" type="text/css" href="/cache.e.css?123" />'
113
+ h.asset_library_stylesheet_tags(:m, :e).should == '<link rel="stylesheet" type="text/css" href="/cache.e.css?123" />'
114
114
  end
115
115
  end
116
116
  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.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - adamh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-21 00:00:00 -07:00
12
+ date: 2009-08-18 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -32,6 +32,7 @@ files:
32
32
  - README.rdoc
33
33
  has_rdoc: true
34
34
  homepage: http://github.com/adamh/asset_library
35
+ licenses:
35
36
  post_install_message:
36
37
  rdoc_options:
37
38
  - --charset=UTF-8
@@ -52,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
53
  requirements: []
53
54
 
54
55
  rubyforge_project:
55
- rubygems_version: 1.2.0
56
+ rubygems_version: 1.3.5
56
57
  signing_key:
57
58
  specification_version: 2
58
59
  summary: Manage and bundle CSS and JavaScript files