Sha256: 05b241782e0b325f2e7581518101882f014996e1822661bff3f1d68a242f2fee

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

# Extension.
require_relative '../ext/swift'
require_relative 'swift/adapter'
require_relative 'swift/attribute'
require_relative 'swift/db'
require_relative 'swift/header'
require_relative 'swift/scheme'
require_relative 'swift/type'

module Swift
  class << self
    def setup name, type, options = {}
      unless type.kind_of?(Class) && type < Swift::Adapter
        raise TypeError, "Expected +type+ Swift::Adapter subclass but got #{type.inspect}"
      end
      (@repositories ||= {})[name] = type.new(options)
    end

    def db name = nil, &block
      # I pilfered the logic from DM but I don't really understand what is/isn't thread safe.
      scopes     = (Thread.current[:swift_db] ||= [])
      repository = if name || scopes.empty?
        @repositories[name || :default] or raise "Unknown db '#{name || :default}', did you forget to #setup?"
      else
        scopes.last
      end

      if block_given?
        begin
          scopes.push(repository)
          block.call(repository)
        ensure
          scopes.pop
        end
      end
      repository
    end

    def schema
      @schema ||= []
    end
  end
end # Swift

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
swift-0.7.1 lib/swift.rb
swift-0.7.0 lib/swift.rb
swift-0.6.1 lib/swift.rb
swift-0.6.0 lib/swift.rb
swift-0.5.1 lib/swift.rb
swift-0.5.0 lib/swift.rb