Class: CatHerder::Assets::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/cat_herder/assets/asset.rb

Direct Known Subclasses

ErbAsset, VerbatimAsset

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject (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

#partialObject (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_pathObject (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_pathObject

Raises:



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_keyObject



16
17
18
# File 'lib/cat_herder/assets/asset.rb', line 16

def cache_key
  source_path.delete_prefix(Rails.root.to_s)
end

#compileObject



60
61
62
# File 'lib/cat_herder/assets/asset.rb', line 60

def compile
  write if !written? || stale?
end

#dependenciesObject



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_digestsObject



32
33
34
# File 'lib/cat_herder/assets/asset.rb', line 32

def dependency_digests
  @metadata[:dependency_digests] || EMPTY_ARRAY
end

#digestObject



24
25
26
# File 'lib/cat_herder/assets/asset.rb', line 24

def digest
  @metadata[:digest]
end

#digest_classObject



20
21
22
# File 'lib/cat_herder/assets/asset.rb', line 20

def digest_class
  ActiveSupport::Digest.hash_digest_class
end

#mtimeObject



36
37
38
# File 'lib/cat_herder/assets/asset.rb', line 36

def mtime
  @metadata[:mtime] || Float::NAN
end

#public_fileObject



52
53
54
# File 'lib/cat_herder/assets/asset.rb', line 52

def public_file
  Rails.public_path.join(public_subpath)
end

#public_subpathObject



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

#readObject

Raises:

  • (NotImplementedError)


86
# File 'lib/cat_herder/assets/asset.rb', line 86

def read; raise NotImplementedError; end

#renderObject



80
81
82
83
# File 'lib/cat_herder/assets/asset.rb', line 80

def render
  compile
  read
end

#source_mtimeObject



40
41
42
# File 'lib/cat_herder/assets/asset.rb', line 40

def source_mtime
  Current.mtime(source_path)
end

#stale?Boolean

Returns:

  • (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

#writeObject

Raises:

  • (NotImplementedError)


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

#written?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cat_herder/assets/asset.rb', line 56

def written?
  Current.mtime(public_file.to_s) > 0
end