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.



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

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.



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

def file_path
  @file_path
end

Class Method Details

.find_by_digest_path(path, **options) ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/dassets/source_file.rb', line 85

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

#asset_fileObject



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

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

#base_pathObject



77
78
79
# File 'lib/dassets/source_file.rb', line 77

def base_path
  source&.base_path.to_s
end

#compiledObject



61
62
63
64
65
66
67
# File 'lib/dassets/source_file.rb', line 61

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

#digest_pathObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dassets/source_file.rb', line 36

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

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

#exists?Boolean

Returns:

  • (Boolean)


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

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

#mtimeObject



73
74
75
# File 'lib/dassets/source_file.rb', line 73

def mtime
  File.mtime(@file_path)
end

#response_headersObject



81
82
83
# File 'lib/dassets/source_file.rb', line 81

def response_headers
  source.nil? ? {} : 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.



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

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