diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-07-20 15:00:36 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-07-20 15:00:36 -0700 |
commit | 703571f7e0bebb3d2bd43a7af5976ad4565ac2cb (patch) | |
tree | 9bb33c0fc5c1fbd62b3457aa8c45726a59881a89 | |
parent | 692566733e4442947c6b6faa2f5a1b13940744d0 (diff) | |
download | rails-703571f7e0bebb3d2bd43a7af5976ad4565ac2cb.tar.gz rails-703571f7e0bebb3d2bd43a7af5976ad4565ac2cb.tar.bz2 rails-703571f7e0bebb3d2bd43a7af5976ad4565ac2cb.zip |
use Session.instance rather than Session.new for easier code
-rw-r--r-- | lib/arel/algebra/relations/relation.rb | 2 | ||||
-rw-r--r-- | lib/arel/session.rb | 32 | ||||
-rw-r--r-- | spec/algebra/unit/relations/relation_spec.rb | 12 | ||||
-rw-r--r-- | spec/algebra/unit/session/session_spec.rb | 12 |
4 files changed, 21 insertions, 37 deletions
diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb index 0332482066..34181beea2 100644 --- a/lib/arel/algebra/relations/relation.rb +++ b/lib/arel/algebra/relations/relation.rb @@ -3,7 +3,7 @@ module Arel attr_reader :count def session - Session.new + Session.instance end def call diff --git a/lib/arel/session.rb b/lib/arel/session.rb index e6da825b97..622ca090f8 100644 --- a/lib/arel/session.rb +++ b/lib/arel/session.rb @@ -1,30 +1,14 @@ module Arel class Session - class << self - attr_accessor :instance - alias_method :manufacture, :new + def self.instance + @instance || new + end - def start - if defined?(@started) && @started - yield - else - begin - @started = true - @instance = manufacture - singleton_class.class_eval do - undef :new - alias_method :new, :instance - end - yield - ensure - singleton_class.class_eval do - undef :new - alias_method :new, :manufacture - end - @started = false - end - end - end + def self.start + @instance ||= new + yield @instance + ensure + @instance = nil end module CRUD diff --git a/spec/algebra/unit/relations/relation_spec.rb b/spec/algebra/unit/relations/relation_spec.rb index a1d1793d92..be77c64b17 100644 --- a/spec/algebra/unit/relations/relation_spec.rb +++ b/spec/algebra/unit/relations/relation_spec.rb @@ -148,8 +148,8 @@ module Arel describe Relation::Operable::Writable do describe '#delete' do it 'manufactures a deletion relation' do - Session.start do - Session.new.should_receive(:delete).with(Deletion.new(@relation)) + Session.start do |s| + s.should_receive(:delete).with(Deletion.new(@relation)) @relation.delete end end @@ -157,9 +157,9 @@ module Arel describe '#insert' do it 'manufactures an insertion relation' do - Session.start do + Session.start do |s| record = { @relation[:name] => 'carl' } - Session.new.should_receive(:create).with(Insert.new(@relation, record)) + s.should_receive(:create).with(Insert.new(@relation, record)) @relation.insert(record) end end @@ -167,9 +167,9 @@ module Arel describe '#update' do it 'manufactures an update relation' do - Session.start do + Session.start do |s| assignments = { @relation[:name] => Value.new('bob', @relation) } - Session.new.should_receive(:update).with(Update.new(@relation, assignments)) + s.should_receive(:update).with(Update.new(@relation, assignments)) @relation.update(assignments) end end diff --git a/spec/algebra/unit/session/session_spec.rb b/spec/algebra/unit/session/session_spec.rb index 0553a140ec..421e3e3637 100644 --- a/spec/algebra/unit/session/session_spec.rb +++ b/spec/algebra/unit/session/session_spec.rb @@ -10,16 +10,16 @@ module Arel describe '::start' do describe '::instance' do it "it is a singleton within the started session" do - Session.start do - Session.new.should == Session.new + Session.start do |session| + Session.instance.should == session end end it "is a singleton across nested sessions" do - Session.start do - outside = Session.new - Session.start do - Session.new.should == outside + Session.start do |s1| + outside = Session.instance + Session.start do |s2| + Session.instance.should == outside end end end |