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.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dassets/asset_file.rb', line 11

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,
      content_cache:     Dassets.config.content_cache,
      fingerprint_cache: Dassets.config.fingerprint_cache,
    )
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



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

def basename
  @basename
end

#digest_pathObject (readonly)

Returns the value of attribute digest_path.



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

def digest_path
  @digest_path
end

#dirnameObject (readonly)

Returns the value of attribute dirname.



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

def dirname
  @dirname
end

#extnameObject (readonly)

Returns the value of attribute extname.



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

def extname
  @extname
end

#source_proxyObject (readonly)

Returns the value of attribute source_proxy.



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

def source_proxy
  @source_proxy
end

Instance Method Details

#==(other_asset_file) ⇒ Object



70
71
72
73
74
# File 'lib/dassets/asset_file.rb', line 70

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



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

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

#digest!Object



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

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

#exists?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dassets/asset_file.rb', line 66

def exists?
  @source_proxy.exists?
end

#fingerprintObject



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

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

#mime_typeObject



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

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

#mtimeObject



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

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

#response_headersObject



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

def response_headers
  @source_proxy.response_headers
end

#sizeObject



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

def size
  return nil if !self.exists?
  self.content.bytesize
end

#urlObject Also known as: href



29
30
31
32
33
34
# File 'lib/dassets/asset_file.rb', line 29

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