compressible 0.0.2.1 → 0.0.2.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/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
5
5
  # http://docs.rubygems.org/read/chapter/20
6
6
  spec = Gem::Specification.new do |s|
7
7
  s.name = "compressible"
8
- s.version = "0.0.2.1"
8
+ s.version = "0.0.2.2"
9
9
  s.author = "Lance Pollard"
10
10
  s.summary = "Compressible: Quick asset compression for Ruby - Perfect for Heroku"
11
11
  s.homepage = "http://github.com/viatropos/compressible"
data/lib/compressible.rb CHANGED
@@ -6,11 +6,10 @@ begin
6
6
  rescue Gem::LoadError => e
7
7
  puts e.inspect
8
8
  end
9
- require 'yui/compressor'
10
9
  this = File.dirname(__FILE__)
11
10
  require File.join(this, "ext.rb")
12
11
 
13
- class Compressible
12
+ module Compressible
14
13
 
15
14
  KEYS = {
16
15
  :css => :stylesheet,
@@ -19,207 +18,10 @@ class Compressible
19
18
  :javascript => :js
20
19
  }
21
20
 
22
- class << self
23
-
24
- attr_reader :config
25
-
26
- def configure(value = nil)
27
- raise "invalid config" unless (value.is_a?(String) || value.is_a?(Hash))
28
- @config = value.is_a?(String) ? YAML.load_file(value) : value
29
- @config.recursively_symbolize_keys!
30
-
31
- @config = defaults.merge(@config)
32
-
33
- # normalize everything to an array
34
- [:js, :css].each do |type|
35
- @config[type] = [@config[type]] unless @config[type].is_a?(Array)
36
- end
37
-
38
- @config
39
- end
40
-
41
- def defaults
42
- {
43
- :js => [],
44
- :css => [],
45
- :stylesheet_path => defined?(Rails) ? "#{Rails.root}/public/stylesheets" : nil,
46
- :javascript_path => defined?(Rails) ? "#{Rails.root}/public/javascripts" : nil
47
- }
48
- end
49
-
50
- def config
51
- @config ||= defaults
52
- end
53
-
54
- def add_to_config(type, key, value)
55
- item = find_or_create(type, key)
56
- item[:paths] = value.collect {|i| asset_name(i)}
57
- item
58
- end
59
-
60
- def find_or_create(type, key)
61
- result = config[type].detect {|i| i[:to].to_s == key.to_s}
62
- unless result
63
- result = {:to => key.to_s}
64
- config[type] << result
65
- end
66
- result
67
- end
68
-
69
- def reset
70
- @config = defaults
71
- end
72
-
73
- def uncached_stylesheet_paths(*keys)
74
- uncached_paths_for(:css, *keys)
75
- end
76
-
77
- def uncached_javascript_paths(*keys)
78
- uncached_paths_for(:js, *keys)
79
- end
80
-
81
- def uncached_paths_for(type, *keys)
82
- returning [] do |result|
83
- config[type].each do |item|
84
- keys.each do |key|
85
- result.concat(item[:paths]) if item[:to] == key.to_s
86
- end
87
- end
88
- end
89
- end
90
-
91
- # called if you gave it a config
92
- def compress(value = nil)
93
- configure(value) if value
94
- raise "set config to yaml file or run 'Compressible.js' or 'Compressible.css' manually" unless @config
95
-
96
- [:js, :css].each do |k|
97
- config[k].each do |item|
98
- compress_from_hash(k, item)
99
- end
100
- end
101
- end
102
-
103
- def compress_from_hash(k, v)
104
- args = v.dup.delete(:paths) + [v]
105
- self.send(k, *args)
106
- end
107
-
108
- def javascripts(hash)
109
- hash.each do |to, paths|
110
- paths << {:to => to}
111
- javascript(*paths)
112
- end
113
- end
114
- alias_method :add_javascripts, :javascripts
115
-
116
- def stylesheets(hash)
117
- hash.each do |to, paths|
118
- paths << {:to => to}
119
- stylesheet(*paths)
120
- end
121
- end
122
- alias_method :add_stylesheets, :stylesheets
123
-
124
- def javascript(*paths)
125
- options = paths.extract_options!
126
- to = asset_name(options[:to])
127
- raise "Please define a name for the cached javascript using ':to => :my_name'" unless to
128
- munge = options.has_key?(:munge) ? options[:munge] : true
129
-
130
- add_to_config(:js, to, paths)
131
-
132
- compressor = YUI::JavaScriptCompressor.new(:munge => munge)
133
-
134
- result = paths.collect do |path|
135
- puts "Compressing #{path}..."
136
- compressor.compress(read(:javascript, path))
137
- end.join("\n\n")
138
-
139
- write(:javascript, to, result) if to
140
-
141
- result
142
- end
143
- alias_method :add_javascript, :javascript
144
- alias_method :js, :javascript
145
-
146
- def stylesheet(*paths)
147
- options = paths.extract_options!
148
- to = asset_name(options[:to])
149
-
150
- add_to_config(:css, to, paths)
151
-
152
- compressor = YUI::CssCompressor.new
153
-
154
- result = paths.collect do |path|
155
- puts "Compressing #{path}..."
156
- compressor.compress(read(:stylesheet, path))
157
- end.join("\n\n")
158
-
159
- write(:stylesheet, to, result) if to
160
-
161
- result
162
- end
163
- alias_method :add_stylesheet, :stylesheet
164
- alias_method :css, :stylesheet
165
-
166
- def stylesheets_for(*keys)
167
- assets_for(:stylesheet, *keys)
168
- end
169
-
170
- def javascripts_for(*keys)
171
- assets_for(:javascript, *keys)
172
- end
173
-
174
- def assets_for(type, *keys)
175
- options = keys.extract_options!
176
- environment = defined?(Rails) ? Rails.env.to_s : (options[:current] || "production")
177
- environment = environment.to_s
178
- cache_environments = options[:environments] || "production"
179
- cache_environments = [cache_environments] unless cache_environments.is_a?(Array)
180
- cache_environments = cache_environments.collect(&:to_s)
181
-
182
- assets = cache_environments.include?(environment) ? keys : send("uncached_#{type.to_s}_paths", *keys)
183
- assets
184
- end
185
-
186
- def read(type, from)
187
- IO.read(path_for(type, from))
188
- end
189
-
190
- def write(type, to, result)
191
- File.open(path_for(type, to), "w") {|f| f.puts result}
192
- end
193
-
194
- def asset_name(path)
195
- result = path.to_s.split(".")
196
- if result.last =~ /(js|css)/
197
- result = result[0..-2].join(".")
198
- else
199
- result = result.join(".")
200
- end
201
- result
202
- end
203
-
204
- # ultimately should return global path
205
- def path_for(type, file)
206
- key = "#{type.to_s}_path".to_sym
207
-
208
- if config && config[key]
209
- path = File.join(config[key], file.to_s)
210
- elsif defined?(Rails)
211
- path = File.join(Rails.root.to_s, "public/#{type.to_s.pluralize}", file.to_s)
212
- else
213
- path = file.to_s
214
- end
215
-
216
- path << ".#{KEYS[type].to_s}" unless path.split(".").last == KEYS[type].to_s
217
-
218
- path
219
- end
220
-
221
- end
222
-
223
21
  end
