aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/sessions/session.rb
blob: becf23b8b6a28293d3494e56d19dcb07b14cdf29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'singleton'

module Arel
  class Session    
    class << self
      attr_accessor :instance
      alias_method :manufacture, :new
      
      def start
        if @started
          yield
        else
          begin
            @started = true
            @instance = manufacture
            metaclass.send :alias_method, :new, :instance
            yield
          ensure
            metaclass.send :alias_method, :new, :manufacture
            @started = false
          end
        end
      end
    end
    
    module CRUD
      def create(insert)
        insert.call(insert.engine.connection)
      end
      
      def read(select)
        @read ||= Hash.new do |hash, select|
          hash[select] = select.call(select.engine.connection)
        end
        @read[select]
      end
      
      def update(update)
        update.call(update.engine.connection)
      end
      
      def delete(delete)
        delete.call(delete.engine.connection)
      end
    end
    include CRUD
    
    module Transactions
    end
    include Transactions
    
    module UnitOfWork
    end
    include UnitOfWork
  end
end