Class: Swift::Scheme

Inherits:
Object
  • Object
show all
Defined in:
lib/swift/scheme.rb,
lib/swift/migrations.rb,
lib/swift/validations.rb,
lib/swift/identity_map.rb

Overview

Errors

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Scheme

Returns a new instance of Scheme.

Examples:

User.new(
  name:       'Apple Arthurton',
  email:      '[email protected]',
  updated_at: Time.now
)

Parameters:

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

    Create resource and set attributes. {name: value}



24
25
26
27
# File 'lib/swift/scheme.rb', line 24

def initialize options = {}
  @tuple = scheme.header.new_tuple
  options.each{|k, v| send(:"#{k}=", v)}
end

Class Attribute Details

.headerSwift::Header

Attribute set.

Returns:



58
59
60
# File 'lib/swift/scheme.rb', line 58

def header
  @header
end

Instance Attribute Details

#tupleObject

Returns the value of attribute tuple.



14
15
16
# File 'lib/swift/scheme.rb', line 14

def tuple
  @tuple
end

Class Method Details

.all(conditions = '', *binds, &block) ⇒ Swift::Result

Select one or more.

Examples:

All.

User.all

All with conditions and binds.

User.all(':name = ? and :age > ?', 'Apple Arthurton', 32)

Block form iterator.

User.all(':age > ?', 32) do |user|
  puts user.name
end

Parameters:

  • conditions (String) (defaults to: '')

    Optional SQL ‘where’ fragment.

  • *binds (Object, ...)

    Optional bind values that accompany conditions SQL fragment.

  • &block (Proc)

    Optional ‘each’ iterator block.

Returns:



131
132
133
# File 'lib/swift/scheme.rb', line 131

def all conditions = '', *binds, &block
  Swift.db.all(self, conditions, *binds, &block)
end

.attribute(name, type, options = {}) ⇒ Object

Define a new attribute for this scheme.

See Also:

  • Attribute#new


76
77
78
79
# File 'lib/swift/scheme.rb', line 76

def attribute name, type, options = {}
  header.push(attribute = type.new(self, name, options))
  (class << self; self end).send(:define_method, name, lambda{ attribute })
end

.create(resources = {}) ⇒ Object

Create (insert).

Examples:

apple = User.create(
  name:       'Apple Arthurton',
  email:      '[email protected]',
  updated_at: Time.now
)

Parameters:

  • options (Hash, Array<Hash>)

    Create with attributes. {name: value}



99
100
101
# File 'lib/swift/scheme.rb', line 99

def create resources = {}
  Swift.db.create(self, resources)
end

.first(conditions = '', *binds, &block) ⇒ Swift::Scheme?

Select one.

Examples:

First.

User.first

First with conditions and binds.

User.first(':name = ? and :age > ?', 'Apple Arthurton', 32)

Block form iterator.

User.first(User, 'age > ?', 32) do |user|
  puts user.name
end

Parameters:

  • conditions (String) (defaults to: '')

    Optional SQL ‘where’ fragment.

  • *binds (Object, ...)

    Optional bind values that accompany conditions SQL fragment.

  • &block (Proc)

    Optional ‘each’ iterator block.

Returns:



150
151
152
# File 'lib/swift/scheme.rb', line 150

def first conditions = '', *binds, &block
  Swift.db.first(self, conditions, *binds, &block)
end

.get(keys) ⇒ Swift::Scheme?

Select by id(s).

Examples:

Single key.

User.get(id: 12)

Complex primary key.

UserAddress.get(user_id: 12, address_id: 15)

Parameters:

  • keys (Hash)

    Hash of id(s) {id_name: value}.

Returns:



112
113
114
# File 'lib/swift/scheme.rb', line 112

def get keys
  Swift.db.get(self, keys)
end

.inherited(klass) ⇒ Object



60
61
62
63
64
65
# File 'lib/swift/scheme.rb', line 60

def inherited klass
  klass.header = Header.new
  klass.header.push(*header) if header
  klass.store store          if store
  Swift.schema.push(klass)   if klass.name
end

.load(tuple) ⇒ Object

– TODO: Redefined method :(



39
40
41
42
43
# File 'lib/swift/identity_map.rb', line 39

def load tuple
  scheme       = allocate
  scheme.tuple = tuple
  scheme
end

.migrate!(db = Swift.db) ⇒ Object



7
8
9
# File 'lib/swift/migrations.rb', line 7

def self.migrate! db = Swift.db
  db.migrate! self
end

.migrations(&migrations) ⇒ Object



3
4
5
# File 'lib/swift/migrations.rb', line 3

def self.migrations &migrations
  (class << self; self end).send :define_method, :migrate!, lambda{|db = Swift.db| migrations.call(db) }
end

.store(name = nil) ⇒ Symbol

Define the store (table).

Parameters:

  • name (Symbol) (defaults to: nil)

    Storage name.

Returns:

  • (Symbol)


85
86
87
# File 'lib/swift/scheme.rb', line 85

def store name = nil
  name ? @store = name : @store
end

.validations(&validations) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/swift/validations.rb', line 11

def self.validations &validations
  define_method :validate do
    errors = Errors.new(self)
    instance_exec errors, &validations
    errors
  end
end

Instance Method Details

#destroy(resources = self) ⇒ Object

Examples:

apple = User.create(
  name:       'Apple Arthurton',
  email:      '[email protected]',
  updated_at: Time.now
)
apple.destroy


50
51
52
# File 'lib/swift/scheme.rb', line 50

def destroy resources = self
  Swift.db.destroy(scheme, resources)
end

#update(options = {}) ⇒ Object

Examples:

apple = User.create(
  name:       'Apple Arthurton',
  email:      '[email protected]',
  updated_at: Time.now
)
apple.update(name: 'Arthur Appleton')

Parameters:

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

    Update attributes. {name: value}



38
39
40
41
# File 'lib/swift/scheme.rb', line 38

def update options = {}
  options.each{|k, v| send(:"#{k}=", v)}
  Swift.db.update(scheme, self)
end

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/swift/validations.rb', line 23

def valid?
  validate.empty?
end

#validate(errors = Errors.new(self)) ⇒ Object



19
20
21
# File 'lib/swift/validations.rb', line 19

def validate errors = Errors.new(self)
  errors
end