Class: Crush::Engine

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/crush/engine.rb

Overview

Crush::Engine is an abstract class like Tilt::Template, which adds methods common to compression library APIs.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, *args, &block) ⇒ Engine

Override Tilt::Template#initialize so that it does not require a file or block. This is done so that Crush::Engines can follow the usual compressor API convention of having an instance method, #compress, which accepts the data to compress as an argument.

(see Tilt::Template#initialize)



30
31
32
33
34
35
# File 'lib/crush/engine.rb', line 30

def initialize(file = nil, *args, &block)
  unless block_given? or args[0].respond_to?(:to_str)
    block = Proc.new {}
  end
  super file, *args, &block
end

Class Method Details

.compileString

Convenience method of initializing an engine and immediately compressing the given data.

Parameters:

  • data (String)

    The data to compress.

  • options (Hash)

    Options to pass to the underlying compressor.

Returns:

  • (String)

    the compressed data.



19
20
21
# File 'lib/crush/engine.rb', line 19

def compress(data, options = {})
  self.new(options).compress(data)
end

.compress(data, options = {}) ⇒ String

Convenience method of initializing an engine and immediately compressing the given data.

Parameters:

  • data (String)

    The data to compress.

  • options (Hash) (defaults to: {})

    Options to pass to the underlying compressor.

Returns:

  • (String)

    the compressed data.



16
17
18
# File 'lib/crush/engine.rb', line 16

def compress(data, options = {})
  self.new(options).compress(data)
end

Instance Method Details

#compress(data = nil) ⇒ String Also known as: compile

Compresses the given data.

Parameters:

  • data (String) (defaults to: nil)

    The data to compress.

Returns:

  • (String)

    the compressed data.



41
42
43
44
# File 'lib/crush/engine.rb', line 41

def compress(data = nil)
  @data = data.to_s unless data.nil?
  render
end

#render(*args) ⇒ Object

Override Tilt::Template#render to check for data and raise an error if there isn’t any.

(see Tilt::Template#render)

Raises:

  • (ArgumentError)


51
52
53
54
# File 'lib/crush/engine.rb', line 51

def render(*args)
  raise ArgumentError, "data must be set before rendering" if @data.nil?
  super
end