Class: Igniter::Store::Fact

Inherits:
Struct
  • Object
show all
Defined in:
lib/igniter/store/fact.rb,
lib/igniter/store/fact.rb

Overview

Reopen Fact (Ruby Struct or native class) and add from_h + normalizations.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#causationObject

Returns the value of attribute causation

Returns:

  • (Object)

    the current value of causation



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def causation
  @causation
end

#derivationObject

Returns the value of attribute derivation

Returns:

  • (Object)

    the current value of derivation



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def derivation
  @derivation
end

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def id
  @id
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def key
  @key
end

#producerObject

Returns the value of attribute producer

Returns:

  • (Object)

    the current value of producer



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def producer
  @producer
end

#schema_versionObject

Returns the value of attribute schema_version

Returns:

  • (Object)

    the current value of schema_version



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def schema_version
  @schema_version
end

#storeObject

Returns the value of attribute store

Returns:

  • (Object)

    the current value of store



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def store
  @store
end

#transaction_timeObject Also known as: timestamp

Returns the value of attribute transaction_time

Returns:

  • (Object)

    the current value of transaction_time



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def transaction_time
  @transaction_time
end

#valid_timeObject Also known as: term

Returns the value of attribute valid_time

Returns:

  • (Object)

    the current value of valid_time



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def valid_time
  @valid_time
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def value
  @value
end

#value_hashObject

Returns the value of attribute value_hash

Returns:

  • (Object)

    the current value of value_hash



12
13
14
# File 'lib/igniter/store/fact.rb', line 12

def value_hash
  @value_hash
end

Class Method Details

.build(store:, key:, value:, causation: nil, valid_time: nil, term: nil, schema_version: 1, producer: nil, derivation: nil) ⇒ Object

Canonical build entry point. valid_time: domain time (writer-supplied, nullable Float). term: backward-compat alias for valid_time — accepted but deprecated.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/igniter/store/fact.rb', line 29

def self.build(store:, key:, value:, causation: nil, valid_time: nil, term: nil,
               schema_version: 1, producer: nil, derivation: nil)
  vt = valid_time.nil? ? (term ? term.to_f : nil) : valid_time.to_f
  serialized = JSON.generate(stable_sort(value))
  new(
    id:               SecureRandom.uuid,
    store:            store,
    key:              key,
    value:            deep_freeze(value),
    value_hash:       Digest::SHA256.hexdigest(serialized),
    causation:        causation,
    transaction_time: Process.clock_gettime(Process::CLOCK_REALTIME),
    valid_time:       vt,
    schema_version:   schema_version,
    producer:         producer ? deep_freeze(producer) : nil,
    derivation:       derivation ? deep_freeze(derivation) : nil
  ).freeze
end

.from_h(h) ⇒ Object

Reconstruct a Fact from a wire-deserialized hash. Accepts both old field names (timestamp, term) and new names (transaction_time, valid_time) for smooth transition.

In native mode: Fact.new is unavailable (no Ruby allocator), so we call Fact.build which recomputes id and transaction_time. This is a known Phase 2 gap for time-travel fidelity over the network.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/igniter/store/fact.rb', line 95

def self.from_h(h)
  h = h.transform_keys(&:to_sym)
  h[:store]    = h.fetch(:store).to_sym
  # Accept both old (timestamp) and new (transaction_time) key names.
  h[:transaction_time] = (h[:transaction_time] || h[:timestamp])&.to_f || 0.0
  h[:valid_time]       = (h[:valid_time] || h[:term])&.to_f

  if defined?(Igniter::Store::NATIVE) && Igniter::Store::NATIVE
    build(
      store:        h[:store],
      key:          h[:key],
      value:        h[:value],
      causation:    h[:causation],
      valid_time:   h[:valid_time],
      schema_version: h.fetch(:schema_version, 1),
      producer:     h[:producer],
      derivation:   h[:derivation]
    )
  else
    new(**h.slice(:id, :store, :key, :value, :value_hash, :causation,
                  :transaction_time, :valid_time, :schema_version,
                  :producer, :derivation)).freeze
  end
end

Instance Method Details

#_native_store_strObject

Returns the value of attribute store Native extension stores ‘store` as a Rust String; normalize to Symbol to match the Ruby Struct fallback behaviour.

Returns:

  • (Object)

    the current value of store



84
85
86
# File 'lib/igniter/store/fact.rb', line 84

def store
  @store
end