compressible 0.0.2.3 → 0.0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/lib/compressible.rb +1 -1
- data/lib/compressible/assetable.rb +1 -4
- data/lib/compressible/configurable.rb +12 -9
- data/lib/compressible/readable.rb +18 -4
- data/lib/compressible/writable.rb +38 -32
- data/test/test_compressible.rb +13 -2
- metadata +13 -4
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.
|
8
|
+
s.version = "0.0.2.4"
|
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"
|
@@ -53,7 +53,7 @@ end
|
|
53
53
|
|
54
54
|
desc "Install the gem locally"
|
55
55
|
task :install => [:package] do
|
56
|
-
sh %{
|
56
|
+
sh %{gem install pkg/#{spec.name}-#{spec.version} --no-ri --no-rdoc}
|
57
57
|
end
|
58
58
|
|
59
59
|
desc "Generate the rdoc"
|
data/lib/compressible.rb
CHANGED
@@ -25,4 +25,4 @@ Dir["#{this}/compressible/*"].each { |c| require c }
|
|
25
25
|
Compressible.send(:include, Compressible::Configurable)
|
26
26
|
Compressible.send(:include, Compressible::Assetable)
|
27
27
|
Compressible.send(:include, Compressible::Readable)
|
28
|
-
Compressible.send(:include, Compressible::Writable)
|
28
|
+
Compressible.send(:include, Compressible::Writable)
|
@@ -6,7 +6,7 @@ module Compressible
|
|
6
6
|
end
|
7
7
|
|
8
8
|
module ClassMethods
|
9
|
-
|
9
|
+
|
10
10
|
def javascripts(hash)
|
11
11
|
hash.each do |to, paths|
|
12
12
|
paths << {:to => to}
|
@@ -27,10 +27,7 @@ module Compressible
|
|
27
27
|
paths = args.dup
|
28
28
|
options = paths.extract_options!
|
29
29
|
to = asset_name(options[:to])
|
30
|
-
raise "Please define a name for the cached javascript using ':to => :my_name'" unless to
|
31
|
-
|
32
30
|
add_to_config(:js, to, paths)
|
33
|
-
|
34
31
|
write_javascript(*args) unless config[:read_only] == true
|
35
32
|
end
|
36
33
|
alias_method :add_javascript, :javascript
|
@@ -10,17 +10,15 @@ module Compressible
|
|
10
10
|
|
11
11
|
def configure(value = nil)
|
12
12
|
raise "invalid config" unless (value.is_a?(String) || value.is_a?(Hash))
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
@config = defaults.merge(@config)
|
13
|
+
|
14
|
+
config.merge!((value.is_a?(String) ? YAML.load_file(value) : value).recursively_symbolize_keys!)
|
17
15
|
|
18
16
|
# normalize everything to an array
|
19
17
|
[:js, :css].each do |type|
|
20
|
-
|
18
|
+
config[type] = [config[type]] unless config[type].is_a?(Array)
|
21
19
|
end
|
22
|
-
|
23
|
-
|
20
|
+
|
21
|
+
self
|
24
22
|
end
|
25
23
|
|
26
24
|
def defaults
|
@@ -36,13 +34,14 @@ module Compressible
|
|
36
34
|
def config
|
37
35
|
@config ||= defaults
|
38
36
|
end
|
39
|
-
|
37
|
+
|
40
38
|
def add_to_config(type, key, value)
|
39
|
+
raise "Please define a name for the cached #{type.to_s} using ':to => :my_name'" unless key
|
41
40
|
item = find_or_create(type, key)
|
42
41
|
item[:paths] = value.collect {|i| asset_name(i)}
|
43
42
|
item
|
44
43
|
end
|
45
|
-
|
44
|
+
|
46
45
|
def find_or_create(type, key)
|
47
46
|
result = config[type].detect {|i| i[:to].to_s == key.to_s}
|
48
47
|
unless result
|
@@ -55,6 +54,10 @@ module Compressible
|
|
55
54
|
def reset
|
56
55
|
@config = defaults
|
57
56
|
end
|
57
|
+
|
58
|
+
def inspect
|
59
|
+
"<#Compressible @config=#{config.inspect}/>"
|
60
|
+
end
|
58
61
|
end
|
59
62
|
|
60
63
|
end
|
@@ -44,10 +44,6 @@ module Compressible
|
|
44
44
|
assets
|
45
45
|
end
|
46
46
|
|
47
|
-
def read(type, from)
|
48
|
-
IO.read(path_for(type, from))
|
49
|
-
end
|
50
|
-
|
51
47
|
def asset_name(path)
|
52
48
|
result = path.to_s.split(".")
|
53
49
|
if result.last =~ /(js|css)/
|
@@ -74,6 +70,24 @@ module Compressible
|
|
74
70
|
|
75
71
|
path
|
76
72
|
end
|
73
|
+
|
74
|
+
def size(type, *paths)
|
75
|
+
result = paths.collect { |path| File.size(path_for(type, path)) }.inject(0) { |sum, x| sum + x }
|
76
|
+
by = "kb"
|
77
|
+
unless result <= 0
|
78
|
+
result = case by
|
79
|
+
when "kb"
|
80
|
+
result / 1_000
|
81
|
+
when "mb"
|
82
|
+
result / 1_000_000
|
83
|
+
end
|
84
|
+
end
|
85
|
+
return "#{result.to_s}#{by}"
|
86
|
+
end
|
87
|
+
|
88
|
+
def read(type, from)
|
89
|
+
IO.read(path_for(type, from))
|
90
|
+
end
|
77
91
|
end
|
78
92
|
|
79
93
|
end
|
@@ -28,44 +28,50 @@ module Compressible
|
|
28
28
|
def write_javascript(*args)
|
29
29
|
paths = args.dup
|
30
30
|
options = paths.extract_options!
|
31
|
-
to = asset_name(options[:to])
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
compressor = YUI::JavaScriptCompressor.new(:munge => munge)
|
37
|
-
|
38
|
-
result = paths.collect do |path|
|
39
|
-
puts "Compressing #{path}..."
|
40
|
-
compressor.compress(read(:javascript, path))
|
41
|
-
end.join("\n\n")
|
42
|
-
|
43
|
-
write(:javascript, to, result) if to
|
44
|
-
|
45
|
-
result
|
31
|
+
options[:to] = asset_name(options[:to])
|
32
|
+
options[:munge] = options.has_key?(:munge) ? options[:munge] : true
|
33
|
+
paths << options
|
34
|
+
process(:javascript, *paths)
|
46
35
|
end
|
47
36
|
|
48
37
|
def write_stylesheet(*args)
|
49
38
|
paths = args.dup
|
50
39
|
options = paths.extract_options!
|
51
|
-
to = asset_name(options[:to])
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
require 'yui/compressor' unless defined?(YUI)
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
result
|
62
|
-
|
63
|
-
|
40
|
+
options[:to] = asset_name(options[:to])
|
41
|
+
paths << options
|
42
|
+
process(:stylesheet, *paths)
|
43
|
+
end
|
44
|
+
|
45
|
+
def process(type, *paths)
|
46
|
+
require 'yui/compressor' unless defined?(::YUI)
|
47
|
+
options = paths.extract_options!
|
48
|
+
to = options[:to]
|
49
|
+
|
50
|
+
raise 'must define result file name via :to => name' unless to
|
51
|
+
|
52
|
+
compressor = compressor_for(type, options)
|
53
|
+
|
54
|
+
start_size = size(type, *paths)
|
55
|
+
|
56
|
+
compressed = paths.collect do |path|
|
57
|
+
puts "Compressing '#{path}'... (#{size(type, path)})"
|
58
|
+
compressor.compress(read(type, path))
|
64
59
|
end.join("\n\n")
|
65
|
-
|
66
|
-
write(
|
67
|
-
|
68
|
-
|
60
|
+
|
61
|
+
write(type, to, compressed)
|
62
|
+
|
63
|
+
end_size = size(type, to)
|
64
|
+
|
65
|
+
puts "Compressed to '#{to.to_s}' (#{end_size} from #{start_size})"
|
66
|
+
|
67
|
+
compressed
|
68
|
+
end
|
69
|
+
|
70
|
+
def compressor_for(type, options = {})
|
71
|
+
{
|
72
|
+
:javascript => YUI::JavaScriptCompressor,
|
73
|
+
:stylesheet => YUI::CssCompressor
|
74
|
+
}[type].new(options.reject {|k,v| k.to_s !~ /(munge|charset|linebreak|optimize|preserve_semicolons)/})
|
69
75
|
end
|
70
76
|
|
71
77
|
def write(type, to, result)
|
data/test/test_compressible.rb
CHANGED
@@ -6,8 +6,8 @@ class CompressibleTest < Test::Unit::TestCase
|
|
6
6
|
|
7
7
|
context "configuration" do
|
8
8
|
|
9
|
-
should "load configuration file and result should be a
|
10
|
-
assert_kind_of
|
9
|
+
should "load configuration file and result should be a Module" do
|
10
|
+
assert_kind_of Module, Compressible.configure("test/config.yml")
|
11
11
|
end
|
12
12
|
|
13
13
|
should "raise an RuntimeError if pass junk to config" do
|
@@ -115,6 +115,17 @@ class CompressibleTest < Test::Unit::TestCase
|
|
115
115
|
assert_equal ["test/test-a", "test/test-b"], Compressible.config[:js][0][:paths]
|
116
116
|
end
|
117
117
|
|
118
|
+
should "be able to add config dynamically" do
|
119
|
+
assert_equal Compressible.defaults, Compressible.config
|
120
|
+
Compressible.configure(:stylesheet_path => "public/stylesheets", :read_only => true)
|
121
|
+
assert_equal "public/stylesheets", Compressible.config[:stylesheet_path]
|
122
|
+
Compressible.stylesheets("test/result" => ["test/test-a", "test/test-b"])
|
123
|
+
assets = Compressible.assets_for(:stylesheet, 'test/result', :environments => "production", :current => "development")
|
124
|
+
assert_equal ["test/test-a", "test/test-b"], assets
|
125
|
+
result = {:js=>[], :javascript_path=>nil, :stylesheet_path=>"public/stylesheets", :read_only=>true, :css=>[{:paths=>["test/test-a", "test/test-b"], :to=>"test/result"}]}
|
126
|
+
assert_equal result, Compressible.config
|
127
|
+
end
|
128
|
+
|
118
129
|
teardown { Compressible.reset }
|
119
130
|
|
120
131
|
end
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: compressible
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 79
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 0
|
8
9
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.0.2.
|
10
|
+
- 4
|
11
|
+
version: 0.0.2.4
|
11
12
|
platform: ruby
|
12
13
|
authors:
|
13
14
|
- Lance Pollard
|
@@ -15,16 +16,18 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2010-06-
|
19
|
+
date: 2010-06-07 00:00:00 -07:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
22
23
|
name: activesupport
|
23
24
|
prerelease: false
|
24
25
|
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
25
27
|
requirements:
|
26
28
|
- - ">="
|
27
29
|
- !ruby/object:Gem::Version
|
30
|
+
hash: 15
|
28
31
|
segments:
|
29
32
|
- 2
|
30
33
|
- 1
|
@@ -36,9 +39,11 @@ dependencies:
|
|
36
39
|
name: yui-compressor
|
37
40
|
prerelease: false
|
38
41
|
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
39
43
|
requirements:
|
40
44
|
- - ">="
|
41
45
|
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
42
47
|
segments:
|
43
48
|
- 0
|
44
49
|
version: "0"
|
@@ -87,23 +92,27 @@ rdoc_options: []
|
|
87
92
|
require_paths:
|
88
93
|
- lib
|
89
94
|
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
90
96
|
requirements:
|
91
97
|
- - ">="
|
92
98
|
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
93
100
|
segments:
|
94
101
|
- 0
|
95
102
|
version: "0"
|
96
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
97
105
|
requirements:
|
98
106
|
- - ">="
|
99
107
|
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
100
109
|
segments:
|
101
110
|
- 0
|
102
111
|
version: "0"
|
103
112
|
requirements: []
|
104
113
|
|
105
114
|
rubyforge_project: compressible
|
106
|
-
rubygems_version: 1.3.
|
115
|
+
rubygems_version: 1.3.7
|
107
116
|
signing_key:
|
108
117
|
specification_version: 3
|
109
118
|
summary: "Compressible: Quick asset compression for Ruby - Perfect for Heroku"
|