Permalink
Browse files

Moved n-1 functionality from has() into belongs_to

  • Loading branch information...
1 parent 6340ca2 commit 09e150960ce2a79802242c3879078b7f1935d6b3 @andykent andykent committed with Dan Kubb Apr 19, 2008
Showing with 91 additions and 100 deletions.
  1. +32 −32 lib/data_mapper/associations.rb
  2. +59 −68 spec/unit/associations_spec.rb
@@ -21,59 +21,59 @@ def n
end
#
- # A shorthand, clear syntax for defining resource relationships.
+ # A shorthand, clear syntax for defining one-to-one, one-to-many and many-to-many resource relationships.
#
- # Basic Usage Examples...
+ # Usage Examples...
#
- # * has 1..n, :friends # one_to_many :friends
- # * has 1, :friend # one_to_one, :friend
- # * has n..1, :friends # many_to_one, :friends
- # * has n..n, :friends # many_to_many, :friends
+ # * has 1, :friend # one_to_one, :friend
+ # * has n, :friends # one_to_many :friends
+ # * has 1..3, :friends # one_to_many :friends, :min => 1, :max => 3
+ # * has 3, :friends # one_to_many :friends, :min => 3, :max => 3
+ # * has 1, :friend, :class_name=>'User' # one_to_one :friend, :class_name => 'User'
+ # * has 3, :friends, :through=>:friendships # one_to_one :friend, :class_name => 'User'
#
- # Advanced Usage Examples...
- #
- # * has 1..3, :friends # one_to_many :friends, :min => 3, :max => 3
- # * has 1..2, :friends, :max=>5 # one_to_many :friends, :min => 2, :max => 5
- # * has 3, :friends # one_to_many :friends, :min => 3, :max => 3
- # * has 3..3, :friends # many_to_many :friends, :left=>{:min=>3, :max=>3}, :right=>{:min=>3, :max=>3}
- # * has 1, :friend, :class_name=>'User' # one_to_one :friend, :class_name => 'User'
- #
- # * <tt>contraints</tt> - constraints can be defined as either a fixed number, Infinity or a range
+ # * <tt>cardinality</tt> - can be defined as either a fixed number, Infinity or a range
+ # * <tt>name</tt> - name of the resource to associate with
+ # * <tt>options</tt> - A hash of additional options
#
def has(cardinality, name, options = {})
case cardinality
when Range
- left, right = cardinality.first, cardinality.last
- case 1
- when left #1..n or 1..2
- one_to_many(name, extract_min_max(right).merge(options))
- when right # n..1 or 2..1
- many_to_one(name, extract_min_max(left).merge(options))
- else # n..n or 2..2
- many_to_many(name, extract_min_max(cardinality).merge(options))
- end
+ min, max = cardinality.first, cardinality.last
+ # 1..n or 3..5
+ one_to_many(name, extract_min_max(cardinality).merge(options))
when 1
one_to_one(name, options)
- when Fixnum, Bignum, n # n or 2 - shorthand form of 1..n or 1..2
+ when Fixnum, Bignum, n # n or 2 - shorthand form of n..n or 2..2
one_to_many(name, extract_min_max(cardinality).merge(options))
end || raise(ArgumentError, "Cardinality #{cardinality.inspect} (#{cardinality.class}) not handled")
end
+ #
+ # A shorthand, clear syntax for defining many-to-one resource relationships.
+ #
+ # Usage Examples...
+ #
+ # * belongs_to :user # many_to_one, :friend
+ # * belongs_to :friend, :classname => 'User' # one_to_many :friends
+ #
+ # * <tt>name</tt> - name of the resource to associate with
+ # * <tt>options</tt> - A hash of additional options
+ #
+ def belongs_to(name, options={})
+ many_to_one(name, options)
+ end
+
private
- # A support method form converting numbers ranges or Infinity values into a {:min=>x, :max=>y} hash.
+ # A support method form converting Fixnum, Range or Infinity values into a {:min=>x, :max=>y} hash.
#
# * <tt>contraints</tt> - constraints can be defined as either a fixed number, Infinity or a range
def extract_min_max(contraints)
case contraints
when Range
- left = extract_min_max(contraints.first)
- right = extract_min_max(contraints.last)
- conditions = {}
- conditions.merge!(:left=>left) if left.any?
- conditions.merge!(:right=>right) if right.any?
- conditions
+ {:min=>contraints.first, :max=>contraints.last}
when Fixnum, Bignum
{:min=>contraints, :max=>contraints}
when n
@@ -21,7 +21,7 @@ class Manufacturer
it "should allow overwriting of the auto assigned min/max values with keys" do
Manufacturer.should_receive(:one_to_many).
- with(:vehicles, {:min=>2, :max=>10}).
+ with(:vehicles, {:min=>1, :max=>10}).
and_return(@relationship)
class Manufacturer
has 1..2, :vehicles, :max=>10
@@ -56,7 +56,7 @@ class Manufacturer
with(:vehicles,{}).
and_return(@relationship)
class Manufacturer
- has 1..n, :vehicles
+ has n, :vehicles
end
end
@@ -65,93 +65,84 @@ class Manufacturer
with(:vehicles,{:min=>4, :max=>4}).
and_return(@relationship)
class Manufacturer
- has 1..4, :vehicles
+ has 4, :vehicles
end
end
it "should create a one-to-many association with min/max constraints" do
- pending
- end
-
- it "should create a one-to-many association with options" do
Manufacturer.should_receive(:one_to_many).
- with(:vehicles,{:class_name => 'Car'}).
- and_return(@relationship)
- class Manufacturer
- has 1..n, :vehicles,
- :class_name => 'Car'
- end
- end
- end
-
- describe "many-to-one syntax" do
- it "should create a basic many-to-one association with no constraints" do
- Manufacturer.should_receive(:many_to_one).
- with(:vehicles,{}).
+ with(:vehicles,{:min=>2, :max=>4}).
and_return(@relationship)
class Manufacturer
- has n..1, :vehicles
- end
- end
-
- it "should create a many-to-one association with fixed constraint" do
- Manufacturer.should_receive(:many_to_one).
- with(:vehicles,{:min=>4, :max=>4}).
- and_return(@relationship)
- class Manufacturer
- has 4..1, :vehicles
+ has 2..4, :vehicles
end
end
- it "should create a many-to-one association with min/max constraints" do
- pending
- end
-
- it "should create a many-to-one association with options" do
- Manufacturer.should_receive(:many_to_one).
- with(:vehicles,{:class_name => 'Car'}).
+ it "should create a one-to-many association with options" do
+ Manufacturer.should_receive(:one_to_many).
+ with(:vehicles,{:min=>1, :max=>@n, :class_name => 'Car'}).
and_return(@relationship)
class Manufacturer
- has n..1, :vehicles,
+ has 1..n, :vehicles,
:class_name => 'Car'
end
end
end
- describe "many-to-many syntax" do
- it "should create a basic many-to-one association with no constraints" do
- Manufacturer.should_receive(:many_to_many).
- with(:vehicles,{}).
- and_return(@relationship)
- class Manufacturer
- has n..n, :vehicles
- end
- end
-
- it "should create a many-to-many association with fixed constraints" do
- Manufacturer.should_receive(:many_to_many).
- with(:vehicles, :left=>{:min=>4, :max=>4}, :right=>{:min=>4, :max=>4}).
- and_return(@relationship)
- class Manufacturer
- has 4..4, :vehicles
- end
- end
-
- it "should create a many-to-many association with min/max constraints" do
- pending
+ # describe "many-to-many syntax" do
+ # it "should create a basic many-to-one association with no constraints" do
+ # Manufacturer.should_receive(:many_to_many).
+ # with(:vehicles,{}).
+ # and_return(@relationship)
+ # class Manufacturer
+ # has n..n, :vehicles
+ # end
+ # end
+ #
+ # it "should create a many-to-many association with fixed constraints" do
+ # Manufacturer.should_receive(:many_to_many).
+ # with(:vehicles, :left=>{:min=>4, :max=>4}, :right=>{:min=>4, :max=>4}).
+ # and_return(@relationship)
+ # class Manufacturer
+ # has 4..4, :vehicles
+ # end
+ # end
+ #
+ # it "should create a many-to-many association with min/max constraints" do
+ # pending
+ # end
+ #
+ # it "should create a many-to-many association with options" do
+ # Manufacturer.should_receive(:many_to_many).
+ # with(:vehicles,{:class_name => 'Car'}).
+ # and_return(@relationship)
+ # class Manufacturer
+ # has n..n, :vehicles,
+ # :class_name => 'Car'
+ # end
+ # end
+ # end
+ end
+
+ describe ".belongs_to" do
+ it "should create a basic many-to-one association" do
+ Manufacturer.should_receive(:many_to_one).
+ with(:vehicle,{}).
+ and_return(@relationship)
+ class Manufacturer
+ belongs_to :vehicle
end
+ end
- it "should create a many-to-many association with options" do
- Manufacturer.should_receive(:many_to_many).
- with(:vehicles,{:class_name => 'Car'}).
- and_return(@relationship)
- class Manufacturer
- has n..n, :vehicles,
- :class_name => 'Car'
- end
+ it "should create a many-to-one association with options" do
+ Manufacturer.should_receive(:many_to_one).
+ with(:vehicle,{:class_name => 'Car'}).
+ and_return(@relationship)
+ class Manufacturer
+ belongs_to :vehicle,
+ :class_name => 'Car'
end
end
-
end
end

0 comments on commit 09e1509

Please sign in to comment.