Class: Gumbo::AssetPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/gumbo/asset_package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ AssetPackage

Returns a new instance of AssetPackage.



9
10
11
12
13
# File 'lib/gumbo/asset_package.rb', line 9

def initialize(attrs={})
  attrs.each do |k,v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#digestObject

Returns the value of attribute digest.



7
8
9
# File 'lib/gumbo/asset_package.rb', line 7

def digest
  @digest
end

#filesObject

Returns the value of attribute files.



7
8
9
# File 'lib/gumbo/asset_package.rb', line 7

def files
  @files
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/gumbo/asset_package.rb', line 7

def name
  @name
end

#output_dirObject

Returns the value of attribute output_dir.



7
8
9
# File 'lib/gumbo/asset_package.rb', line 7

def output_dir
  @output_dir
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/gumbo/asset_package.rb', line 7

def type
  @type
end

Instance Method Details

#buildObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gumbo/asset_package.rb', line 27

def build
  out = ""
  files.each do |file|
    out << File.read(file.output_file)
    out << "\n"
  end

  out = compress(out)

  self.digest = Digest::MD5.hexdigest(out)

  open(output_file, 'w') do |f|
    f << out
  end

  logger.info "Created package #{output_file}"
end

#compress(out) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/gumbo/asset_package.rb', line 45

def compress(out)
  case type
  when 'css'
    CSSMin.minify(out)
  when 'js'
    Uglifier.compile(out, :mangle => true)
  else
    raise "Unknown package type '#{type}'"
  end
end

#eql?(o) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/gumbo/asset_package.rb', line 56

def eql?(o)
  o.is_a?(self.class) &&
    o.type == type &&
    o.name == name
end

#file_nameObject



19
20
21
# File 'lib/gumbo/asset_package.rb', line 19

def file_name
  @file_name ||= "#{name}-#{digest}.#{type}"
end

#hashObject



63
64
65
# File 'lib/gumbo/asset_package.rb', line 63

def hash
  type.hash ^ name.hash
end

#loggerObject



67
68
69
# File 'lib/gumbo/asset_package.rb', line 67

def logger
  Gumbo.logger
end

#output_fileObject



23
24
25
# File 'lib/gumbo/asset_package.rb', line 23

def output_file
  @output_file ||= File.join(output_dir, file_name)
end