crush 0.2.0 → 0.3.0

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.
@@ -0,0 +1,3 @@
1
+ require "crush"
2
+
3
+ Crush.register_js
@@ -1,8 +1,12 @@
1
+ require "crush/engine"
2
+
1
3
  module Crush
4
+ # Engine implementation of Douglas Crockford's
5
+ # JSMin JavaScript minifier. See:
6
+ #
7
+ # https://rubygems.org/gems/jsmin
2
8
  class JSMin < Engine
3
- def self.engine_name
4
- "jsmin"
5
- end
9
+ self.default_mime_type = "application/javascript"
6
10
 
7
11
  def self.engine_initialized?
8
12
  !!(defined? ::JSMin)
@@ -12,8 +16,12 @@ module Crush
12
16
  require_template_library "jsmin"
13
17
  end
14
18
 
15
- def evaluate
16
- ::JSMin.minify(data)
19
+ def prepare
20
+ @output = nil
21
+ end
22
+
23
+ def evaluate(scope, locals, &block)
24
+ @output ||= ::JSMin.minify(data)
17
25
  end
18
26
  end
19
27
  end
@@ -1,5 +1,13 @@
1
+ require "crush/engine"
2
+
1
3
  module Crush
4
+ # Engine implementation of Dean Edwards'
5
+ # JavaScript compressor, Packr. See:
6
+ #
7
+ # https://rubygems.org/gems/packr
2
8
  class Packr < Engine
9
+ self.default_mime_type = "application/javascript"
10
+
3
11
  def self.engine_initialized?
4
12
  !!(defined? ::Packr)
5
13
  end
@@ -8,8 +16,12 @@ module Crush
8
16
  require_template_library "packr"
9
17
  end
10
18
 
11
- def evaluate
12
- ::Packr.pack(data, options)
19
+ def prepare
20
+ @output = nil
21
+ end
22
+
23
+ def evaluate(scope, locals, &block)
24
+ @output ||= ::Packr.pack(data, options)
13
25
  end
14
26
  end
15
27
  end
@@ -1,5 +1,13 @@
1
+ require "crush/engine"
2
+
1
3
  module Crush
4
+ # Engine implementation of the CSS compressor,
5
+ # Rainpress. See:
6
+ #
7
+ # https://rubygems.org/gems/rainpress
2
8
  class Rainpress < Engine
9
+ self.default_mime_type = "text/css"
10
+
3
11
  def self.engine_initialized?
4
12
  !!(defined? ::Rainpress)
5
13
  end
@@ -8,8 +16,12 @@ module Crush
8
16
  require_template_library "rainpress"
9
17
  end
10
18
 
11
- def evaluate
12
- ::Rainpress.compress(data, options)
19
+ def prepare
20
+ @output = nil
21
+ end
22
+
23
+ def evaluate(scope, locals, &block)
24
+ @output ||= ::Rainpress.compress(data, options)
13
25
  end
14
26
  end
15
27
  end
@@ -1,5 +1,13 @@
1
+ require "crush/engine"
2
+
1
3
  module Crush
4
+ # Engine implementation of the UglifyJS
5
+ # JavaScript compressor. See:
6
+ #
7
+ # https://rubygems.org/gems/rainpress
2
8
  class Uglifier < Engine
9
+ self.default_mime_type = "application/javascript"
10
+
3
11
  def self.engine_initialized?
4
12
  !!(defined? ::Uglifier)
5
13
  end
@@ -10,10 +18,11 @@ module Crush
10
18
 
11
19
  def prepare
12
20
  @engine = ::Uglifier.new(options)
21
+ @output = nil
13
22
  end
14
23
 
15
- def evaluate
16
- @engine.compile(data)
24
+ def evaluate(scope, locals, &block)
25
+ @output ||= @engine.compile(data)
17
26
  end
18
27
  end
19
28
  end
@@ -1,3 +1,3 @@
1
1
  module Crush
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,12 +1,16 @@
1
+ require "crush/engine"
2
+
1
3
  module Crush
2
4
  module YUI
