diff options
Diffstat (limited to 'lib/arel/session.rb')
-rw-r--r-- | lib/arel/session.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/arel/session.rb b/lib/arel/session.rb new file mode 100644 index 0000000000..8b72fd1fe6 --- /dev/null +++ b/lib/arel/session.rb @@ -0,0 +1,47 @@ +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)[select] + end + + def update(update) + update.call(update.engine.connection) + end + + def delete(delete) + delete.call(delete.engine.connection) + end + end + include CRUD + end +end
\ No newline at end of file |