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) ⇒ Object



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

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

Instance Method Details

#==(other_source_file) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/dassets/source_file.rb', line 74

def ==(other_source_file)
  if other_source_file.is_a?(self.class)
    self.file_path == other_source_file.file_path
  else
    super
  end
end

#asset_fileObject



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

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

#compiledObject



54
55
56
57
58
59
60
# File 'lib/dassets/source_file.rb', line 54

def compiled
  @ext_list.reduce(read_file(@file_path)) { |file_acc, ext|
    self.source.engines[ext].reduce(file_acc) { |ext_acc, engine|
      engine.compile(ext_acc)
    }
  }
end

#digest_pathObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dassets/source_file.rb', line 35

def digest_path
  @digest_path ||=
    begin
      digest_basename =
        @ext_list
          .reduce([]) { |digest_ext_list, ext|
            digest_ext_list <<
              self.source.engines[ext].reduce(ext) { |ext_acc, engine|
                engine.ext(ext_acc)
              }
          }
          .reject(&:empty?)
          .reverse
          .join(".")

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

#exists?Boolean

Returns:

  • (Boolean)


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

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

#mtimeObject



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

def mtime
  File.mtime(@file_path)
end

#response_headersObject



70
71
72
# File 'lib/dassets/source_file.rb', line 70

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
29
# File 'lib/dassets/source_file.rb', line 24

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