Class: Assetify::Pathfix

Inherits:
Object
  • Object
show all
Defined in:
lib/assetify/asset/pathfix.rb

Overview

Attempt to fix assets in js/css for #image_url

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chunk, renderer = :erb, ns = nil) ⇒ Pathfix

Returns a new instance of Pathfix.



6
7
8
9
10
11
# File 'lib/assetify/asset/pathfix.rb', line 6

def initialize(chunk, renderer = :erb, ns = nil)
  @chunk = chunk
  @renderer = renderer
  @ns = ns
  @images = scan_images
end

Instance Attribute Details

#imagesObject (readonly)

Returns the value of attribute images.



13
14
15
# File 'lib/assetify/asset/pathfix.rb', line 13

def images
  @images
end

Instance Method Details

#fixObject



28
29
30
31
32
33
# File 'lib/assetify/asset/pathfix.rb', line 28

def fix
  @images.each do |path|
    @chunk["url(#{path})"] = replace path.split('/').last
  end
  @renderer != :erb ? tmpl_chunk : @chunk
end

#replace(src) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/assetify/asset/pathfix.rb', line 19

def replace(src)
  fpath = @ns ? "#{@ns}/#{src}" : src
  if @renderer == :erb
    "url('<%= image_path('#{fpath}') %>')"
  else
    "image-url('#{fpath}')"
  end
end

#scan_imagesObject



15
16
17
# File 'lib/assetify/asset/pathfix.rb', line 15

def scan_images
  @chunk.scan(%r{url\(([a-zA-Z0-9/\_\-\.]*\.\w+)\)}xo).flatten
end

#tmpl_chunkObject



35
36
37
38
39
40
# File 'lib/assetify/asset/pathfix.rb', line 35

def tmpl_chunk
  require 'sass/css'
  Sass::CSS.new(@chunk).render(@renderer)
rescue Sass::SyntaxError => e
  @error = e
end