5
+ # Engine implementation the YUI JavaScript
6
+ # compressor. See:
7
+ #
8
+ # https://rubygems.org/gems/yui-compressor
3
9
  class JavaScriptCompressor < Crush::Engine
4
- def self.engine_name
5
- "yui_js"
6
- end
10
+ self.default_mime_type = "application/javascript"
7
11
 
8
12
  def self.engine_initialized?
9
- !!(defined? ::YUI) && !!(defined? ::YUI::JavaScriptCompressor)
13
+ !!(defined? ::YUI && defined? ::YUI::JavaScriptCompressor)
10
14
  end
11
15
 
12
16
  def initialize_engine
@@ -15,20 +19,23 @@ module Crush
15
19
 
16
20
  def prepare
17
21
  @engine = ::YUI::JavaScriptCompressor.new(options)
22
+ @output = nil
18
23
  end
19
24
 
20
- def evaluate
21
- @engine.compress(data)
25
+ def evaluate(scope, locals, &block)
26
+ @output ||= @engine.compress(data)
22
27
  end
23
28
  end
24
29
 
30
+ # Engine implementation the YUI CSS
31
+ # compressor. See:
32
+ #
33
+ # https://rubygems.org/gems/yui-compressor
25
34
  class CssCompressor < Crush::Engine
26
- def self.engine_name
27
- "yui_css"
28
- end
35
+ self.default_mime_type = "text/css"
29
36
 
30
37
  def self.engine_initialized?
31
- !!(defined? ::YUI) && !!(defined? ::YUI::CssCompressor)
38
+ !!(defined? ::YUI && defined? ::YUI::CssCompressor)
32
39
  end
33
40
 
34
41
  def initialize_engine
@@ -37,10 +44,11 @@ module Crush
37
44
 
38
45
  def prepare
39
46
  @engine = ::YUI::CssCompressor.new(options)
47
+ @output = nil
40
48
  end
41
49
 
42
- def evaluate
43
- @engine.compress(data)
50
+ def evaluate(scope, locals, &block)
51
+ @output ||= @engine.compress(data)
44
52
  end
45
53
  end
46
54
  end
@@ -1,18 +1,13 @@
1
1
  require "spec_helper"
2
- require "closure-compiler"
3
2
 
4
3
  describe Crush::Closure::Compiler do
5
- specify { Crush::Closure::Compiler.engine_name.should == "closure" }
4
+ specify { Crush::Closure::Compiler.default_mime_type.should == "application/javascript" }
6
5
 
7
- it "is registered for '.js' files" do
8
- Crush.mappings["js"].should include(Crush::Closure::Compiler)
9
- end
10
-
11
- it "minifies using Closure::Compiler" do
6
+ it "compresses using Closure::Compiler" do
12
7
  compressor = mock(:compressor)
13
8
  ::Closure::Compiler.should_receive(:new).with({}).and_return(compressor)
14
9
  compressor.should_receive(:compile).with("hello").and_return("world")
15
- Crush::Closure::Compiler.new.compress("hello").should == "world"
10
+ Crush::Closure::Compiler.compress("hello").should == "world"
16
11
  end
17
12
 
18
13
  it "sends options to Closure::Compiler" do
@@ -21,4 +16,12 @@ describe Crush::Closure::Compiler do
21
16
  compressor.should_receive(:compile).with("hello").and_return("world")
22
17
  Crush::Closure::Compiler.new(:foo => "bar").compress("hello")
23
18
  end
19
+
20
+ it "is registered with Tilt" do
21
+ compressor = mock(:compressor)
22
+ ::Closure::Compiler.should_receive(:new).with({}).and_return(compressor)
23
+ compressor.should_receive(:compile).with("hello").and_return("world")
24
+ Tilt.register Crush::Closure::Compiler, "js"
25
+ Tilt.new("application.js").compress("hello").should == "world"
26
+ end
24
27
  end
@@ -1,15 +1,16 @@
1
1
  require "spec_helper"
2
- require "cssmin"
3
2
 
4
3
  describe Crush::CSSMin do
5
- specify { Crush::CSSMin.engine_name.should == "cssmin" }
4
+ specify { Crush::CSSMin.default_mime_type.should == "text/css" }
6
5
 
