Class: PaperTrailDiff::RecordReference

Inherits:
Object
  • Object
show all
Defined in:
lib/paper_trail_diff/snapshot.rb,
sig/generated/paper_trail_diff/snapshot.rbs

Overview

Immutable identity shared by snapshots and stable-record changes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, id:) ⇒ RecordReference

: (type: String, id: untyped) -> void

Parameters:

  • type: (String)
  • id: (Object)


11
12
13
14
15
# File 'lib/paper_trail_diff/snapshot.rb', line 11

def initialize(type:, id:)
  @type = Support.immutable_copy(type.to_s)
  @id = Support.immutable_copy(id)
  freeze
end

Instance Attribute Details

#idObject (readonly)

Signature:

  • untyped

Returns:

  • (Object)


8
9
10
# File 'lib/paper_trail_diff/snapshot.rb', line 8

def id
  @id
end

#typeString (readonly)

Signature:

  • String

Returns:

  • (String)


7
8
9
# File 'lib/paper_trail_diff/snapshot.rb', line 7

def type
  @type
end

Instance Method Details

#[](key) ⇒ Object

: (reference_key) -> untyped

Parameters:

  • (reference_key)

Returns:

  • (Object)


18
19
20
21
22
23
# File 'lib/paper_trail_diff/snapshot.rb', line 18

def [](key)
  case key.to_s
  when 'type' then type
  when 'id' then id
  end
end

#fetch(key) ⇒ Object

: (reference_key) -> untyped

Parameters:

  • (reference_key)

Returns:

  • (Object)


26
27
28
29
30
31
# File 'lib/paper_trail_diff/snapshot.rb', line 26

def fetch(key)
  value = self[key]
  return value if %w[type id].include?(key.to_s)

  raise KeyError, "key not found: #{key.inspect}"
end

#to_hHash[Symbol, untyped]

: () -> Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


34
35
36
# File 'lib/paper_trail_diff/snapshot.rb', line 34

def to_h
  { type: type, id: id }
end