Class: Crush::Engine

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, options = nil) ⇒ Engine

Create a new engine with the file and options specified. By default, the data to compress is read from the file. When a block is given, it should read data and return as a String.

All arguments are optional.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/crush/engine.rb', line 37

def initialize(file = nil, options = nil)
  if file.respond_to?(:to_hash)
    @options = file.to_hash
  else
    @file    = file
    @options = options || {}
  end
  
  unless self.class.engine_initialized?
    initialize_engine
    self.class.engine_initialized = true
  end
  
  @data = if block_given?
    yield
  elsif @file
    File.respond_to?(:binread) ? File.binread(@file) : File.read(@file)
  end
  
  prepare
end

Class Attribute Details

.engine_initializedObject Also known as: engine_initialized?

Returns the value of attribute engine_initialized.



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

def engine_initialized
  @engine_initialized
end

Instance Attribute Details

#dataObject (readonly)

The data to cmopress; loaded from a file or given directly.



11
12
13
# File 'lib/crush/engine.rb', line 11

def data
  @data
end

#fileObject (readonly)

The name of the file to be compressed



4
5
6
# File 'lib/crush/engine.rb', line 4

def file
  @file
end

#optionsObject (readonly)

A Hash of compression engine specific options. This is passed directly to the underlying engine and is not used by the generic interface.



8
9
10
# File 'lib/crush/engine.rb', line 8

def options
  @options
end

Class Method Details

.engine_nameObject

Returns a lowercase, underscored name for the engine.



21
22
23
24
25
26
27
28
29
# File 'lib/crush/engine.rb', line 21

def engine_name
  engine_name = name.to_s.dup
  engine_name.sub!  /^.*::/ , ""
  engine_name.gsub! /([A-Z]+)([A-Z][a-z])/, '\1_\2'
  engine_name.gsub! /([a-z\d])([A-Z])/, '\1_\2'
  engine_name.tr!   "-", "_"
  engine_name.downcase!
  engine_name
end

Instance Method Details

#render(data = nil) ⇒ Object Also known as: compress, compile

Compresses the data. Data can be read through the file or the block given at initialization, or through passing it here directly.



61
62
63
64
# File 'lib/crush/engine.rb', line 61

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