Module: Igniter::Extensions::Contracts::ContentAddressingPack

Defined in:
lib/igniter/extensions/contracts/content_addressing_pack.rb

Class Method Summary collapse

Class Method Details

.cacheObject



24
25
26
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 24

def cache
  @cache ||= ContentAddressing::Cache.new
end

.cache=(value) ⇒ Object



28
29
30
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 28

def cache=(value)
  @cache = value
end

.content_addressed(callable: nil, fingerprint: nil, capabilities: [:pure], cache: self.cache, &block) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 53

def content_addressed(callable: nil, fingerprint: nil, capabilities: [:pure], cache: self.cache, &block)
  target = callable || block
  raise ArgumentError, "content_addressed requires a callable or block" unless target

  ContentAddressing::Declaration.new(
    callable: target,
    fingerprint: fingerprint || default_fingerprint_for(target),
    cache: cache,
    capabilities: capabilities
  )
end

.content_key(inputs:, fingerprint: nil, callable: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 40

def content_key(inputs:, fingerprint: nil, callable: nil)
  resolved_fingerprint =
    if fingerprint
      fingerprint
    elsif callable
      callable.respond_to?(:content_fingerprint) ? callable.content_fingerprint : default_fingerprint_for(callable)
    else
      raise ArgumentError, "content_key requires fingerprint: or callable:"
    end

  ContentAddressing::ContentKey.compute(fingerprint: resolved_fingerprint, inputs: inputs)
end

.default_fingerprint_for(target) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 75

def default_fingerprint_for(target)
  if target.respond_to?(:content_fingerprint)
    target.content_fingerprint
  elsif target.is_a?(Proc)
    file, line = target.source_location
    "proc:#{file}:#{line}"
  elsif target.respond_to?(:name) && !target.name.nil?
    target.name
  else
    "anonymous_callable"
  end
end

.install_into(kernel) ⇒ Object



20
21
22
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 20

def install_into(kernel)
  kernel
end

.manifestObject



13
14
15
16
17
18
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 13

def manifest
  Igniter::Contracts::PackManifest.new(
    name: :extensions_content_addressing,
    metadata: { category: :runtime, capabilities: [:pure] }
  )
end

.pure(callable: nil, fingerprint: nil, cache: self.cache, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 65

def pure(callable: nil, fingerprint: nil, cache: self.cache, &block)
  content_addressed(
    callable: callable,
    fingerprint: fingerprint,
    capabilities: [:pure],
    cache: cache,
    &block
  )
end

.reset_cache!Object



32
33
34
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 32

def reset_cache!
  cache.clear
end

.statsObject



36
37
38
# File 'lib/igniter/extensions/contracts/content_addressing_pack.rb', line 36

def stats
  cache.stats
end