Class: PaperTrailDiff::TraversalEntry

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

Overview

One immutable location and value emitted while walking a Diff tree.

Constant Summary collapse

CONTEXTS =

Returns:

  • (Object)
%i[change included_state].freeze
STATES =

Returns:

  • (Object)
%i[before after].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, context:, association_path: [], record_path: [], association_kind: nil, state: nil, attribute: nil, value: nil) ⇒ TraversalEntry

: (kind: Symbol, context: traversal_context, ?association_path: traversal_association_path, ?record_path: traversal_record_path, ?association_kind: Symbol?, ?state: traversal_state?, ?attribute: String?, ?value: untyped) -> void

Parameters:

  • kind: (Symbol)
  • context: (traversal_context)
  • association_path: (traversal_association_path) (defaults to: [])
  • record_path: (traversal_record_path) (defaults to: [])
  • association_kind: (Symbol, nil) (defaults to: nil)
  • state: (traversal_state, nil) (defaults to: nil)
  • attribute: (String, nil) (defaults to: nil)
  • value: (Object) (defaults to: nil)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/paper_trail_diff/traversal_entry.rb', line 21

def initialize( # rubocop:disable Metrics/ParameterLists
  kind:,
  context:,
  association_path: [],
  record_path: [],
  association_kind: nil,
  state: nil,
  attribute: nil,
  value: nil
)
  validate!(context, state)
  @kind = kind
  @context = context
  @association_path = immutable_names(association_path)
  @record_path = Support.immutable_copy(record_path)
  @association_kind = association_kind
  @state = state
  @attribute = Support.immutable_copy(attribute&.to_s)
  @value = Support.immutable_copy(value)
  freeze
end

Instance Attribute Details

#association_kindSymbol? (readonly)

Signature:

  • Symbol?

Returns:

  • (Symbol, nil)


15
16
17
# File 'lib/paper_trail_diff/traversal_entry.rb', line 15

def association_kind
  @association_kind
end

#association_pathtraversal_association_path (readonly)

Signature:

  • traversal_association_path

Returns:

  • (traversal_association_path)


13
14
15
# File 'lib/paper_trail_diff/traversal_entry.rb', line 13

def association_path
  @association_path
end

#attributeString? (readonly)

Signature:

  • String?

Returns:

  • (String, nil)


17
18
19
# File 'lib/paper_trail_diff/traversal_entry.rb', line 17

def attribute
  @attribute
end

#contexttraversal_context (readonly)

Signature:

  • traversal_context

Returns:

  • (traversal_context)


12
13
14
# File 'lib/paper_trail_diff/traversal_entry.rb', line 12

def context
  @context
end

#kindSymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


11
12
13
# File 'lib/paper_trail_diff/traversal_entry.rb', line 11

def kind
  @kind
end

#record_pathtraversal_record_path (readonly)

Signature:

  • traversal_record_path

Returns:

  • (traversal_record_path)


14
15
16
# File 'lib/paper_trail_diff/traversal_entry.rb', line 14

def record_path
  @record_path
end

#statetraversal_state? (readonly)

Signature:

  • traversal_state?

Returns:

  • (traversal_state, nil)


16
17
18
# File 'lib/paper_trail_diff/traversal_entry.rb', line 16

def state
  @state
end

#valueObject (readonly)

Signature:

  • untyped

Returns:

  • (Object)


18
19
20
# File 'lib/paper_trail_diff/traversal_entry.rb', line 18

def value
  @value
end

Instance Method Details

#associationString?

: () -> String?

Returns:

  • (String, nil)


54
55
56
# File 'lib/paper_trail_diff/traversal_entry.rb', line 54

def association
  association_path.last
end

#change?Boolean

: () -> bool

Returns:

  • (Boolean)


44
45
46
# File 'lib/paper_trail_diff/traversal_entry.rb', line 44

def change?
  context == :change
end

#immutable_names(names) ⇒ traversal_association_path

: (traversal_association_path) -> traversal_association_path

Parameters:

  • (traversal_association_path)

Returns:

  • (traversal_association_path)


90
91
92
# File 'lib/paper_trail_diff/traversal_entry.rb', line 90

def immutable_names(names)
  names.map { |name| Support.immutable_copy(name.to_s) }.freeze
end

#included_state?Boolean

: () -> bool

Returns:

  • (Boolean)


49
50
51
# File 'lib/paper_trail_diff/traversal_entry.rb', line 49

def included_state?
  context == :included_state
end

#recordRecordReference?

: () -> RecordReference?

Returns:



59
60
61
# File 'lib/paper_trail_diff/traversal_entry.rb', line 59

def record
  record_path.last
end

#to_hHash[Symbol, untyped]

: () -> Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/paper_trail_diff/traversal_entry.rb', line 64

def to_h
  {
    kind: kind,
    context: context,
    association_path: Support.serialize(association_path),
    record_path: Support.serialize(record_path),
    association_kind: association_kind,
    state: state,
    attribute: attribute,
    value: Support.serialize(value)
  }
end

#validate!(candidate_context, candidate_state) ⇒ void

This method returns an undefined value.

: (Symbol, Symbol?) -> void

Parameters:

  • (Symbol)
  • (Symbol, nil)


80
81
82
83
84
85
86
87
# File 'lib/paper_trail_diff/traversal_entry.rb', line 80

def validate!(candidate_context, candidate_state)
  unless CONTEXTS.include?(candidate_context)
    raise ArgumentError, "unsupported traversal context: #{candidate_context.inspect}"
  end
  return if candidate_state.nil? || STATES.include?(candidate_state)

  raise ArgumentError, "unsupported traversal state: #{candidate_state.inspect}"
end