7
- it "is registered for '.js' files" do
8
- Crush.mappings["css"].should include(Crush::CSSMin)
6
+ it "minifies using CSSMin" do
7
+ ::CSSMin.should_receive(:minify).with("hello").and_return("world")
8
+ Crush::CSSMin.compress("hello").should == "world"
9
9
  end
10
10
 
11
- it "minifies using CSSMin" do
11
+ it "is registered with Tilt" do
12
12
  ::CSSMin.should_receive(:minify).with("hello").and_return("world")
13
- Crush::CSSMin.new.compress("hello").should == "world"
13
+ Tilt.register Crush::CSSMin, "css"
14
+ Tilt.new("application.css").compress("hello").should == "world"
14
15
  end
15
16
  end
@@ -1,110 +1,66 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Crush::Engine do
4
- class MockEngine < Crush::Engine
5
- end
6
-
7
- describe ".engine_name" do
8
- it "returns an undescored version of the class name" do
9
- MockEngine.engine_name.should == "mock_engine"
4
+ class Stripper < Crush::Engine
5
+ def evaluate(*args)
6
+ case options[:direction]
7
+ when :left
8
+ data.lstrip
9
+ when :right
10
+ data.rstrip
11
+ else
12
+ data.strip
13
+ end
10
14
  end
11
15
  end
12
16
 
13
- describe "#file" do
14
- it "returns the file the engine was initialized with" do
15
- engine = MockEngine.new("application.js") {}
16
- engine.file.should == "application.js"
17
- end
18
- end
19
-
20
- describe "#options" do
21
- it "returns the options passed to the engine" do
22
- engine = MockEngine.new(nil, :foo => "bar") {}
23
- engine.options[:foo].should == "bar"
24
- engine = MockEngine.new(:bar => "foo") {}
25
- engine.options[:bar].should == "foo"
26
- end
27
- end
28
-
29
- describe "#data" do
30
- it "returns the data from the file" do
31
- File.stub(:binread => "Hello", :read => "Hello")
32
- engine = MockEngine.new("application.js")
33
- engine.data.should == "Hello"
17
+ describe ".compress" do
18
+ it "compresses the given data" do
19
+ Stripper.compress(" data ").should == "data"
34
20
  end
35
21
 
36
- it "returns the data from the given block" do
37
- engine = MockEngine.new("application.js") { "Hello" }
38
- engine.data.should == "Hello"
39
- end
40
- end
41
-
42
- class InitializingMockEngine < Crush::Engine
43
- class << self
44
- attr_accessor :initialized_count
22
+ it "accepts options" do
23
+ Stripper.compress(" data ", :direction => :left).should == "data "
24
+ Stripper.compress(" data ", :direction => :right).should == " data"
45
25
  end
46
-
47
- def initialize_engine
48
- self.class.initialized_count += 1
26
+
27
+ it "is aliased as `compile`" do
28
+ Stripper.compile(" data ").should == "data"
49
29
  end
50
30
  end
51
31
 
52
- describe "#initialize_engine" do
53
- it "is called one time to require the library" do
54
- InitializingMockEngine.initialized_count = 0
55
- InitializingMockEngine.new
56
- InitializingMockEngine.initialized_count.should == 1
57
- InitializingMockEngine.new
58
- InitializingMockEngine.initialized_count.should == 1
32
+ describe "#initialize" do
33
+ it "does not raise errors without a file or block" do
34
+ expect {
35
+ Stripper.new
36
+ }.to_not raise_error(ArgumentError)
59
37
  end
60
38
  end
61
39
 
62
- class InitializedMockEngine < Crush::Engine
63
- end
64
-
65
- describe ".engine_initialized?" do
66
- it "returns false before the engine has been initialized" do
67
- InitializedMockEngine.engine_initialized?.should be_false
40
+ describe "#compress" do
41
+ it "compresses the given data" do
42
+ Stripper.new.compress(" data ").should == "data"
68
43
  end
69
44
 
