Class: RecordDiff::Matcher
- Inherits:
-
Object
- Object
- RecordDiff::Matcher
- Extended by:
- Forwardable
- Defined in:
- lib/record_diff/matcher.rb
Overview
Handles matching results.
Instance Attribute Summary collapse
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#after_grouped ⇒ Object
readonly
Returns the value of attribute after_grouped.
-
#before ⇒ Object
readonly
Returns the value of attribute before.
-
#before_grouped ⇒ Object
readonly
Returns the value of attribute before_grouped.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#results ⇒ RecordDiff::ResultSet
readonly
-
the results of the diff operation.
-
Class Method Summary collapse
Instance Method Summary collapse
-
#generate_compares(before, after) ⇒ Object
rubocop:disable MethodLength.
- #generate_result(key, before_ary, after_ary) ⇒ Array<RecordDiff::Results::AbstractResult>
- #generate_result_obj(key, before, after) ⇒ Object
- #generate_results ⇒ Object
- #group_records ⇒ Object
-
#initialize(before:, after:, options: {}) ⇒ Matcher
constructor
A new instance of Matcher.
- #key_for_record(record) ⇒ Object
- #process ⇒ Object
Constructor Details
#initialize(before:, after:, options: {}) ⇒ Matcher
Returns a new instance of Matcher.
26 27 28 29 30 31 |
# File 'lib/record_diff/matcher.rb', line 26 def initialize(before:, after:, options: {}) @options = MatcherOptions.new() @before = before @after = after process end |
Instance Attribute Details
#after ⇒ Object (readonly)
Returns the value of attribute after.
10 11 12 |
# File 'lib/record_diff/matcher.rb', line 10 def after @after end |
#after_grouped ⇒ Object (readonly)
Returns the value of attribute after_grouped.
10 11 12 |
# File 'lib/record_diff/matcher.rb', line 10 def after_grouped @after_grouped end |
#before ⇒ Object (readonly)
Returns the value of attribute before.
10 11 12 |
# File 'lib/record_diff/matcher.rb', line 10 def before @before end |
#before_grouped ⇒ Object (readonly)
Returns the value of attribute before_grouped.
10 11 12 |
# File 'lib/record_diff/matcher.rb', line 10 def before_grouped @before_grouped end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/record_diff/matcher.rb', line 13 def @options end |
#results ⇒ RecordDiff::ResultSet (readonly)
Returns - the results of the diff operation.
12 13 14 |
# File 'lib/record_diff/matcher.rb', line 12 def results @results end |
Class Method Details
.diff_hash(before:, after:) ⇒ Object
20 21 22 23 24 |
# File 'lib/record_diff/matcher.rb', line 20 def self.diff_hash(before:, after:) new before: before, after: after, options: { before_id: :first, after_id: :first, before_transform: :second, after_transform: :second } end |
Instance Method Details
#generate_compares(before, after) ⇒ Object
rubocop:disable MethodLength
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/record_diff/matcher.rb', line 104 def generate_compares(before, after) # rubocop:disable MethodLength begin before_compare = before ? before_transform.call(before) : nil rescue StandardError => e before_compare = e end begin after_compare = after ? after_transform.call(after) : nil rescue StandardError => e after_compare = e end [before_compare, after_compare] end |
#generate_result(key, before_ary, after_ary) ⇒ Array<RecordDiff::Results::AbstractResult>
60 61 62 63 64 65 66 67 |
# File 'lib/record_diff/matcher.rb', line 60 def generate_result(key, before_ary, after_ary) raise "Multiple records to compare with same Key #{key}" if before_ary.count > 1 || after_ary.count > 1 before_obj = before_ary[0] after_obj = after_ary[0] [generate_result_obj(key, before_obj, after_obj)] end |
#generate_result_obj(key, before, after) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/record_diff/matcher.rb', line 70 def generate_result_obj(key, before, after) # rubocop:disable MethodLength before_compare, after_compare = generate_compares before, after if before_compare.is_a?(StandardError) || after_compare.is_a?(StandardError) return Results::ErroredResult.new id: key, before: before, before_compare: before_compare, after: after, after_compare: after_compare end if before_compare.nil? return Results::AddedResult.new id: key, after: after, after_compare: after_compare end if after_compare.nil? return Results::DroppedResult.new id: key, before: before, before_compare: before_compare end if before_compare == after_compare return Results::UnchangedResult.new( id: key, before: before, before_compare: before_compare, after: after, after_compare: after_compare ) end Results::ChangedResult.new( id: key, before: before, before_compare: before_compare, after: after, after_compare: after_compare ) end |
#generate_results ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/record_diff/matcher.rb', line 48 def generate_results keys = (before_grouped.keys + after_grouped.keys).uniq result_ary = keys.map do |key| generate_result key, before_grouped[key] || [], after_grouped[key] || [] end result_ary.flatten! @results = ResultSet.new(result_ary) end |
#group_records ⇒ Object
43 44 45 46 |
# File 'lib/record_diff/matcher.rb', line 43 def group_records @before_grouped = before.group_by { |r| before_id.call r } @after_grouped = after.group_by { |r| after_id.call r } end |
#key_for_record(record) ⇒ Object
39 40 41 |
# File 'lib/record_diff/matcher.rb', line 39 def key_for_record(record) id_method.call(record) end |
#process ⇒ Object
33 34 35 36 37 |
# File 'lib/record_diff/matcher.rb', line 33 def process group_records generate_results self end |