Skip to content

Commit

Permalink
add method to iterate on blocks of any size and works with each row
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosenarruzza committed Dec 14, 2020
1 parent df35929 commit fdf3a8a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rasti/db/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def each(&block)
all.each(&block)
end

def each_page(size:, &block)
dataset.each_page(size) do |page|
page.each do |row|
block.call collection_class.model.new(with_graph(row))
end
end
end

def graph(*relations)
build_query relations_graph: relations_graph.merge(relations: relations)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/minitest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class Minitest::Spec
let :db do
Sequel.connect(driver).tap do |db|

db.extension :pagination

db.create_table :users do
primary_key :id
String :name, null: false, unique: true
Expand Down
11 changes: 11 additions & 0 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@
users_query.detect(id: 3).must_equal User.new(id: 3, name: 'User 3')
end

it 'Each page' do
user_pages = []
users_query.each_page(size: 2) do | page |
user_pages << page
end
user_pages.size.must_equal 10
user_pages.each_with_index do | user_page, i |
user_page.must_equal User.new(id: i+1, name: "User #{i+1}")
end
end

it 'Where' do
users_query.where(id: 3).all.must_equal [User.new(id: 3, name: 'User 3')]
end
Expand Down

0 comments on commit fdf3a8a

Please sign in to comment.