Class: Dassets::SourceFile

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

Direct Known Subclasses

NullSourceFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ SourceFile

Returns a new instance of SourceFile.



16
17
18
19
# File 'lib/dassets/source_file.rb', line 16

def initialize(file_path)
  @file_path = file_path.to_s
  @ext_list  = File.basename(@file_path).split('.').reverse
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



14
15
16
# File 'lib/dassets/source_file.rb', line 14

def file_path
  @file_path
end

Class Method Details

.find_by_digest_path(path, options = nil) ⇒ Object



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

def self.find_by_digest_path(path, options = nil)
  Dassets.source_files[path] || NullSourceFile.new(path, options)
end

Instance Method Details

#==(other_source_file) ⇒ Object



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

def ==(other_source_file)
  self.file_path == other_source_file.file_path
end

#asset_fileObject



30
31
32
# File 'lib/dassets/source_file.rb', line 30

def asset_file
  @asset_file ||= Dassets::AssetFile.new(self.digest_path)
end

#compiledObject



44
45
46
47
48
# File 'lib/dassets/source_file.rb', line 44

def compiled
  @ext_list.inject(read_file(@file_path)) do |content, ext|
    self.source.engines[ext].compile(content)
  end
end

#digest_pathObject



34
35
36
37
38
39
40
41
42
# File 'lib/dassets/source_file.rb', line 34

def digest_path
  @digest_path ||= begin
    digest_basename = @ext_list.inject([]) do |digest_ext_list, ext|
      digest_ext_list << self.source.engines[ext].ext(ext)
    end.reject(&:empty?).reverse.join('.')

    File.join([digest_dirname(@file_path), digest_basename].reject(&:empty?))
  end
end

#exists?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/dassets/source_file.rb', line 50

def exists?
  File.file?(@file_path)
end

#mtimeObject



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

def mtime
  File.mtime(@file_path)
end

#response_headersObject



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

def response_headers
  self.source.nil? ? Hash.new : self.source.response_headers
end

#sourceObject

get the last matching one (in the case two sources with the same path are configured) since we select the last matching source file (from the last configured source) in ‘find_by_digest_path` above.



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

def source
  @source ||= Dassets.config.sources.select do |source|
    @file_path =~ /^#{slash_path(source.path)}/
  end.last
end