-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add inspection tree builder for ActiveSupport::OrderedOptions (#202)
I believe this fixes #199 and, I suspect, fixes #163. See #199 (comment) for more background. You can reproduce errors like those raised in the aforementioned issues by checking out 26e1f6c and running `bundle exec rspec spec/unit/active_support/object_inspection_spec.rb`. The solution I opted for here was to create a hash-like inspector for `ActiveSupport::OrderedOptions` that takes precedence over the default `CustomObject` inspector, which in turn [takes precedence over](https://github.com/mcmire/super_diff/blob/fb6718a2b60bc8135424295cc069a4b984983f77/lib/super_diff/object_inspection/inspection_tree_builders/defaults.rb#L5-L7) the `Hash` inspector. The `OrderedOptions` inspection tree is basically a copy-paste of the one for `HashWithIndifferentAccess` – let me know if I should try to DRY those up.
- Loading branch information
Showing
6 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
lib/super_diff/active_support/object_inspection/inspection_tree_builders/ordered_options.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module SuperDiff | ||
module ActiveSupport | ||
module ObjectInspection | ||
module InspectionTreeBuilders | ||
class OrderedOptions < SuperDiff::ObjectInspection::InspectionTreeBuilders::Hash | ||
def self.applies_to?(value) | ||
value.is_a?(::ActiveSupport::OrderedOptions) | ||
end | ||
|
||
def call | ||
SuperDiff::ObjectInspection::InspectionTree.new do | ||
as_lines_when_rendering_to_lines(collection_bookend: :open) do | ||
add_text "#<OrderedOptions {" | ||
end | ||
|
||
when_rendering_to_string { add_text " " } | ||
|
||
nested do |ordered_options| | ||
insert_hash_inspection_of(ordered_options.to_hash) | ||
end | ||
|
||
when_rendering_to_string { add_text " " } | ||
|
||
as_lines_when_rendering_to_lines(collection_bookend: :close) do | ||
add_text "}>" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe SuperDiff, type: :unit do | ||
describe ".inspect_object", | ||
"for ActiveSupport objects", | ||
active_support: true do | ||
context "given an ActiveSupport::OrderedOptions object" do | ||
context "given as_lines: false" do | ||
it "returns an inspected version of the object" do | ||
string = | ||
described_class.inspect_object( | ||
::ActiveSupport::OrderedOptions[name: "Bob", age: 42], | ||
as_lines: false | ||
) | ||
expect(string).to eq(%(#<OrderedOptions { name: "Bob", age: 42 }>)) | ||
end | ||
end | ||
|
||
context "given as_lines: true" do | ||
it "returns an inspected version of the object as multiple Lines" do | ||
tiered_lines = | ||
described_class.inspect_object( | ||
::ActiveSupport::OrderedOptions[name: "Bob", age: 42], | ||
as_lines: true, | ||
type: :delete, | ||
indentation_level: 1 | ||
) | ||
expect(tiered_lines).to match( | ||
[ | ||
an_object_having_attributes( | ||
type: :delete, | ||
indentation_level: 1, | ||
value: "#<OrderedOptions {", | ||
collection_bookend: :open | ||
), | ||
an_object_having_attributes( | ||
type: :delete, | ||
indentation_level: 2, | ||
prefix: "name: ", | ||
value: "\"Bob\"", | ||
add_comma: true | ||
), | ||
an_object_having_attributes( | ||
type: :delete, | ||
indentation_level: 2, | ||
prefix: "age: ", | ||
value: "42", | ||
add_comma: false | ||
), | ||
an_object_having_attributes( | ||
type: :delete, | ||
indentation_level: 1, | ||
value: "}>", | ||
collection_bookend: :close | ||
) | ||
] | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
8fdca7b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️ thanks !