Class: Linner::Asset

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

Defined Under Namespace

Classes: RenderError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Asset

Returns a new instance of Asset.



9
10
11
12
# File 'lib/linner/asset.rb', line 9

def initialize(path)
  @path = path
  @mtime = File.mtime(path).to_i if File.exist?(path)
end

Instance Attribute Details

#content(context = nil) ⇒ Object

Returns the value of attribute content.



7
8
9
# File 'lib/linner/asset.rb', line 7

def content
  @content
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/linner/asset.rb', line 7

def path
  @path
end

Instance Method Details

#compressObject



94
95
96
# File 'lib/linner/asset.rb', line 94

def compress
  @content = Compressor.compress(self)
end

#digest_pathObject



22
23
24
25
# File 'lib/linner/asset.rb', line 22

def digest_path
  digest = Digest::MD5.hexdigest content
  path.chomp(extname) << "-#{digest}" << extname
end

#eruby?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/linner/asset.rb', line 79

def eruby?
  Tilt[path] and Tilt[path].default_mime_type = "application/x-eruby"
end

#extnameObject



18
19
20
# File 'lib/linner/asset.rb', line 18

def extname
  @extname = File.extname path
end

#javascript?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/linner/asset.rb', line 67

def javascript?
  Tilt[path] and Tilt[path].default_mime_type == "application/javascript"
end

#logical_pathObject



98
99
100
# File 'lib/linner/asset.rb', line 98

def logical_path
  @logical_path ||= @path.gsub(/^(#{Linner.env.paths.join("|")})\/?/, "")
end

#mtimeObject



14
15
16
# File 'lib/linner/asset.rb', line 14

def mtime
  @mtime
end

#relative_digest_pathObject



27
28
29
# File 'lib/linner/asset.rb', line 27

def relative_digest_path
  digest_path.gsub /#{Linner.env.public_folder}/, ""
end

#revable?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/linner/asset.rb', line 31

def revable?
  javascript? or stylesheet?
end

#revision!Object



35
36
37
# File 'lib/linner/asset.rb', line 35

def revision!
  File.rename path, digest_path
end

#stylesheet?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/linner/asset.rb', line 71

def stylesheet?
  Tilt[path] and Tilt[path].default_mime_type == "text/css"
end

#template?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/linner/asset.rb', line 75

def template?
  Tilt[path] and Tilt[path].default_mime_type == "text/template"
end

#wrap(source) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/linner/asset.rb', line 55

def wrap(source)
  if javascript?
    Wrapper::Module.wrap(logical_path.chomp(File.extname logical_path), source)
  elsif template?
    if File.basename(path).start_with?("_")
      Wrapper::Template.partial_wrap(logical_path.chomp(File.extname logical_path), source)
    else
      Wrapper::Template.wrap(logical_path.chomp(File.extname logical_path), source)
    end
  end
end

#wrappable?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/linner/asset.rb', line 83

def wrappable?
  !!(self.javascript? and !Linner.env.modules_ignored.include?(@path) or self.template?)
end

#writeObject



87
88
89
90
91
92
# File 'lib/linner/asset.rb', line 87

def write
  FileUtils.mkdir_p File.dirname(@path)
  File.open @path, "w" do |file|
    file.write @content
  end
end