diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-19 19:27:48 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-19 19:27:48 -0700 |
commit | 9e5ee49ec55b9cb1c2b4444dee58f3dfaefc7c7e (patch) | |
tree | b6f762f065574a6394da1a4c70efb874802d6677 /lib | |
parent | 5a5501cde76bbba69bcea27d3d0efeaffa3e3bf5 (diff) | |
download | rails-9e5ee49ec55b9cb1c2b4444dee58f3dfaefc7c7e.tar.gz rails-9e5ee49ec55b9cb1c2b4444dee58f3dfaefc7c7e.tar.bz2 rails-9e5ee49ec55b9cb1c2b4444dee58f3dfaefc7c7e.zip |
some drying up of boiler plate initialization and equality code
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/extensions/class.rb | 26 | ||||
-rw-r--r-- | lib/arel/relations/operations/alias.rb | 6 | ||||
-rw-r--r-- | lib/arel/relations/operations/group.rb | 14 | ||||
-rw-r--r-- | lib/arel/relations/operations/join.rb | 10 | ||||
-rw-r--r-- | lib/arel/relations/operations/order.rb | 17 | ||||
-rw-r--r-- | lib/arel/relations/operations/project.rb | 14 | ||||
-rw-r--r-- | lib/arel/relations/operations/skip.rb | 13 | ||||
-rw-r--r-- | lib/arel/relations/operations/take.rb | 13 | ||||
-rw-r--r-- | lib/arel/relations/operations/where.rb | 13 |
9 files changed, 55 insertions, 71 deletions
diff --git a/lib/arel/extensions/class.rb b/lib/arel/extensions/class.rb index 09e6d86ed4..f37898e7d7 100644 --- a/lib/arel/extensions/class.rb +++ b/lib/arel/extensions/class.rb @@ -1,4 +1,30 @@ class Class + def attributes(*attrs) + @attributes = attrs + attr_reader *attrs + end + + def deriving(*methods) + methods.each { |m| derive m } + end + + def derive(method_name) + methods = { + :initialize => " + def #{method_name}(#{@attributes.join(',')}) + #{@attributes.collect { |a| "@#{a} = #{a}" }.join("\n")} + end + ", + :== => " + def ==(other) + #{name} === other && + #{@attributes.collect { |a| "@#{a} == other.#{a}" }.join(" &&\n")} + end + " + } + class_eval methods[method_name], __FILE__, __LINE__ + end + def hash_on(delegatee) define_method :eql? do |other| self == other diff --git a/lib/arel/relations/operations/alias.rb b/lib/arel/relations/operations/alias.rb index d14a51f67a..8ed33fc597 100644 --- a/lib/arel/relations/operations/alias.rb +++ b/lib/arel/relations/operations/alias.rb @@ -1,10 +1,8 @@ module Arel class Alias < Compound include Recursion::BaseCase + attributes :relation + deriving :initialize alias_method :==, :equal? - - def initialize(relation) - @relation = relation - end end end
\ No newline at end of file diff --git a/lib/arel/relations/operations/group.rb b/lib/arel/relations/operations/group.rb index bc3a7f3437..22af2734a6 100644 --- a/lib/arel/relations/operations/group.rb +++ b/lib/arel/relations/operations/group.rb @@ -1,15 +1,11 @@ module Arel class Group < Compound - attr_reader :groupings + attributes :relation, :groupings + deriving :== - def initialize(relation, *groupings) - @relation, @groupings = relation, groupings.collect { |g| g.bind(relation) } - end - - def ==(other) - Group === other and - relation == other.relation and - groupings == other.groupings + def initialize(relation, *groupings, &block) + @relation = relation + @groupings = (groupings + (block_given?? [yield(self)] : [])).collect { |g| g.bind(relation) } end def aggregation? diff --git a/lib/arel/relations/operations/join.rb b/lib/arel/relations/operations/join.rb index acad75c817..01fa6da255 100644 --- a/lib/arel/relations/operations/join.rb +++ b/lib/arel/relations/operations/join.rb @@ -1,6 +1,7 @@ module Arel class Join < Relation - attr_reader :join_sql, :relation1, :relation2, :predicates + attributes :join_sql, :relation1, :relation2, :predicates + deriving :== delegate :engine, :name, :to => :relation1 hash_on :relation1 @@ -45,13 +46,6 @@ module Arel def join? true end - - def ==(other) - Join === other and - predicates == other.predicates and - relation1 == other.relation1 and - relation2 == other.relation2 - end end class Relation diff --git a/lib/arel/relations/operations/order.rb b/lib/arel/relations/operations/order.rb index ebb4dc0668..82924806e2 100644 --- a/lib/arel/relations/operations/order.rb +++ b/lib/arel/relations/operations/order.rb @@ -1,19 +1,16 @@ module Arel class Order < Compound - attr_reader :orderings + attributes :relation, :orderings + deriving :== - def initialize(relation, *orderings) - @relation, @orderings = relation, orderings.collect { |o| o.bind(relation) } + def initialize(relation, *orderings, &block) + @relation = relation + @orderings = (orderings + (block_given?? [yield(self)] : [])).collect { |o| o.bind(relation) } end + # TESTME def orders - orderings + relation.orders - end - - def ==(other) - Order === other and - relation == other.relation and - orderings == other.orderings + (orderings + relation.orders).collect { |o| o.bind(self) } end end end
\ No newline at end of file diff --git a/lib/arel/relations/operations/project.rb b/lib/arel/relations/operations/project.rb index 0efc13bdb3..2be87fe694 100644 --- a/lib/arel/relations/operations/project.rb +++ b/lib/arel/relations/operations/project.rb @@ -1,9 +1,11 @@ module Arel class Project < Compound - attr_reader :projections + attributes :relation, :projections + deriving :== - def initialize(relation, *projections) - @relation, @projections = relation, projections + def initialize(relation, *projections, &block) + @relation = relation + @projections = (projections + (block_given?? [yield(self)] : [])).collect { |p| p.bind(relation) } end def attributes @@ -13,11 +15,5 @@ module Arel def aggregation? attributes.any?(&:aggregation?) end - - def ==(other) - Project === other and - relation == other.relation and - projections == other.projections - end end end
\ No newline at end of file diff --git a/lib/arel/relations/operations/skip.rb b/lib/arel/relations/operations/skip.rb index 01ac4c7204..ea5df21f53 100644 --- a/lib/arel/relations/operations/skip.rb +++ b/lib/arel/relations/operations/skip.rb @@ -1,15 +1,6 @@ module Arel class Skip < Compound - attr_reader :skipped - - def initialize(relation, skipped) - @relation, @skipped = relation, skipped - end - - def ==(other) - Skip === other and - relation == other.relation and - skipped == other.skipped - end + attributes :relation, :skipped + deriving :initialize, :== end end
\ No newline at end of file diff --git a/lib/arel/relations/operations/take.rb b/lib/arel/relations/operations/take.rb index 0a49891aee..095e430417 100644 --- a/lib/arel/relations/operations/take.rb +++ b/lib/arel/relations/operations/take.rb @@ -1,15 +1,6 @@ module Arel class Take < Compound - attr_reader :taken - - def initialize(relation, taken) - @relation, @taken = relation, taken - end - - def ==(other) - Take === other and - relation == other.relation and - taken == other.taken - end + attributes :relation, :taken + deriving :initialize, :== end end
\ No newline at end of file diff --git a/lib/arel/relations/operations/where.rb b/lib/arel/relations/operations/where.rb index ba34846c04..9acb8ae3c6 100644 --- a/lib/arel/relations/operations/where.rb +++ b/lib/arel/relations/operations/where.rb @@ -1,21 +1,16 @@ module Arel class Where < Compound - attr_reader :predicate + attributes :relation, :predicate + deriving :== - def initialize(relation, *predicates) - predicate = predicates.shift + def initialize(relation, *predicates, &block) + predicate = block_given?? yield(self) : predicates.shift @relation = predicates.empty?? relation : Where.new(relation, *predicates) @predicate = predicate.bind(@relation) end def wheres @wheres ||= (relation.wheres + [predicate]).collect { |p| p.bind(self) } - end - - def ==(other) - Where === other and - relation == other.relation and - predicate == other.predicate end end end
\ No newline at end of file |