Class: Sinatra::SimpleAssets::Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/simple_assets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, root, files) ⇒ Bundle

Returns a new instance of Bundle.



22
23
24
25
26
27
# File 'lib/sinatra/simple_assets.rb', line 22

def initialize(name, type, root, files)
  @name = name
  @type = type
  @root = root
  @files = files
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



20
21
22
# File 'lib/sinatra/simple_assets.rb', line 20

def files
  @files
end

Instance Method Details

#combinedObject



62
63
64
65
66
# File 'lib/sinatra/simple_assets.rb', line 62

def combined
  @combined ||= @files.map do |file|
    File.open(@root + file) { |f| f.read }
  end.join("\n")
end

#compileObject



72
73
74
75
76
# File 'lib/sinatra/simple_assets.rb', line 72

def compile
  File.open("#{@root}/#{hashed_path}", 'w') do |f|
    f << content
  end
end

#contentObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/sinatra/simple_assets.rb', line 51

def content
  case @type
  when :js
    require 'uglifier'
    @content ||= Uglifier.new.compress combined
  when :css
    require 'cssmin'
    @content ||= CSSMin.minify combined
  end
end

#hashObject



41
42
43
44
45
46
47
48
49
# File 'lib/sinatra/simple_assets.rb', line 41

def hash
  @hash ||= begin
    Digest::SHA1.hexdigest content
  rescue
    # Find the most recent compressed version if the JS runtime is unavailable
    fname = Dir.glob("#{@root}/#{path}/#{@name}-*.#{@type}").sort_by {|f| File.mtime(f)}.last
    fname.split('-').last.sub(".#{@type}", "")
  end
end

#hash_nameObject



33
34
35
# File 'lib/sinatra/simple_assets.rb', line 33

def hash_name
  "#{@name}-#{hash}.#{@type}"
end

#hashed_pathObject



37
38
39
# File 'lib/sinatra/simple_assets.rb', line 37

def hashed_path
  "#{path}/#{hash_name}"
end

#nameObject



29
30
31
# File 'lib/sinatra/simple_assets.rb', line 29

def name
  "#{@name}.#{@type}"
end

#pathObject



68
69
70
# File 'lib/sinatra/simple_assets.rb', line 68

def path
  @type == :js ? 'javascripts' : 'stylesheets'
end