Skip to content

Commit 147825e

Browse files
committed
Add diffing / deep inspection for HashWithIndifferentAccess
1 parent e64e5ef commit 147825e

15 files changed

+469
-0
lines changed

lib/super_diff/rails.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
1+
module SuperDiff
2+
module Rails
3+
autoload :DiffFormatters, "super_diff/rails/diff_formatters"
4+
autoload :Differs, "super_diff/rails/differs"
5+
autoload :ObjectInspection, "super_diff/rails/object_inspection"
6+
autoload :OperationalSequencers, "super_diff/rails/operational_sequencers"
7+
autoload :OperationalSequences, "super_diff/rails/operational_sequences"
8+
end
9+
end
10+
11+
if defined?(SuperDiff::RSpec)
12+
SuperDiff::RSpec.configure do |config|
13+
config.add_extra_differ_class(
14+
SuperDiff::Rails::Differs::HashWithIndifferentAccess,
15+
)
16+
config.add_extra_operational_sequencer_class(
17+
SuperDiff::Rails::OperationalSequencers::HashWithIndifferentAccess,
18+
)
19+
config.add_extra_diff_formatter_class(
20+
SuperDiff::Rails::DiffFormatters::HashWithIndifferentAccess,
21+
)
22+
end
23+
end
24+
25+
SuperDiff::ObjectInspection.map.prepend(
26+
SuperDiff::Rails::ObjectInspection::MapExtension,
27+
)
28+
129
require "super_diff/active_record"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module SuperDiff
2+
module Rails
3+
module DiffFormatters
4+
autoload(
5+
:HashWithIndifferentAccess,
6+
"super_diff/rails/diff_formatters/hash_with_indifferent_access",
7+
)
8+
end
9+
end
10+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module SuperDiff
2+
module Rails
3+
module DiffFormatters
4+
class HashWithIndifferentAccess < SuperDiff::DiffFormatters::Base
5+
def self.applies_to?(operations)
6+
operations.is_a?(OperationSequences::HashWithIndifferentAccess)
7+
end
8+
9+
def call
10+
SuperDiff::DiffFormatters::Collection.call(
11+
open_token: "#<HashWithIndifferentAccess {",
12+
close_token: "}>",
13+
collection_prefix: collection_prefix,
14+
build_item_prefix: -> (operation) {
15+
key =
16+
if operation.respond_to?(:left_key)
17+
operation.left_key
18+
else
19+
operation.key
20+
end
21+
22+
if key.is_a?(Symbol)
23+
"#{key}: "
24+
else
25+
"#{key.inspect} => "
26+
end
27+
},
28+
operations: operations,
29+
indent_level: indent_level,
30+
add_comma: add_comma?,
31+
)
32+
end
33+
end
34+
end
35+
end
36+
end

lib/super_diff/rails/differs.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module SuperDiff
2+
module Rails
3+
module Differs
4+
autoload(
5+
:HashWithIndifferentAccess,
6+
"super_diff/rails/differs/hash_with_indifferent_access",
7+
)
8+
end
9+
end
10+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module SuperDiff
2+
module Rails
3+
module Differs
4+
class HashWithIndifferentAccess < SuperDiff::Differs::Hash
5+
def self.applies_to?(expected, actual)
6+
(
7+
expected.is_a?(::HashWithIndifferentAccess) &&
8+
actual.is_a?(::Hash)
9+
) ||
10+
(
11+
expected.is_a?(::Hash) &&
12+
actual.is_a?(::HashWithIndifferentAccess)
13+
)
14+
end
15+
16+
def call
17+
DiffFormatters::HashWithIndifferentAccess.call(
18+
operations,
19+
indent_level: indent_level,
20+
)
21+
end
22+
23+
private
24+
25+
def operations
26+
OperationalSequencers::HashWithIndifferentAccess.call(
27+
expected: expected,
28+
actual: actual,
29+
extra_operational_sequencer_classes: extra_operational_sequencer_classes,
30+
extra_diff_formatter_classes: extra_diff_formatter_classes,
31+
)
32+
end
33+
end
34+
end
35+
end
36+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module SuperDiff
2+
module Rails
3+
module ObjectInspection
4+
autoload :Inspectors, "super_diff/rails/object_inspection/inspectors"
5+
autoload :MapExtension, "super_diff/rails/object_inspection/map_extension"
6+
end
7+
end
8+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module SuperDiff
2+
module Rails
3+
module ObjectInspection
4+
module Inspectors
5+
autoload(
6+
:HashWithIndifferentAccess,
7+
"super_diff/rails/object_inspection/inspectors/hash_with_indifferent_access",
8+
)
9+
end
10+
end
11+
end
12+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module SuperDiff
2+
module Rails
3+
module ObjectInspection
4+
module Inspectors
5+
HashWithIndifferentAccess = SuperDiff::ObjectInspection::InspectionTree.new do
6+
add_text "#<HashWithIndifferentAccess {"
7+
8+
nested do |hash|
9+
insert_hash_inspection_of(hash)
10+
end
11+
12+
add_break " "
13+
add_text "}>"
14+
end
15+
end
16+
end
17+
end
18+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module SuperDiff
2+
module Rails
3+
module ObjectInspection
4+
module MapExtension
5+
def call(object)
6+
if object.is_a?(::HashWithIndifferentAccess)
7+
Inspectors::HashWithIndifferentAccess
8+
else
9+
super
10+
end
11+
end
12+
end
13+
end
14+
end
15+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module SuperDiff
2+
module Rails
3+
module OperationSequences
4+
autoload(
5+
:HashWithIndifferentAccess,
6+
"super_diff/rails/operation_sequences/hash_with_indifferent_access",
7+
)
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)