Class: Igniter::Extensions::Contracts::ContentAddressing::Declaration

Inherits:
Object
  • Object
show all
Defined in:
lib/igniter/extensions/contracts/content_addressing/declaration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callable:, fingerprint:, cache:, capabilities:) ⇒ Declaration

Returns a new instance of Declaration.



10
11
12
13
14
15
16
# File 'lib/igniter/extensions/contracts/content_addressing/declaration.rb', line 10

def initialize(callable:, fingerprint:, cache:, capabilities:)
  @callable = callable
  @fingerprint = fingerprint.to_s.freeze
  @cache = cache
  @capabilities = Array(capabilities).map(&:to_sym).uniq.freeze
  freeze
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'lib/igniter/extensions/contracts/content_addressing/declaration.rb', line 8

def cache
  @cache
end

#callableObject (readonly)

Returns the value of attribute callable.



8
9
10
# File 'lib/igniter/extensions/contracts/content_addressing/declaration.rb', line 8

def callable
  @callable
end

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



8
9
10
# File 'lib/igniter/extensions/contracts/content_addressing/declaration.rb', line 8

def capabilities
  @capabilities
end

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



8
9
10
# File 'lib/igniter/extensions/contracts/content_addressing/declaration.rb', line 8

def fingerprint
  @fingerprint
end

Instance Method Details

#call(**kwargs) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/igniter/extensions/contracts/content_addressing/declaration.rb', line 18

def call(**kwargs)
  key = content_key(kwargs)
  cached = cache.fetch(key)
  return cached unless cached.nil?

  value = callable.call(**kwargs)
  cache.store(key, value)
  value
end

#content_fingerprintObject



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

def content_fingerprint
  fingerprint
end

#content_key(inputs) ⇒ Object



40
41
42
# File 'lib/igniter/extensions/contracts/content_addressing/declaration.rb', line 40

def content_key(inputs)
  ContentKey.compute(fingerprint: fingerprint, inputs: inputs)
end

#declared_capabilitiesObject



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

def declared_capabilities
  capabilities
end

#pure?Boolean

Returns:

  • (Boolean)


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

def pure?
  capabilities.include?(:pure)
end