From 4c43dac54e649f579a96505e0038cfb6ea52ef1e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 6 Aug 2010 08:58:58 -0700 Subject: unfactoring more module inclusion --- lib/arel/algebra/relations/relation.rb | 97 ++++++++++++++-------------- spec/algebra/unit/relations/relation_spec.rb | 2 +- 2 files changed, 48 insertions(+), 51 deletions(-) diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb index de48aaf04e..e2f9f12866 100644 --- a/lib/arel/algebra/relations/relation.rb +++ b/lib/arel/algebra/relations/relation.rb @@ -102,75 +102,72 @@ module Arel session.read(self).each { |e| yield e } end - module Operable - def join(other_relation = nil, join_class = InnerJoin) - case other_relation - when String - StringJoin.new(self, other_relation) - when Relation - JoinOperation.new(join_class, self, other_relation) - else - self - end + def join(other_relation = nil, join_class = InnerJoin) + case other_relation + when String + StringJoin.new(self, other_relation) + when Relation + JoinOperation.new(join_class, self, other_relation) + else + self end + end - def outer_join(other_relation = nil) - join(other_relation, OuterJoin) - end + def outer_join(other_relation = nil) + join(other_relation, OuterJoin) + end - %w{ + %w{ having group order project - }.each do |op| - class_eval <<-OPERATION, __FILE__, __LINE__ + }.each do |op| + class_eval <<-OPERATION, __FILE__, __LINE__ def #{op}(*args) args.all? { |x| x.blank? } ? self : #{op.capitalize}.new(self, args) end - OPERATION - end + OPERATION + end - def where clause = nil - clause ? Where.new(self, Array(clause)) : self - end + def where clause = nil + clause ? Where.new(self, Array(clause)) : self + end - def skip thing = nil - thing ? Skip.new(self, thing) : self - end + def skip thing = nil + thing ? Skip.new(self, thing) : self + end - def take count - Take.new self, count - end + def take count + Take.new self, count + end - def from thing - From.new self, thing - end + def from thing + From.new self, thing + end - def lock(locking = true) - Lock.new(self, locking) - end + def lock(locking = true) + Lock.new(self, locking) + end - def alias - Alias.new(self) - end + def alias + Alias.new(self) + end - def insert(record) - session.create Insert.new(self, record) - end + def insert(record) + session.create Insert.new(self, record) + end - def update(assignments) - session.update Update.new(self, assignments) - end + def update(assignments) + session.update Update.new(self, assignments) + end - def delete - session.delete Deletion.new(self) - end + def delete + session.delete Deletion.new(self) + end - JoinOperation = Struct.new(:join_class, :relation1, :relation2) do - def on(*predicates) - join_class.new(relation1, relation2, *predicates) - end + JoinOperation = Struct.new(:join_class, :relation1, :relation2) do + def on(*predicates) + join_class.new(relation1, relation2, *predicates) end end - include Operable def [](index) attributes[index] diff --git a/spec/algebra/unit/relations/relation_spec.rb b/spec/algebra/unit/relations/relation_spec.rb index 07a7dcb843..d08c38acd5 100644 --- a/spec/algebra/unit/relations/relation_spec.rb +++ b/spec/algebra/unit/relations/relation_spec.rb @@ -23,7 +23,7 @@ module Arel end end - describe Relation::Operable do + describe 'Relation::Operable' do describe 'joins' do before do @predicate = @relation[:id].eq(@relation[:id]) -- cgit v1.2.3