Class: Dassets::SourceFile
- Inherits:
-
Object
- Object
- Dassets::SourceFile
- Defined in:
- lib/dassets/source_file.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other_source_file) ⇒ Object
- #asset_file ⇒ Object
- #compiled ⇒ Object
- #digest_path ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(file_path) ⇒ SourceFile
constructor
A new instance of SourceFile.
- #mtime ⇒ Object
- #response_headers ⇒ Object
-
#source ⇒ Object
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.
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_path ⇒ Object (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, **) Dassets.source_files[path] || Dassets::NullSourceFile.new(path, **) 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_file ⇒ Object
31 32 33 |
# File 'lib/dassets/source_file.rb', line 31 def asset_file @asset_file ||= Dassets::AssetFile.new(self.digest_path) end |
#compiled ⇒ Object
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_path ⇒ Object
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
62 63 64 |
# File 'lib/dassets/source_file.rb', line 62 def exists? File.file?(@file_path) end |
#mtime ⇒ Object
66 67 68 |
# File 'lib/dassets/source_file.rb', line 66 def mtime File.mtime(@file_path) end |
#response_headers ⇒ Object
70 71 72 |
# File 'lib/dassets/source_file.rb', line 70 def response_headers self.source.nil? ? Hash.new : self.source.response_headers end |
#source ⇒ Object
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 |