Class: PaperTrailDiff::TraversalEntry
- Inherits:
-
Object
- Object
- PaperTrailDiff::TraversalEntry
- 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 =
%i[change included_state].freeze
- STATES =
%i[before after].freeze
Instance Attribute Summary collapse
- #association_kind ⇒ Symbol? readonly
- #association_path ⇒ traversal_association_path readonly
- #attribute ⇒ String? readonly
- #context ⇒ traversal_context readonly
- #kind ⇒ Symbol readonly
- #record_path ⇒ traversal_record_path readonly
- #state ⇒ traversal_state? readonly
- #value ⇒ Object readonly
Instance Method Summary collapse
-
#association ⇒ String?
: () -> String?.
-
#change? ⇒ Boolean
: () -> bool.
-
#immutable_names(names) ⇒ traversal_association_path
: (traversal_association_path) -> traversal_association_path.
-
#included_state? ⇒ Boolean
: () -> bool.
-
#initialize(kind:, context:, association_path: [], record_path: [], association_kind: nil, state: nil, attribute: nil, value: nil) ⇒ TraversalEntry
constructor
: (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.
-
#record ⇒ RecordReference?
: () -> RecordReference?.
-
#to_h ⇒ Hash[Symbol, untyped]
: () -> Hash[Symbol, untyped].
-
#validate!(candidate_context, candidate_state) ⇒ void
: (Symbol, Symbol?) -> void.
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
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_kind ⇒ Symbol? (readonly)
15 16 17 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 15 def association_kind @association_kind end |
#association_path ⇒ traversal_association_path (readonly)
13 14 15 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 13 def association_path @association_path end |
#attribute ⇒ String? (readonly)
17 18 19 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 17 def attribute @attribute end |
#context ⇒ traversal_context (readonly)
12 13 14 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 12 def context @context end |
#kind ⇒ Symbol (readonly)
11 12 13 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 11 def kind @kind end |
#record_path ⇒ traversal_record_path (readonly)
14 15 16 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 14 def record_path @record_path end |
#state ⇒ traversal_state? (readonly)
16 17 18 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 16 def state @state end |
#value ⇒ Object (readonly)
18 19 20 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 18 def value @value end |
Instance Method Details
#association ⇒ String?
: () -> String?
54 55 56 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 54 def association association_path.last end |
#change? ⇒ Boolean
: () -> bool
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
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
49 50 51 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 49 def included_state? context == :included_state end |
#record ⇒ RecordReference?
: () -> RecordReference?
59 60 61 |
# File 'lib/paper_trail_diff/traversal_entry.rb', line 59 def record record_path.last end |
#to_h ⇒ Hash[Symbol, untyped]
: () -> 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
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 |