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.



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

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.



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

def basename
  @basename
end

#digest_pathObject (readonly)

Returns the value of attribute digest_path.



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

def digest_path
  @digest_path
end

#dirnameObject (readonly)

Returns the value of attribute dirname.



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

def dirname
  @dirname
end

#extnameObject (readonly)

Returns the value of attribute extname.



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

def extname
  @extname
end

#source_proxyObject (readonly)

Returns the value of attribute source_proxy.



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

def source_proxy
  @source_proxy
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  other.is_a?(Dassets::AssetFile) &&
  digest_path == other.digest_path &&
  fingerprint == other.fingerprint
end

#contentObject



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

def content
  return nil unless exists?
  @source_proxy.content
end

#digest!Object



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

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

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  @source_proxy.exists?
end

#fingerprintObject



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

def fingerprint
  return nil unless exists?
  @source_proxy.fingerprint
end

#mime_typeObject



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

def mime_type
  return nil unless exists?
  Rack::Mime.mime_type(@extname)
end

#mtimeObject



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

def mtime
  return nil unless exists?
  @source_proxy.mtime.httpdate
end

#response_headersObject



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

def response_headers
  @source_proxy.response_headers
end

#sizeObject



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

def size
  return nil unless exists?
  content.bytesize
end

#urlObject Also known as: href



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

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