Class: CatHerder::Assets::Asset
- Inherits:
-
Object
- Object
- CatHerder::Assets::Asset
- Defined in:
- lib/cat_herder/assets/asset.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#logical_path ⇒ Object
readonly
Returns the value of attribute logical_path.
-
#partial ⇒ Object
(also: #partial?)
readonly
Returns the value of attribute partial.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
Instance Method Summary collapse
- #asset_path ⇒ Object
- #cache_key ⇒ Object
- #compile ⇒ Object
- #dependencies ⇒ Object
- #dependency_digests ⇒ Object
- #digest ⇒ Object
- #digest_class ⇒ Object
-
#initialize(logical_path, source_path) ⇒ Asset
constructor
A new instance of Asset.
- #mtime ⇒ Object
- #public_file ⇒ Object
- #public_subpath ⇒ Object
- #read ⇒ Object
- #render ⇒ Object
- #source_mtime ⇒ Object
- #stale? ⇒ Boolean
- #write ⇒ Object
- #write_metadata(digest:, dependencies: nil) ⇒ Object
- #written? ⇒ Boolean
Constructor Details
#initialize(logical_path, source_path) ⇒ Asset
Returns a new instance of Asset.
9 10 11 12 13 14 |
# File 'lib/cat_herder/assets/asset.rb', line 9 def initialize(logical_path, source_path) @logical_path = logical_path @source_path = source_path @partial = File.basename(source_path).start_with?("_") @metadata = Assets.cache.read([self, "metadata"]) || {} end |
Instance Attribute Details
#logical_path ⇒ Object (readonly)
Returns the value of attribute logical_path.
6 7 8 |
# File 'lib/cat_herder/assets/asset.rb', line 6 def logical_path @logical_path end |
#partial ⇒ Object (readonly) Also known as: partial?
Returns the value of attribute partial.
6 7 8 |
# File 'lib/cat_herder/assets/asset.rb', line 6 def partial @partial end |
#source_path ⇒ Object (readonly)
Returns the value of attribute source_path.
6 7 8 |
# File 'lib/cat_herder/assets/asset.rb', line 6 def source_path @source_path end |
Instance Method Details
#asset_path ⇒ Object
74 75 76 77 78 |
# File 'lib/cat_herder/assets/asset.rb', line 74 def asset_path raise AssetNotPublic, self if partial? compile File.join("/", public_subpath) end |
#cache_key ⇒ Object
16 17 18 |
# File 'lib/cat_herder/assets/asset.rb', line 16 def cache_key source_path.delete_prefix(Rails.root.to_s) end |
#compile ⇒ Object
60 61 62 |
# File 'lib/cat_herder/assets/asset.rb', line 60 def compile write if !written? || stale? end |
#dependencies ⇒ Object
28 29 30 |
# File 'lib/cat_herder/assets/asset.rb', line 28 def dependencies @metadata[:dependencies]&.map { |logical_path| Assets[logical_path] } || EMPTY_ARRAY end |
#dependency_digests ⇒ Object
32 33 34 |
# File 'lib/cat_herder/assets/asset.rb', line 32 def dependency_digests @metadata[:dependency_digests] || EMPTY_ARRAY end |
#digest ⇒ Object
24 25 26 |
# File 'lib/cat_herder/assets/asset.rb', line 24 def digest @metadata[:digest] end |
#digest_class ⇒ Object
20 21 22 |
# File 'lib/cat_herder/assets/asset.rb', line 20 def digest_class ActiveSupport::Digest.hash_digest_class end |
#mtime ⇒ Object
36 37 38 |
# File 'lib/cat_herder/assets/asset.rb', line 36 def mtime @metadata[:mtime] || Float::NAN end |
#public_file ⇒ Object
52 53 54 |
# File 'lib/cat_herder/assets/asset.rb', line 52 def public_file Rails.public_path.join(public_subpath) end |
#public_subpath ⇒ Object
48 49 50 |
# File 'lib/cat_herder/assets/asset.rb', line 48 def public_subpath File.join(Assets.public_subpath, logical_path, "#{digest}#{File.extname(logical_path)}") end |
#read ⇒ Object
86 |
# File 'lib/cat_herder/assets/asset.rb', line 86 def read; raise NotImplementedError; end |
#render ⇒ Object
80 81 82 83 |
# File 'lib/cat_herder/assets/asset.rb', line 80 def render compile read end |
#source_mtime ⇒ Object
40 41 42 |
# File 'lib/cat_herder/assets/asset.rb', line 40 def source_mtime Current.mtime(source_path) end |
#stale? ⇒ Boolean
44 45 46 |
# File 'lib/cat_herder/assets/asset.rb', line 44 def stale? mtime != source_mtime || dependency_digests != dependencies.map(&:digest) || dependencies.any?(&:stale?) end |
#write ⇒ Object
85 |
# File 'lib/cat_herder/assets/asset.rb', line 85 def write; raise NotImplementedError; end |
#write_metadata(digest:, dependencies: nil) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/cat_herder/assets/asset.rb', line 64 def (digest:, dependencies: nil) @metadata = { mtime: source_mtime, digest: digest, dependencies: dependencies&.map(&:logical_path), dependency_digests: dependencies&.map(&:digest), } Assets.cache.write([self, "metadata"], @metadata) end |