Skip to content

Commit

Permalink
Merge pull request #2239 from herwinw/data_inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m committed Sep 29, 2024
2 parents 9496445 + 6e23ff6 commit cbaba03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def self.define(*members, &block)
end
end

define_method(:inspect) do
"#<data #{self.class}#{members.map { |member| " #{member}=#{public_send(member).inspect}" }.join(',')}>"
end
alias_method :to_s, :inspect

define_method(:to_h) { members.to_h { |member| [member, public_send(member)] } }

define_method(:with) do |**kwargs|
Expand Down
22 changes: 22 additions & 0 deletions test/natalie/data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
it 'transforms the data object into a hash' do
1.should == 1
end

it 'results in a readable representation' do
1.should == 1
end

it 'results in a readable representation' do
1.should == 1
end
end
end

Expand All @@ -16,4 +24,18 @@
data.to_h.should == { amount: 42, unit: 'km' }
end
end

describe 'Data#inspect' do
it 'results in a readable representation' do
data = DataSpecs::Measure.new(amount: 42, unit: 'km')
data.inspect.should == '#<data DataSpecs::Measure amount=42, unit="km">'
end
end

describe 'Data#to_s' do
it 'results in a readable representation' do
data = DataSpecs::Measure.new(amount: 42, unit: 'km')
data.to_s.should == '#<data DataSpecs::Measure amount=42, unit="km">'
end
end
end

0 comments on commit cbaba03

Please sign in to comment.