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.



21
22
23
24
# File 'lib/dassets/source_file.rb', line 21

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.



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

def file_path
  @file_path
end

Class Method Details

.find_by_digest_path(path, cache = nil) ⇒ Object



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

def self.find_by_digest_path(path, cache = nil)
  # look in the configured source list
  source_files = Dassets.source_list.map{ |p| self.new(p) }

  # get the last matching one (in case two source files have the same digest
  # path the last one *should* be correct since it was last to be configured)
  source_files.select{ |s| s.digest_path == path }.last || NullSourceFile.new(path, cache)
end

Instance Method Details

#==(other_source_file) ⇒ Object



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

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

#asset_fileObject



35
36
37
# File 'lib/dassets/source_file.rb', line 35

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

#compiledObject



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

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

#digest_pathObject



39
40
41
42
43
44
45
46
47
# File 'lib/dassets/source_file.rb', line 39

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)


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

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

#mtimeObject



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

def mtime
  File.mtime(@file_path)
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.



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

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