224
22
 
225
23
  Dir["#{this}/compressible/*"].each { |c| require c }
24
+
25
+ Compressible.send(:include, Compressible::Configurable)
26
+ Compressible.send(:include, Compressible::Condensible)
27
+ Compressible.send(:include, Compressible::Accessible)
@@ -0,0 +1,84 @@
1
+ module Compressible
2
+ module Accessible
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def uncached_stylesheet_paths(*keys)
10
+ uncached_paths_for(:css, *keys)
11
+ end
12
+
13
+ def uncached_javascript_paths(*keys)
14
+ uncached_paths_for(:js, *keys)
15
+ end
16
+
17
+ def uncached_paths_for(type, *keys)
18
+ returning [] do |result|
19
+ config[type].each do |item|
20
+ keys.each do |key|
21
+ result.concat(item[:paths]) if item[:to] == key.to_s
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def stylesheets_for(*keys)
28
+ assets_for(:stylesheet, *keys)
29
+ end
30
+
31
+ def javascripts_for(*keys)
32
+ assets_for(:javascript, *keys)
33
+ end
34
+
35
+ def assets_for(type, *keys)
36
+ options = keys.extract_options!
37
+ environment = defined?(Rails) ? Rails.env.to_s : (options[:current] || "production")
38
+ environment = environment.to_s
39
+ cache_environments = options[:environments] || "production"
40
+ cache_environments = [cache_environments] unless cache_environments.is_a?(Array)
41
+ cache_environments = cache_environments.collect(&:to_s)
42
+
43
+ assets = cache_environments.include?(environment) ? keys : send("uncached_#{type.to_s}_paths", *keys)
44
+ assets
45
+ end
46
+
47
+ def read(type, from)
48
+ IO.read(path_for(type, from))
49
+ end
50
+
51
+ def write(type, to, result)
52
+ File.open(path_for(type, to), "w") {|f| f.puts result}
53
+ end
54
+
55
+ def asset_name(path)
56
+ result = path.to_s.split(".")
57
+ if result.last =~ /(js|css)/
58
+ result = result[0..-2].join(".")
59
+ else
60
+ result = result.join(".")
61
+ end
62
+ result
63
+ end
64
+
65
+ # ultimately should return global path
66
+ def path_for(type, file)
67
+ key = "#{type.to_s}_path".to_sym
68
+
69
+ if config && config[key]
70
+ path = File.join(config[key], file.to_s)
71
+ elsif defined?(Rails)
72
+ path = File.join(Rails.root.to_s, "public/#{type.to_s.pluralize}", file.to_s)
73
+ else
74
+ path = file.to_s
75
+ end
76
+
77
+ path << ".#{KEYS[type].to_s}" unless path.split(".").last == KEYS[type].to_s
78
+
79
+ path
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,90 @@
1
+ module Compressible
2
+ module Condensible
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ # called if you gave it a config
10
+ def compress(value = nil)
11
+ configure(value) if value
12
+ raise "set config to yaml file or run 'Compressible.js' or 'Compressible.css' manually" unless @config
13
+
14
+ [:js, :css].each do |k|
15
+ config[k].each do |item|
16
+ compress_from_hash(k, item)
17
+ end
18
+ end
19
+ end
20
+
21
+ def compress_from_hash(k, v)
22
+ args = v.dup.delete(:paths) + [v]
23
+ self.send(k, *args)
24
+ end
25
+
26
+ def javascripts(hash)
27
+ hash.each do |to, paths|
28
+ paths << {:to => to}
29
+ javascript(*paths)
30
+ end
31
+ end
32
+ alias_method :add_javascripts, :javascripts
33
+
34
+ def stylesheets(hash)
35
+ hash.each do |to, paths|
36
+ paths << {:to => to}
37
+ stylesheet(*paths)
38
+ end
39
+ end
40
+ alias_method :add_stylesheets, :stylesheets
41
+
42
+ def javascript(*paths)
43
+ options = paths.extract_options!
44
+ to = asset_name(options[:to])
45
+ raise "Please define a name for the cached javascript using ':to => :my_name'" unless to
46
+ munge = options.has_key?(:munge) ? options[:munge] : true
47
+
48
+ add_to_config(:js, to, paths)
49
+
50
+ require 'yui/compressor' unless defined?(YUI)
51
+
52
+ compressor = YUI::JavaScriptCompressor.new(:munge => munge)
53
+
54
+ result = paths.collect do |path|
55
+ puts "Compressing #{path}..."
56
+ compressor.compress(read(:javascript, path))
57
+ end.join("\n\n")
58
+
59
+ write(:javascript, to, result) if to
60
+
61
+ result
62
+ end
63
+ alias_method :add_javascript, :javascript
64
+ alias_method :js, :javascript
65
+
66
+ def stylesheet(*paths)
67
+ options = paths.extract_options!
68
+ to = asset_name(options[:to])
69
+
70
+ add_to_config(:css, to, paths)
71
+
72
+ require 'yui/compressor' unless defined?(YUI)
73
+
74
+ compressor = YUI::CssCompressor.new
75
+
76
+ result = paths.collect do |path|
77
+ puts "Compressing #{path}..."
78
+ compressor.compress(read(:stylesheet, path))
79
+ end.join("\n\n")
80
+
81
+ write(:stylesheet, to, result) if to
82
+
83
+ result
84
+ end
85
+ alias_method :add_stylesheet, :stylesheet
86
+ alias_method :css, :stylesheet
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,60 @@
1
+ module Compressible
2
+ module Configurable
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ attr_reader :config
10
+
11
+ def configure(value = nil)
12
+ raise "invalid config" unless (value.is_a?(String) || value.is_a?(Hash))
13
+ @config = value.is_a?(String) ? YAML.load_file(value) : value
14
+ @config.recursively_symbolize_keys!
15
+
16
+ @config = defaults.merge(@config)
17
+
18
+ # normalize everything to an array
19
+ [:js, :css].each do |type|
20
+ @config[type] = [@config[type]] unless @config[type].is_a?(Array)
21
+ end
22
+
23
+ @config
24
+ end
25
+
26
+ def defaults
27
+ {
28
+ :js => [],
29
+ :css => [],
30
+ :stylesheet_path => defined?(Rails) ? "#{Rails.root}/public/stylesheets" : nil,
31
+ :javascript_path => defined?(Rails) ? "#{Rails.root}/public/javascripts" : nil
32
+ }
33
+ end
34
+
35
+ def config
36
+ @config ||= defaults
37
+ end
38
+
39
+ def add_to_config(type, key, value)
40
+ item = find_or_create(type, key)
41
+ item[:paths] = value.collect {|i| asset_name(i)}
42
+ item
43
+ end
44
+
45
+ def find_or_create(type, key)
46
+ result = config[type].detect {|i| i[:to].to_s == key.to_s}
47
+ unless result
48
+ result = {:to => key.to_s}
49
+ config[type] << result
50
+ end
51
+ result
52
+ end
53
+
54
+ def reset
55
+ @config = defaults
56
+ end
57
+ end
58
+
59
+ end
60
+ end
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.0.2.1
9
+ - 2
10
+ version: 0.0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lance Pollard
@@ -57,6 +57,9 @@ files:
57
57
  - Rakefile
58
58
  - init.rb
59
59
  - MIT-LICENSE
60
+ - lib/compressible/accessible.rb
61
+ - lib/compressible/condensible.rb
62
+ - lib/compressible/configurable.rb
60
63
  - lib/compressible/view_helpers.rb
61
64
  - lib/compressible.rake
62
65
  - lib/compressible.rb