Class: Julewire::Core::Records::Record

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/julewire/core/records/record.rb

Constant Summary collapse

KINDS =
{
  "point" => :point,
  "summary" => :summary
}.freeze
HASH_SECTIONS =
Fields::Bags.record_hash_sections
REQUIRED_KEYS =
Fields::Bags.required_record_keys

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, lineage: nil) ⇒ Record

Returns a new instance of Record.



166
167
168
169
170
# File 'lib/julewire/core/records/record.rb', line 166

def initialize(data, lineage: nil)
  @data = data
  @lineage = (lineage || Execution::Lineage.from_execution_hash(fetch(:execution))).freeze
  freeze
end

Instance Attribute Details

#lineageObject (readonly)



164
165
166
# File 'lib/julewire/core/records/record.rb', line 164

def lineage
  @lineage
end

Class Method Details

.from_normalized_hash(record, lineage: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/julewire/core/records/record.rb', line 21

def from_normalized_hash(record, lineage: nil)
  validate_normalized_hash!(record)
  execution = record.fetch(:execution)
  lineage ||= Execution::Lineage.from_execution_hash(execution)
  record = record.merge(
    execution: Execution::Lineage.clean_normalized_lazy_relationship_hash(execution)
  )
  new(snapshot_hash(record), lineage: lineage)
end

.from_owned_hash(record, lineage: nil, trust_frozen: false) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/julewire/core/records/record.rb', line 31

def from_owned_hash(record, lineage: nil, trust_frozen: false)
  validate_normalized_hash!(record)
  lineage ||= Execution::Lineage.from_execution_hash(record.fetch(:execution))
  execution = Execution::Lineage.clean_normalized_lazy_relationship_hash(record.fetch(:execution))
  record = record.frozen? ? record.merge(execution: execution) : replace_execution(record, execution)
  new(Serialization::DeepFreeze.call(record, trust_frozen: trust_frozen), lineage: lineage)
end

.validate_normalized!(record) ⇒ Object

Raises:

  • (TypeError)


39
40
41
42
43
# File 'lib/julewire/core/records/record.rb', line 39

def validate_normalized!(record)
  return record if record.is_a?(self)

  raise TypeError, "expected Julewire::Record"
end

.validate_normalized_hash!(record) ⇒ Object



45
46
47
48
# File 'lib/julewire/core/records/record.rb', line 45

def validate_normalized_hash!(record)
  validate_hash!(record)
  record
end

Instance Method Details

#==(other) ⇒ Object



191
192
193
# File 'lib/julewire/core/records/record.rb', line 191

def ==(other)
  other.instance_of?(Record) && @data == other.serializable_data
end

#[](key) ⇒ Object



172
# File 'lib/julewire/core/records/record.rb', line 172

def [](key) = @data[key]

#digObject



176
# File 'lib/julewire/core/records/record.rb', line 176

def dig(...) = @data.dig(...)

#eachObject



180
# File 'lib/julewire/core/records/record.rb', line 180

def each(&) = @data.each(&)

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/julewire/core/records/record.rb', line 195

def eql?(other)
  other.instance_of?(Record) && @data.eql?(other.serializable_data)
end

#fetchObject



174
# File 'lib/julewire/core/records/record.rb', line 174

def fetch(...) = @data.fetch(...)

#hashObject



199
# File 'lib/julewire/core/records/record.rb', line 199

def hash = @data.hash

#inspectObject



201
# File 'lib/julewire/core/records/record.rb', line 201

def inspect = "#<#{self.class} #{@data}>"

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


178
# File 'lib/julewire/core/records/record.rb', line 178

def key?(key) = @data.key?(key)

#serializable_dataObject



185
# File 'lib/julewire/core/records/record.rb', line 185

def serializable_data = @data

#to_hObject



182
# File 'lib/julewire/core/records/record.rb', line 182

def to_h = Fields::FieldSet.deep_dup_owned(@data)