Module: Swift

Defined in:
lib/swift.rb,
lib/swift/type.rb,
lib/swift/header.rb,
lib/swift/record.rb,
lib/swift/result.rb,
lib/swift/adapter.rb,
lib/swift/attribute.rb,
lib/swift/statement.rb,
lib/swift/synchrony.rb,
lib/swift/migrations.rb,
lib/swift/adapter/sql.rb,
lib/swift/validations.rb,
lib/swift/eventmachine.rb,
lib/swift/identity_map.rb,
lib/swift/adapter/mysql.rb,
lib/swift/adapter/sqlite3.rb,
lib/swift/adapter/postgres.rb

Overview

A rational rudimentary object relational mapper.

Synopsis

require 'swift'
require 'swift/migrations'

Swift.trace true # Debugging.
Swift.setup :default, Swift::DB::Postgres, db: 'swift'

class User < Swift::Record
  store     :users
  attribute :id,    Swift::Type::Integer, serial: true, key: true
  attribute :name,  Swift::Type::String
  attribute :email, Swift::Type::String
end # User

# Migrate it.
User.migrate!

# Create
User.create name: 'Apple Arthurton', email: '[email protected]' # => User

# Get by key.
user = User.get id: 1

# Alter attribute and update in one.
user.update name: 'Jimmy Arthurton'

# Alter attributes and update.
user.name = 'Apple Arthurton'
user.update

# Destroy
user.delete

See

  • README.rdoc has more usage examples.

  • API.rdoc is a public API overview.

Defined Under Namespace

Modules: Migrations, Type Classes: Adapter, Attribute, Error, Errors, Header, IdentityMap, Record, Result, RuntimeError, Statement

Class Method Summary collapse

Class Method Details

.db(name = nil, &block) ⇒ Swift::Adapter

Fetch or scope a block to a specific DB by name.

– I pilfered the logic from DM but I don’t really understand what is/isn’t thread safe.

Examples:

Swift.db :other do |other|
  # Inside this block all these are the same:
  # other
  # Swift.db
  # Swift.db :other

  other_users = User.prepare('select * from users where age > ?')
  other_users.execute(32)
end

Parameters:

  • name (Symbol) (defaults to: nil)

    Adapter name.

  • block (Proc)

    Scope this block to the named adapter instead of :default.

Returns:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/swift.rb', line 95

def db name = nil, &block
  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

.migrate!(name = nil) ⇒ Object

Migrations



47
48
49
# File 'lib/swift/migrations.rb', line 47

def self.migrate! name = nil
  schema.each {|record| record.migrate!(db(name))}
end

.schemaArray<Swift::Schema>

List of known Swift::Schema classes.

Handy if you are brewing stuff like migrations and need a list of defined schema subclasses.

Returns:

  • (Array<Swift::Schema>)


118
119
120
# File 'lib/swift.rb', line 118

def schema
  @schema ||= []
end

.scopesObject



126
127
128
# File 'lib/swift.rb', line 126

def scopes
  Thread.current[:swift_db] ||= []
end

.setup(name, type, options = {}) ⇒ Swift::Adapter

Setup a new DB connection.

You almost certainly want to setup a :default named adapter. The :default scope will be used for unscoped calls to Swift.db.

Examples:

Swift.setup :default, Swift::DB::Postgres, db: 'db1'
Swift.setup :other,   Swift::DB::Postgres, db: 'db2'

Parameters:

  • name (Symbol)

    Adapter name.

  • type (Swift::Adapter)

    Concrete adapter subclass. See Swift::DB

  • options (Hash) (defaults to: {})

    Connection options

Options Hash (options):

  • :db (String)

    Name.

  • :user (String) — default: *nix login user
  • :password (String) — default: ''
  • :host (String) — default: 'localhost'
  • :port (Integer) — default: DB default

Returns:

See Also:



70
71
72
73
74
75
# File 'lib/swift.rb', line 70

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

.trace(io = $stdout, &block) ⇒ Object



122
123
124
# File 'lib/swift.rb', line 122

def trace io = $stdout, &block
  Swift.db.trace(io, &block)
end