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,
lib/swift/fiber_connection_pool.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, FiberConnectionPool, Header, IdentityMap, Record, Result, RuntimeError, Statement
Class Method Summary collapse
-
.db(name = nil, &block) ⇒ Swift::Adapter
Fetch or scope a block to a specific DB by name.
-
.migrate!(name = nil) ⇒ Object
Migrations.
- .repositories ⇒ Object
-
.schema ⇒ Array<Swift::Schema>
List of known Swift::Schema classes.
- .scopes ⇒ Object
-
.setup(name, type = nil, options = {}) ⇒ Swift::Adapter
Setup a new DB connection.
-
.trace(io = $stdout, &block) ⇒ Object
Trace the command execution in currently scoped adapter.
Class Method Details
.db(name = nil, &block) ⇒ Swift::Adapter
Fetch or scope a block to a specific DB by name.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/swift.rb', line 97 def db name = nil, &block repository = name || scopes.size < 1 ? repositories[name ||= :default] : scopes.last repository or raise "Unknown db '#{name}', did you forget to #setup ?" 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 |
.repositories ⇒ Object
132 133 134 |
# File 'lib/swift.rb', line 132 def repositories @repositories ||= {} end |
.schema ⇒ Array<Swift::Schema>
List of known Swift::Schema classes.
Handy if you are brewing stuff like migrations and need a list of defined schema subclasses.
117 118 119 |
# File 'lib/swift.rb', line 117 def schema @schema ||= [] end |
.scopes ⇒ Object
137 138 139 |
# File 'lib/swift.rb', line 137 def scopes Thread.current[:swift_db] ||= [] end |
.setup(name, type = nil, 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
.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/swift.rb', line 70 def setup name, type = nil, = {} if block_given? repositories[name] = yield else unless type.kind_of?(Class) && type < Swift::Adapter raise TypeError, "Expected +type+ Swift::Adapter subclass but got #{type.inspect}" end repositories[name] = type.new() end end |