mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Found a cleaner way to patch inspect
I tried to move on, I really did, but I already had these things in my head and they suddenly clicked together, so I thought it worth updating now.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DfcConnectorInspect
|
||||
def inspect
|
||||
# Show all other instance variables, which are a summary of the semanticPropertiesMap
|
||||
"#<#{self.class.name} #{instance_values.except('semanticPropertiesMap')}>"
|
||||
# Override PP method, which is used in pretty_inspect for rails console
|
||||
def pretty_print_instance_variables
|
||||
instance_variables.reject{ |var| var == :@semanticPropertiesMap }
|
||||
end
|
||||
end
|
||||
|
||||
# Include custom inspect on all connector classes
|
||||
# Include on all connector classes
|
||||
DataFoodConsortium::Connector.constants.each do |klass|
|
||||
DataFoodConsortium::Connector.const_get(klass).class_eval do
|
||||
include DfcConnectorInspect
|
||||
|
||||
@@ -2,24 +2,36 @@
|
||||
|
||||
require_relative "../spec_helper"
|
||||
|
||||
RSpec.describe "#inspect" do
|
||||
it "provides a clean-enough output for Address" do
|
||||
RSpec.describe "#pretty_inspect" do
|
||||
it "provides a clean output for Address" do
|
||||
subject = DataFoodConsortium::Connector::Address.new("some/id")
|
||||
|
||||
expect(subject.inspect).to eq <<~HEREDOC.squish
|
||||
#<DataFoodConsortium::Connector::Address
|
||||
{"semanticId"=>"some/id", "semanticType"=>"dfc-b:Address", "street"=>nil, "postalCode"=>nil,
|
||||
"city"=>nil, "country"=>nil, "latitude"=>nil, "longitude"=>nil, "region"=>nil}>
|
||||
expect(subject.pretty_inspect).to match <<~HEREDOC
|
||||
#<DataFoodConsortium::Connector::Address:.*
|
||||
@semanticId="some/id",
|
||||
@semanticType="dfc-b:Address",
|
||||
@street=nil,
|
||||
@postalCode=nil,
|
||||
@city=nil,
|
||||
@country=nil,
|
||||
@latitude=nil,
|
||||
@longitude=nil,
|
||||
@region=nil>
|
||||
HEREDOC
|
||||
end
|
||||
|
||||
it "provides a clean-enough output for OrderLine" do
|
||||
it "provides a clean output for OrderLine" do
|
||||
subject = DataFoodConsortium::Connector::OrderLine.new("some/id")
|
||||
|
||||
expect(subject.inspect).to eq <<~HEREDOC.squish
|
||||
#<DataFoodConsortium::Connector::OrderLine
|
||||
{"semanticId"=>"some/id", "semanticType"=>"dfc-b:OrderLine", "description"=>nil,
|
||||
"quantity"=>nil, "price"=>nil, "offer"=>nil, "order"=>nil}>
|
||||
expect(subject.pretty_inspect).to match <<~HEREDOC
|
||||
#<DataFoodConsortium::Connector::OrderLine:.*
|
||||
@semanticId="some/id",
|
||||
@semanticType="dfc-b:OrderLine",
|
||||
@description=nil,
|
||||
@quantity=nil,
|
||||
@price=nil,
|
||||
@offer=nil,
|
||||
@order=nil>
|
||||
HEREDOC
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user