Class: Pakyow::Assets::Pack

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/assets/pack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, prefix: "/") ⇒ Pack

Returns a new instance of Pack.



18
19
20
21
22
23
24
25
# File 'lib/pakyow/assets/pack.rb', line 18

def initialize(name, config, prefix: "/")
  @name, @config = name, config
  @assets = []
  @packed = { js: [], css: [] }
  @public_path = String.normalize_path(
    File.join(config.prefix, prefix, "packs", name.to_s)
  )
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/pakyow/assets/pack.rb', line 16

def name
  @name
end

#public_pathObject (readonly)

Returns the value of attribute public_path.



16
17
18
# File 'lib/pakyow/assets/pack.rb', line 16

def public_path
  @public_path
end

Instance Method Details

#<<(asset) ⇒ Object



41
42
43
# File 'lib/pakyow/assets/pack.rb', line 41

def <<(asset)
  @assets << asset.disable_source_map
end

#finalizeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pakyow/assets/pack.rb', line 27

def finalize
  tap do
    if @config.fingerprint
      extension = File.extname(@public_path)
      @public_path = File.join(
        File.dirname(@public_path),
        File.basename(@public_path, extension) + "__" + fingerprint + extension
      )
    end

    pack_assets!
  end
end

#fingerprintObject



69
70
71
72
73
# File 'lib/pakyow/assets/pack.rb', line 69

def fingerprint
  @assets.flat_map(&:fingerprint).sort.each_with_object(Digest::MD5.new) { |fingerprint, digest|
    digest.update(fingerprint)
  }.hexdigest
end

#javascriptsObject



53
54
55
# File 'lib/pakyow/assets/pack.rb', line 53

def javascripts
  @packed[:js]
end

#javascripts?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/pakyow/assets/pack.rb', line 61

def javascripts?
  javascripts.any?
end

#packed(path) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/pakyow/assets/pack.rb', line 45

def packed(path)
  if path.start_with?(@public_path + ".")
    @packed[File.extname(path)[1..-1].to_sym]
  else
    nil
  end
end

#public_css_pathObject



79
80
81
# File 'lib/pakyow/assets/pack.rb', line 79

def public_css_path
  @public_path + ".css"
end

#public_js_pathObject



75
76
77
# File 'lib/pakyow/assets/pack.rb', line 75

def public_js_path
  @public_path + ".js"
end

#stylesheetsObject



57
58
59
# File 'lib/pakyow/assets/pack.rb', line 57

def stylesheets
  @packed[:css]
end

#stylesheets?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/pakyow/assets/pack.rb', line 65

def stylesheets?
  stylesheets.any?
end