Skip to content

Commit

Permalink
extract duplicate code into many association
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran authored and jasondew committed Apr 16, 2012
1 parent 534648a commit d5907d3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 43 deletions.
1 change: 1 addition & 0 deletions lib/dynamoid/associations.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8 # encoding: utf-8
require 'dynamoid/associations/association' require 'dynamoid/associations/association'
require 'dynamoid/associations/single_association' require 'dynamoid/associations/single_association'
require 'dynamoid/associations/many_association'
require 'dynamoid/associations/has_many' require 'dynamoid/associations/has_many'
require 'dynamoid/associations/belongs_to' require 'dynamoid/associations/belongs_to'
require 'dynamoid/associations/has_one' require 'dynamoid/associations/has_one'
Expand Down
25 changes: 3 additions & 22 deletions lib/dynamoid/associations/has_and_belongs_to_many.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,28 +4,9 @@ module Dynamoid #:nodoc:
# The has and belongs to many association. # The has and belongs to many association.
module Associations module Associations
class HasAndBelongsToMany class HasAndBelongsToMany
include Dynamoid::Associations::Association include Association

include ManyAssociation
# Is this array equal to the association's records?
#
# @return [Boolean] true/false
#
# @since 0.2.0
def ==(other)
records == Array(other)
end

# Delegate methods we don't find directly to the records array.
#
# @since 0.2.0
def method_missing(method, *args)
if records.respond_to?(method)
records.send(method, *args)
else
super
end
end

private private


# Find the target association, always another :has_and_belongs_to_many association. Uses either options[:inverse_of] or the source class name # Find the target association, always another :has_and_belongs_to_many association. Uses either options[:inverse_of] or the source class name
Expand Down
23 changes: 2 additions & 21 deletions lib/dynamoid/associations/has_many.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,27 +4,8 @@ module Dynamoid #:nodoc:
# The has_many association. # The has_many association.
module Associations module Associations
class HasMany class HasMany
include Dynamoid::Associations::Association include Association

include ManyAssociation
# Is this array equal to the association's records?
#
# @return [Boolean] true/false
#
# @since 0.2.0
def ==(other)
records == Array(other)
end

# Delegate methods we don't find directly to the records array.
#
# @since 0.2.0
def method_missing(method, *args)
if records.respond_to?(method)
records.send(method, *args)
else
super
end
end


private private


Expand Down
28 changes: 28 additions & 0 deletions lib/dynamoid/associations/many_association.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,28 @@
# encoding: utf-8
module Dynamoid #:nodoc:

module Associations
module ManyAssociation

# Is this array equal to the association's records?
#
# @return [Boolean] true/false
#
# @since 0.2.0
def ==(other)
records == Array(other)
end

# Delegate methods we don't find directly to the records array.
#
# @since 0.2.0
def method_missing(method, *args)
if records.respond_to?(method)
records.send(method, *args)
else
super
end
end
end
end
end

0 comments on commit d5907d3

Please sign in to comment.