Skip to content

Commit

Permalink
feat: improve pp output for ActiveHash::Relation
Browse files Browse the repository at this point in the history
Previously the output might look like:

    #<ActiveHash::Relation:0x00007f4c2dc3ca28
     @all_records=
      [#<Layout:0x00007f4c2df4f198 @attributes={:id=>1, :name=>"1"}>,
       #<Layout:0x00007f4c2df4cfd8 @attributes={:id=>2, :name=>"2"}>,
       #<Layout:0x00007f4c2df4b9a8 @attributes={:id=>3, :name=>"3"}>,
       #<Layout:0x00007f4c2df7d9f8 @attributes={:id=>4, :name=>"4"}>],
     @conditions=#<ActiveHash::Relation::Conditions:0x00007f4c2dc3bd58 @conditions=[]>,
     @klass=Layout,
     @order_values=[]>

After this change the output looks like:

    [#<Layout:0x00007f2755785d60 @attributes={:id=>1, :name=>"1"}>,
     #<Layout:0x00007f2755784668 @attributes={:id=>2, :name=>"2"}>,
     #<Layout:0x00007f2755783a38 @attributes={:id=>3, :name=>"3"}>,
     #<Layout:0x00007f2755782db8 @attributes={:id=>4, :name=>"4"}>]

Closes active-hash#285
  • Loading branch information
flavorjones committed Aug 4, 2023
1 parent 4d08471 commit d23246d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/active_hash/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def where(conditions_hash = :chain)
spawn.where!(conditions_hash)
end

def pretty_print(pp)
pp.pp(entries.to_ary)
end

class WhereChain
attr_reader :relation

Expand Down
12 changes: 12 additions & 0 deletions spec/active_hash/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,16 @@
expect(klass.where(display: true).length).to eq(1)
end
end

describe "#pretty_print" do
it "prints the records" do
out = StringIO.new
PP.pp(subject, out)

expect(out.string.scan(/\bid\b/).length).to eq(2)
expect(out.string).to match(/\bCanada\b/)
expect(out.string).to match(/\bUS\b/)
expect(out.string).to_not match(/ActiveHash::Relation/)
end
end
end

0 comments on commit d23246d

Please sign in to comment.