Class: Swift::Scheme

Inherits:
Object
  • Object
show all
Defined in:
lib/swift/validations.rb,
lib/swift/scheme.rb,
lib/swift/migrations.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.



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

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

Class Attribute Details

.headerObject

Returns the value of attribute header.



21
22
23
# File 'lib/swift/scheme.rb', line 21

def header
  @header
end

Instance Attribute Details

#tupleObject

Returns the value of attribute tuple.



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

def tuple
  @tuple
end

Class Method Details

.all(conditions = '', *binds, &block) ⇒ Object



52
53
54
# File 'lib/swift/scheme.rb', line 52

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

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



35
36
37
38
# File 'lib/swift/scheme.rb', line 35

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(options = {}) ⇒ Object



44
45
46
# File 'lib/swift/scheme.rb', line 44

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

.first(conditions = '', *binds, &block) ⇒ Object



56
57
58
# File 'lib/swift/scheme.rb', line 56

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

.get(keys) ⇒ Object



48
49
50
# File 'lib/swift/scheme.rb', line 48

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

.inherited(klass) ⇒ Object



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

def inherited klass
  klass.header = Header.new
  klass.header.push(*header) if header
  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) ⇒ Object



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

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

#destroyObject



16
17
18
# File 'lib/swift/scheme.rb', line 16

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

#update(options = {}) ⇒ Object



11
12
13
14
# File 'lib/swift/scheme.rb', line 11

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