Skip to content

Commit

Permalink
Implement Relation#each_with_index
Browse files Browse the repository at this point in the history
  • Loading branch information
nathansobo committed Jun 3, 2011
1 parent 49b1402 commit 6025e78
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source "http://www.rubygems.org"
gemspec

gem 'rspec', '2.5'
gem 'rr', '>= 1.0'
gem 'rr', '1.0'
gem 'pg'
gem 'differ'
gem 'machinist'
2 changes: 1 addition & 1 deletion lib/prequel/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Record < Tuple
class << self
delegate :all, :update, :dataset, :count, :[], :to_update_sql, :to_sql, :get_column, :first, :empty?, :find,
:clear, :where, :where_any, :join, :join_through, :left_join, :project, :group_by, :order_by, :limit,
:offset, :tables, :synthetic_columns, :wire_representation, :to => :relation
:offset, :tables, :synthetic_columns, :wire_representation, :each, :each_with_index, :to => :relation

def table
relation
Expand Down
2 changes: 1 addition & 1 deletion lib/prequel/relations/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Relations
class Relation
extend EqualityDerivation

delegate :to_sql, :dataset, :all, :first, :count, :size, :each, :empty?, :to => :query
delegate :to_sql, :dataset, :all, :first, :count, :size, :each, :each_with_index, :empty?, :to => :query

def query(parent=nil)
query_class.new(self, parent).build
Expand Down
6 changes: 6 additions & 0 deletions lib/prequel/sql/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def each
end
end

def each_with_index
dataset.each_with_index do |field_values, index|
yield tuple_builder.build_tuple(field_values), index
end
end

def first
r = dataset
r.empty? ? nil : tuple_builder.build_tuple(r.first)
Expand Down
4 changes: 2 additions & 2 deletions prequel.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', '>= 3.0.4')
s.add_dependency('sequel', '>= 3.20.0')

s.add_development_dependency 'rspec', '>= 2.5'
s.add_development_dependency 'rr', '>= 1.0'
s.add_development_dependency 'rspec', '2.5'
s.add_development_dependency 'rr', '1.0'
s.add_development_dependency 'pg'
s.add_development_dependency 'differ'
s.add_development_dependency 'machinist'
Expand Down
17 changes: 17 additions & 0 deletions spec/prequel/relations/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ class Post < Record
end
end

describe "#each_with_index" do
it "iterates over records in the relation with an index" do
DB[:blogs] << { :id => 1, :title => "Blog 1" }
DB[:blogs] << { :id => 2, :title => "Blog 2" }

records = []
indices = []
Blog.order_by(:id).each_with_index do |record, index|
records.push(record)
indices.push(index)
end

records.should == [Blog.find(1), Blog.find(2)]
indices.should == [0, 1]
end
end

describe "#find" do
before do
DB[:blogs] << { :id => 1, :title => "Blog 1" }
Expand Down

0 comments on commit 6025e78

Please sign in to comment.