Class: Dassets::AssetFile

Inherits:
Object
  • Object
show all
Defined in:
lib/dassets/asset_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(digest_path) ⇒ AssetFile

Returns a new instance of AssetFile.



10
11
12
13
14
15
16
# File 'lib/dassets/asset_file.rb', line 10

def initialize(digest_path)
  @digest_path = digest_path
  @dirname  = File.dirname(@digest_path)
  @extname  = File.extname(@digest_path)
  @basename = File.basename(@digest_path, @extname)
  @source_proxy = Dassets::SourceProxy.new(@digest_path, Dassets.config.cache)
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



8
9
10
# File 'lib/dassets/asset_file.rb', line 8

def basename
  @basename
end

#digest_pathObject (readonly)

Returns the value of attribute digest_path.



8
9
10
# File 'lib/dassets/asset_file.rb', line 8

def digest_path
  @digest_path
end

#dirnameObject (readonly)

Returns the value of attribute dirname.



8
9
10
# File 'lib/dassets/asset_file.rb', line 8

def dirname
  @dirname
end

#extnameObject (readonly)

Returns the value of attribute extname.



8
9
10
# File 'lib/dassets/asset_file.rb', line 8

def extname
  @extname
end

#source_proxyObject (readonly)

Returns the value of attribute source_proxy.



8
9
10
# File 'lib/dassets/asset_file.rb', line 8

def source_proxy
  @source_proxy
end

Instance Method Details

#==(other_asset_file) ⇒ Object



61
62
63
64
65
# File 'lib/dassets/asset_file.rb', line 61

def ==(other_asset_file)
  other_asset_file.kind_of?(Dassets::AssetFile) &&
  self.digest_path == other_asset_file.digest_path &&
  self.fingerprint == other_asset_file.fingerprint
end

#contentObject



37
38
39
40
# File 'lib/dassets/asset_file.rb', line 37

def content
  return nil if !self.exists?
  @source_proxy.content
end

#digest!Object



18
19
20
21
# File 'lib/dassets/asset_file.rb', line 18

def digest!
  return if !self.exists?
  Dassets.config.file_store.save(self.url){ self.content }
end

#exists?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/dassets/asset_file.rb', line 57

def exists?
  @source_proxy.exists?
end

#fingerprintObject



32
33
34
35
# File 'lib/dassets/asset_file.rb', line 32

def fingerprint
  return nil if !self.exists?
  @source_proxy.fingerprint
end

#hrefObject



28
29
30
# File 'lib/dassets/asset_file.rb', line 28

def href
  "/#{self.url}"
end

#mime_typeObject



52
53
54
55
# File 'lib/dassets/asset_file.rb', line 52

def mime_type
  return nil if !self.exists?
  Rack::Mime.mime_type(@extname)
end

#mtimeObject



42
43
44
45
# File 'lib/dassets/asset_file.rb', line 42

def mtime
  return nil if !self.exists?
  @source_proxy.mtime.httpdate
end

#sizeObject



47
48
49
50
# File 'lib/dassets/asset_file.rb', line 47

def size
  return nil if !self.exists?
  Rack::Utils.bytesize(self.content)
end

#urlObject



23
24
25
26
# File 'lib/dassets/asset_file.rb', line 23

def url
  url_basename = "#{@basename}-#{self.fingerprint}#{@extname}"
  File.join(@dirname, url_basename).sub(/^\.\//, '').sub(/^\//, '')
end