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.



148
149
150
151
152
# File 'lib/julewire/core/records/record.rb', line 148

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

Instance Attribute Details

#lineageObject (readonly)



146
147
148
# File 'lib/julewire/core/records/record.rb', line 146

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
38
39
40
# File 'lib/julewire/core/records/record.rb', line 31

def from_owned_hash(record, lineage: nil, trust_frozen: false)
  validate_normalized_hash_structure!(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, validate_symbol_keys: true),
    lineage: lineage
  )
end

.validate_normalized!(record) ⇒ Object

Raises:

  • (TypeError)


42
43
44
45
46
# File 'lib/julewire/core/records/record.rb', line 42

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

  raise TypeError, "expected Julewire::Record"
end

.validate_normalized_hash!(record) ⇒ Object



48
49
50
51
# File 'lib/julewire/core/records/record.rb', line 48

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

Instance Method Details

#==(other) ⇒ Object



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

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

#[](key) ⇒ Object



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

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

#digObject



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

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

#eachObject



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#fetchObject



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

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

#hashObject



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

def hash = @data.hash

#inspectObject



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#serializable_dataObject



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

def serializable_data = @data

#to_hObject



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

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