Class: PaperTrailDiff::TimelineBuilder
- Inherits:
-
Object
- Object
- PaperTrailDiff::TimelineBuilder
- Defined in:
- lib/paper_trail_diff/timeline_builder.rb,
sig/generated/paper_trail_diff/timeline_builder.rbs
Overview
Selects and compares a chronological slice of a record's version history.
Instance Method Summary collapse
-
#analyze ⇒ Analysis
: () -> Analysis.
-
#build ⇒ Array[Step]
: () -> Array.
-
#build_steps(plan) ⇒ Array[Step]
: (RootVersionPlan) -> Array.
-
#compare_history(plan) ⇒ [ Array[Step], RecordSnapshot?, RecordSnapshot? ]
: (RootVersionPlan) -> [Array[Step], RecordSnapshot?, RecordSnapshot?].
-
#empty_history ⇒ [ Array[Step], nil, nil ]
: () -> [Array[Step], nil, nil].
-
#initialize(record, from:, to:, snapshotter:, within: nil, versions: nil, version_scope: nil, plan: nil, live_endpoint: nil) ⇒ TimelineBuilder
constructor
: (untyped, from: untyped, to: untyped, snapshotter: untyped, ?within: untyped, ?versions: Array?, ?version_scope: untyped, ?plan: RootVersionPlan?, ?live_endpoint: untyped) -> void.
-
#selected_versions ⇒ RootVersionPlan
: () -> RootVersionPlan.
Constructor Details
#initialize(record, from:, to:, snapshotter:, within: nil, versions: nil, version_scope: nil, plan: nil, live_endpoint: nil) ⇒ TimelineBuilder
: (untyped, from: untyped, to: untyped, snapshotter: untyped, ?within: untyped, ?versions: Array?, ?version_scope: untyped, ?plan: RootVersionPlan?, ?live_endpoint: untyped) -> void
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/paper_trail_diff/timeline_builder.rb', line 8 def initialize( # rubocop:disable Metrics/ParameterLists record, from:, to:, snapshotter:, within: nil, versions: nil, version_scope: nil, plan: nil, live_endpoint: nil ) @record = record @range = TimelineRange.new( record, from: from, to: to, within: within, versions: versions, version_scope: version_scope, plan: plan, live_endpoint: live_endpoint ) @snapshotter = snapshotter end |
Instance Method Details
#analyze ⇒ Analysis
: () -> Analysis
28 29 30 31 32 33 34 35 36 |
# File 'lib/paper_trail_diff/timeline_builder.rb', line 28 def analyze steps, first_snapshot, last_snapshot = compare_history(selected_versions) Analysis.new( diff: Engine.compare(first_snapshot, last_snapshot), timeline: steps, from_snapshot: first_snapshot, to_snapshot: last_snapshot ) end |
#build ⇒ Array[Step]
: () -> Array
22 23 24 25 |
# File 'lib/paper_trail_diff/timeline_builder.rb', line 22 def build steps, = compare_history(selected_versions) steps end |
#build_steps(plan) ⇒ Array[Step]
: (RootVersionPlan) -> Array
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/paper_trail_diff/timeline_builder.rb', line 55 def build_steps(plan) captured_at = Time.now.utc if plan.closing_record plan.steps.map do |from_endpoint, to_endpoint| Step.new( from_version: from_endpoint, to_version: to_endpoint, captured_at: captured_at, diff: Engine.compare(@snapshotter.call(from_endpoint), @snapshotter.call(to_endpoint)) ) end.freeze end |
#compare_history(plan) ⇒ [ Array[Step], RecordSnapshot?, RecordSnapshot? ]
: (RootVersionPlan) -> [Array[Step], RecordSnapshot?, RecordSnapshot?]
45 46 47 48 49 50 51 52 |
# File 'lib/paper_trail_diff/timeline_builder.rb', line 45 def compare_history(plan) return empty_history if plan.empty? versions = plan.versions @snapshotter.prepare(@record, versions) if @snapshotter.respond_to?(:prepare) steps = build_steps(plan) [steps, @snapshotter.call(versions.first), @snapshotter.call(plan.final_endpoint)] end |
#empty_history ⇒ [ Array[Step], nil, nil ]
: () -> [Array[Step], nil, nil]
68 69 70 71 |
# File 'lib/paper_trail_diff/timeline_builder.rb', line 68 def empty_history steps = [] #: Array[Step] [steps.freeze, nil, nil] end |
#selected_versions ⇒ RootVersionPlan
: () -> RootVersionPlan
74 75 76 |
# File 'lib/paper_trail_diff/timeline_builder.rb', line 74 def selected_versions @range.select_plan end |