Module: PaperTrailDiff::Endpoint
- Defined in:
- lib/paper_trail_diff/endpoint.rb,
sig/generated/paper_trail_diff/endpoint.rbs
Overview
Validates and describes explicit PaperTrail-version and live-record endpoints.
Class Method Summary collapse
- .identity(endpoint) ⇒ Object
- .model_class(version) ⇒ Object
- .record?(endpoint) ⇒ Boolean
- .reload_record(record) ⇒ Object
- .validate!(endpoint) ⇒ Object
-
.validate_order!(from_endpoint, to_endpoint) ⇒ Object
Two versions carry no visible cue about which is earlier, so transposing them is easy to do by accident and impossible to detect afterwards: the result is a valid inverse diff and carries no direction of its own.
- .validate_pair!(from_endpoint, to_endpoint) ⇒ Object
- .version?(endpoint) ⇒ Boolean
Instance Method Summary collapse
-
#self?.identity ⇒ Array[String]
: (untyped) -> Array.
-
#self?.model_class ⇒ Object
: (untyped) -> untyped.
-
#self?.record? ⇒ Boolean
: (untyped) -> bool.
-
#self?.reload_record ⇒ Object
: (untyped) -> untyped.
-
#self?.reversed_versions? ⇒ Boolean
: (untyped, untyped) -> bool.
-
#self?.validate! ⇒ void
: (untyped) -> void.
-
#self?.validate_order! ⇒ void
Two versions carry no visible cue about which is earlier, so transposing them is easy to do by accident and impossible to detect afterwards: the result is a valid inverse diff and carries no direction of its own.
-
#self?.validate_pair! ⇒ void
: (untyped, untyped) -> void.
-
#self?.validate_record! ⇒ void
: (untyped) -> void.
-
#self?.version? ⇒ Boolean
: (untyped) -> bool.
Class Method Details
.identity(endpoint) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/paper_trail_diff/endpoint.rb', line 66 def identity(endpoint) if version?(endpoint) [endpoint.item_type.to_s, endpoint.item_id.to_s] elsif record?(endpoint) [endpoint.class.base_class.name.to_s, endpoint.id.to_s] else raise InvalidEndpointError, 'expected a PaperTrail version or persisted record endpoint' end end |
.model_class(version) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/paper_trail_diff/endpoint.rb', line 77 def model_class(version) subtype = version.item_subtype if version.respond_to?(:item_subtype) model_type = subtype.to_s.empty? ? version.item_type.to_s : subtype.to_s Object.const_get(model_type) rescue NameError => e raise InvalidEndpointError, "cannot resolve endpoint model: #{model_type}", cause: e end |
.record?(endpoint) ⇒ Boolean
58 59 60 61 62 63 |
# File 'lib/paper_trail_diff/endpoint.rb', line 58 def record?(endpoint) !version?(endpoint) && endpoint.respond_to?(:persisted?) && endpoint.respond_to?(:destroyed?) && endpoint.class.respond_to?(:base_class) end |
.reload_record(record) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/paper_trail_diff/endpoint.rb', line 86 def reload_record(record) validate_record!(record) record.class.unscoped.find(record.id) rescue StandardError => e active_record = Object.const_get(:ActiveRecord) #: untyped record_not_found = active_record.const_get(:RecordNotFound) #: untyped raise unless e.is_a?(record_not_found) = 'current record endpoint could not be reloaded from the database' raise InvalidEndpointError, , cause: e end |
.validate!(endpoint) ⇒ Object
44 45 46 47 48 |
# File 'lib/paper_trail_diff/endpoint.rb', line 44 def validate!(endpoint) return if version?(endpoint) validate_record!(endpoint) end |
.validate_order!(from_endpoint, to_endpoint) ⇒ Object
Two versions carry no visible cue about which is earlier, so transposing
them is easy to do by accident and impossible to detect afterwards: the
result is a valid inverse diff and carries no direction of its own. It is
also redundant, because the two orders differ only in which side of each
change is from. A current-record endpoint is exempt: it is self-evidently
the live state, so putting it first is a deliberate reverse comparison.
27 28 29 30 31 32 33 |
# File 'lib/paper_trail_diff/endpoint.rb', line 27 def validate_order!(from_endpoint, to_endpoint) return unless reversed_versions?(from_endpoint, to_endpoint) raise ReversedEndpointsError, 'version endpoints must be given in chronological order; ' \ 'swap them to read the same difference in the other direction' end |
.validate_pair!(from_endpoint, to_endpoint) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/paper_trail_diff/endpoint.rb', line 10 def validate_pair!(from_endpoint, to_endpoint) validate!(from_endpoint) validate!(to_endpoint) unless identity(from_endpoint) == identity(to_endpoint) raise VersionMismatchError, 'endpoints must belong to the same PaperTrail item' end validate_order!(from_endpoint, to_endpoint) end |
.version?(endpoint) ⇒ Boolean
51 52 53 54 55 |
# File 'lib/paper_trail_diff/endpoint.rb', line 51 def version?(endpoint) endpoint.respond_to?(:reify) && endpoint.respond_to?(:item_type) && endpoint.respond_to?(:item_id) end |
Instance Method Details
#self?.identity ⇒ Array[String]
: (untyped) -> Array
31 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 31
def self?.identity: (untyped) -> Array[String]
|
#self?.model_class ⇒ Object
: (untyped) -> untyped
34 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 34
def self?.model_class: (untyped) -> untyped
|
#self?.record? ⇒ Boolean
: (untyped) -> bool
28 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 28
def self?.record?: (untyped) -> bool
|
#self?.reload_record ⇒ Object
: (untyped) -> untyped
37 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 37
def self?.reload_record: (untyped) -> untyped
|
#self?.reversed_versions? ⇒ Boolean
: (untyped, untyped) -> bool
19 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 19
def self?.reversed_versions?: (untyped, untyped) -> bool
|
#self?.validate! ⇒ void
This method returns an undefined value.
: (untyped) -> void
22 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 22
def self?.validate!: (untyped) -> void
|
#self?.validate_order! ⇒ void
This method returns an undefined value.
Two versions carry no visible cue about which is earlier, so transposing
them is easy to do by accident and impossible to detect afterwards: the
result is a valid inverse diff and carries no direction of its own. It is
also redundant, because the two orders differ only in which side of each
change is from. A current-record endpoint is exempt: it is self-evidently
the live state, so putting it first is a deliberate reverse comparison.
: (untyped, untyped) -> void
16 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 16
def self?.validate_order!: (untyped, untyped) -> void
|
#self?.validate_pair! ⇒ void
This method returns an undefined value.
: (untyped, untyped) -> void
7 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 7
def self?.validate_pair!: (untyped, untyped) -> void
|
#self?.validate_record! ⇒ void
This method returns an undefined value.
: (untyped) -> void
40 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 40
def self?.validate_record!: (untyped) -> void
|
#self?.version? ⇒ Boolean
: (untyped) -> bool
25 |
# File 'sig/generated/paper_trail_diff/endpoint.rbs', line 25
def self?.version?: (untyped) -> bool
|