Class: Swift::Record

Inherits:
Object
  • Object
show all
Extended by:
Migrations::ClassMethods
Defined in:
lib/swift/record.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

Methods included from Migrations::ClassMethods

migrate!, migrations

Constructor Details

#initialize(options = {}) ⇒ Record

Returns a new instance of Record.

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/record.rb', line 24

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

Class Attribute Details

.headerSwift::Header

Attribute set.

Returns:



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

def header
  @header
end

Instance Attribute Details

#tupleObject

Returns the value of attribute tuple.



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

def tuple
  @tuple
end

Class Method Details

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

Define a new attribute for this record.

See Also:

  • Attribute#new


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

def attribute name, type, options = {}
  header.push(attribute = type.new(self, name, options))
  define_singleton_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:

  • resources (Hash, Array<Hash>) (defaults to: {})

    Create with attributes. {name: value}



106
107
108
# File 'lib/swift/record.rb', line 106

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

.execute(statement = '', *binds) {|Swift::Result| ... } ⇒ Swift::Result

Execute a single statement.

Examples:

result = User.execute("select * from #{User} where #{User.name} = ?", 'apple')
sth.first # User object.

Parameters:

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

    Query statement.

  • binds (*Object)

    Bind values.

Yields:

Returns:



146
147
148
# File 'lib/swift/record.rb', line 146

def execute statement = '', *binds
  Swift::Result.new(self, Swift.db.execute(statement, *binds))
end

.get(keys) ⇒ Swift::Record?

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:



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

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

.inherited(klass) ⇒ Object



60
61
62
63
64
65
# File 'lib/swift/record.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
  record       = allocate
  record.tuple = tuple
  record
end

.prepare(statement = '') ⇒ Swift::Statement

Prepare a statement for on or more executions.

Examples:

sth = User.prepare("select * from #{User} where #{User.name} = ?")
sth.execute('apple') #=> Result
sth.execute('benny') #=> Result

Parameters:

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

    Query statement.

Returns:



132
133
134
# File 'lib/swift/record.rb', line 132

def prepare statement = ''
  Swift.db.prepare(self, statement)
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/record.rb', line 85

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

.to_sString

Store (table) name.

Returns:

  • (String)


92
93
94
# File 'lib/swift/record.rb', line 92

def to_s
  store.to_s
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

#delete(resources = self) ⇒ Object

Examples:

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


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

def delete resources = self
  Swift.db.delete(record, 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/record.rb', line 38

def update options = {}
  options.each{|k, v| public_send(:"#{k}=", v)}
  Swift.db.update(record, 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