
Collection#[] with range 0...0 does not behave like Array
Reported by Ashley Moran | July 7th, 2009 @ 01:49 AM | in 0.10.0
The first of the following examples fails, with
+options[:limit]+ must be greater than or equal to 1, but was
0
, whereas the Collection#to_a version behaves as
expected:
require 'rubygems'
USE_DM_0_9 = false
if USE_DM_0_9
DM_GEMS_VERSION = "0.9.11"
DO_GEMS_VERSION = "0.9.12"
else
DM_GEMS_VERSION = "0.10.0"
DO_GEMS_VERSION = "0.10.0"
end
gem "data_objects", DO_GEMS_VERSION
gem "do_sqlite3", DO_GEMS_VERSION # If using another database, replace this
gem "dm-core", DM_GEMS_VERSION
gem "dm-aggregates", DM_GEMS_VERSION
gem "dm-migrations", DM_GEMS_VERSION
gem "dm-timestamps", DM_GEMS_VERSION
gem "dm-types", DM_GEMS_VERSION
gem "dm-validations", DM_GEMS_VERSION
gem "dm-serializer", DM_GEMS_VERSION
require "data_objects"
require "dm-core"
require "dm-aggregates"
require "dm-migrations"
require "dm-timestamps"
require "dm-types"
require "dm-validations"
require "dm-serializer"
require 'spec'
SQLITE_FILE = File.join(`pwd`.chomp, "test.db")
DataMapper.setup(:default, "sqlite3:#{SQLITE_FILE}")
DataMapper.setup(:reloaded, "sqlite3:#{SQLITE_FILE}")
class Farm
include DataMapper::Resource
property :id, Serial
has n, :cows, :order => [:cow_id]
end
class Cow
include DataMapper::Resource
property :id, Serial
property :cow_id, Integer
property :name, String
# Comment out the following line to make the spec pass
validates_is_unique :name, :scope => [:farm]
belongs_to :farm
def initialize(options)
p "MOO in init"
super(options)
end
end
module IdentityMapHelper
def reload(object)
object.class.get(object.id)
end
def with_db_reconnect(&blk)
original_repository = DataMapper::Repository.context.pop
repository(:reloaded, &blk)
DataMapper::Repository.context << original_repository
end
end
Spec::Runner.configure do |config|
include IdentityMapHelper
config.before(:each) do
Farm.auto_migrate!
Cow.auto_migrate!
end
config.before(:each) do
DataMapper::Repository.context << repository(:default)
end
config.after(:each) do
DataMapper::Repository.context.pop
end
end
describe "Accessing a subset of a collection using #[]" do
before(:each) do
@bluebell_farm = Farm.new
@cow_1 = Cow.new(:cow_id => 1, :name => "Daisy")
@cow_2 = Cow.new(:cow_id => 2, :name => "Florence")
@cow_3 = Cow.new(:cow_id => 3, :name => "Tastyburger")
@cow_4 = Cow.new(:cow_id => 4, :name => "Juicysteak")
@bluebell_farm.cows << @cow_1 << @cow_2 << @cow_3 << @cow_4
end
it "should let you use an inclusive range from 0...0" do
@bluebell_farm.cows[0...0].should == [ ]
end
it "should let you use an inclusive range from 0...0 like it does for Array#to_a" do
@bluebell_farm.cows.to_a[0...0].should == [ ]
end
end
Comments and changes to this ticket
-
Dan Kubb (dkubb) July 8th, 2009 @ 05:54 PM
- State changed from new to accepted
- Assigned user set to Dan Kubb (dkubb)
-
Dan Kubb (dkubb) July 9th, 2009 @ 03:10 AM
- State changed from accepted to resolved
(from [7f3ec791bb9d047254c8990566886db48bcc3036]) Allow Collection#[] to act the same as Array#[] with 0...0
- Returns empty collection
[#956 state:resolved] http://github.com/datamapper/dm-core/commit/7f3ec791bb9d047254c8990...
-
Dan Kubb (dkubb) July 9th, 2009 @ 03:12 AM
BTW, thanks for tickets that help Collection act more like Array. Ideally I would like Collection to act identically to an Array in terms of visible behavior.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
People watching this ticket
Referenced by
-
956 Collection#[] with range 0...0 does not behave like Array [#956 state:resolved] http://github.com/datamapper/dm-co...