Class: Phronomy::Agent::ContextPolicyDescriptor

Inherits:
Data
  • Object
show all
Defined in:
lib/phronomy/agent/context_policy_descriptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, version:, config: {}) ⇒ ContextPolicyDescriptor

Returns a new instance of ContextPolicyDescriptor.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/phronomy/agent/context_policy_descriptor.rb', line 8

def initialize(id:, version:, config: {})
  normalized_config = Immutable.copy(config || {})
  Immutable.validate_canonical_json!(normalized_config, label: "Context Policy config")
  super(
    id: id.to_s.freeze,
    version: Integer(version),
    config: normalized_config
  )
  raise ArgumentError, "Context Policy id must not be empty" if id.empty?
  raise ArgumentError, "Context Policy version must be positive" unless version.positive?
  freeze
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config

Returns:

  • (Object)

    the current value of config



7
8
9
# File 'lib/phronomy/agent/context_policy_descriptor.rb', line 7

def config
  @config
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



7
8
9
# File 'lib/phronomy/agent/context_policy_descriptor.rb', line 7

def id
  @id
end

#versionObject (readonly)

Returns the value of attribute version

Returns:

  • (Object)

    the current value of version



7
8
9
# File 'lib/phronomy/agent/context_policy_descriptor.rb', line 7

def version
  @version
end

Class Method Details

.from_h(hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/phronomy/agent/context_policy_descriptor.rb', line 21

def self.from_h(hash)
  descriptor = new(
    id: hash.fetch("id") { hash.fetch(:id) },
    version: hash.fetch("version") { hash.fetch(:version) },
    config: hash["config"] || hash[:config] || {}
  )
  expected_digest = hash["config_digest"] || hash[:config_digest]
  if expected_digest && expected_digest.to_s != descriptor.config_digest
    raise Phronomy::ConfigurationError,
      "Context Policy config digest mismatch for #{descriptor.id}@#{descriptor.version}"
  end
  descriptor
end

Instance Method Details

#config_digestObject



35
36
37
# File 'lib/phronomy/agent/context_policy_descriptor.rb', line 35

def config_digest
  "sha256:#{Digest::SHA256.hexdigest(Phronomy::CanonicalJSON.dump(config))}"
end

#to_hObject



39
40
41
42
43
44
45
46
# File 'lib/phronomy/agent/context_policy_descriptor.rb', line 39

def to_h
  {
    "id" => id,
    "version" => version,
    "config" => config,
    "config_digest" => config_digest
  }
end