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
-
.db(name = nil, &block) ⇒ Swift::Adapter
Fetch or scope a block to a specific DB by name.
-
.migrate!(name = nil) ⇒ Object
Migrations.
-
.schema ⇒ Array<Swift::Schema>
List of known Swift::Schema classes.
- .scopes ⇒ Object
-
.setup(name, type, options = {}) ⇒ Swift::Adapter
Setup a new DB connection.
- .trace(io = $stdout, &block) ⇒ Object
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.
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 |
.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.
118 119 120 |
# File 'lib/swift.rb', line 118 def schema @schema ||= [] end |
.scopes ⇒ Object
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.
70 71 72 73 74 75 |
# File 'lib/swift.rb', line 70 def setup name, type, = {} 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 |
.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 |