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.



136
137
138
139
140
# File 'lib/julewire/core/records/record.rb', line 136

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

Instance Attribute Details

#lineageObject (readonly)



134
135
136
# File 'lib/julewire/core/records/record.rb', line 134

def lineage
  @lineage
end

Class Method Details

.from_normalized_hash(record, lineage: nil) ⇒ Object



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

def from_normalized_hash(record, lineage: nil)
  if record.is_a?(Hash)
    lineage ||= Execution::Lineage.from_execution_hash(record[:execution])
    record = record.merge(execution: Execution::Lineage.clean_lazy_relationship_hash(record[:execution]))
  end
  validate_normalized_hash!(record)
  new(snapshot_hash(record), lineage: lineage)
end

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



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

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

.validate_normalized!(record) ⇒ Object

Raises:

  • (TypeError)


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

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

  raise TypeError, "expected Julewire::Record"
end

.validate_normalized_hash!(record) ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

#[](key) ⇒ Object



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

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

#digObject



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

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

#eachObject



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#fetchObject



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

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

#hashObject



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

def hash = @data.hash

#inspectObject



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#serializable_dataObject



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

def serializable_data = @data

#to_hObject



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

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