70
- it "returns true once the engine has been initialized" do
71
- InitializedMockEngine.new
72
- InitializedMockEngine.engine_initialized?.should be_true
73
- end
74
- end
75
-
76
- class PreparingMockEngine < Crush::Engine
77
- class << self
78
- attr_accessor :prepared_count
45
+ it "uses options set when initializing" do
46
+ Stripper.new(:direction => :left).compress(" data ").should == "data "
47
+ Stripper.new(:direction => :right).compress(" data ").should == " data"
79
48
  end
80
49
 
81
- def prepare
82
- self.class.prepared_count += 1
50
+ it "is aliased as `compile`" do
51
+ Stripper.new.compile(" data ").should == "data"
83
52
  end
84
- end
85
-
86
- describe "#prepare" do
87
- it "is called each time an engine is created" do
88
- PreparingMockEngine.prepared_count = 0
89
- PreparingMockEngine.new
90
- PreparingMockEngine.prepared_count.should == 1
91
- PreparingMockEngine.new
92
- PreparingMockEngine.prepared_count.should == 2
93
- end
94
- end
95
-
96
- class SimpleMockEngine < Crush::Engine
97
- def evaluate
98
- @data.strip
53
+
54
+ it "can be used without an argument" do
55
+ Stripper.new { " data " }.compress.should == "data"
99
56
  end
100
57
  end
101
58
 
102
- describe "#compress" do
103
- it "compresses the data using the engine" do
104
- File.stub(:binread => " hello ", :read => " hello ")
105
- SimpleMockEngine.new("file.txt").compile.should == "hello"
106
- SimpleMockEngine.new { " hello " }.render.should == "hello"
107
- SimpleMockEngine.new.compress(" hello ").should == "hello"
59
+ describe "#render" do
60
+ it "throws an error if used without data" do
61
+ expect {
62
+ Stripper.new.render
63
+ }.to raise_error(ArgumentError)
108
64
  end
109
65
  end
110
66
  end
@@ -1,15 +1,16 @@
1
1
  require "spec_helper"
2
- require "jsmin"
3
2
 
4
3
  describe Crush::JSMin do
5
- specify { Crush::JSMin.engine_name.should == "jsmin" }
4
+ specify { Crush::JSMin.default_mime_type.should == "application/javascript" }
6
5
 
7
- it "is registered for '.js' files" do
8
- Crush.mappings["js"].should include(Crush::JSMin)
6
+ it "minifies using JSMin" do
7
+ ::JSMin.should_receive(:minify).with("hello").and_return("world")
8
+ Crush::JSMin.compress("hello").should == "world"
9
9
  end
10
10
 
11
- it "minifies using JSMin" do
11
+ it "is registered with Tilt" do
12
12
  ::JSMin.should_receive(:minify).with("hello").and_return("world")
13
- Crush::JSMin.new.compress("hello").should == "world"
13
+ Tilt.register Crush::JSMin, "js"
14
+ Tilt.new("application.js").compress("hello").should == "world"
14
15
  end
15
16
  end
@@ -1,20 +1,16 @@
1
1
  require "spec_helper"
2
- require "packr"
3
2
 
4
3
  describe Crush::Packr do
5
- specify { Crush::Packr.engine_name.should == "packr" }
4
+ specify { Crush::Packr.default_mime_type.should == "application/javascript" }
6
5
 
7
- it "is registered for '.js' files" do
8
- Crush.mappings["js"].should include(Crush::Packr)
9
- end
10
-
11
- it "minifies using Packr" do
6
+ it "compresses using Packr" do
12
7
  ::Packr.should_receive(:pack).with("hello", {}).and_return("world")
13
- Crush::Packr.new.compress("hello").should == "world"
8
+ Crush::Packr.compress("hello").should == "world"
14
9
  end
15
10
 
16
- it "sends options to Packr" do
17
- ::Packr.should_receive(:pack).with("hello", :foo => "bar")
18
- Crush::Packr.new(:foo => "bar").compress("hello")
11
+ it "is registered with Tilt" do
12
+ ::Packr.should_receive(:pack).with("hello", {}).and_return("world")
13
+ Tilt.register Crush::Packr, "js"
14
+ Tilt.new("application.js").compress("hello").should == "world"
19
15
  end
20
16
  end