Class: RStore::BaseDB

Inherits:
Object
  • Object
show all
Defined in:
lib/rstore/base_db.rb

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

+ (Hash{Symbol=>BaseDB}) db_classes (readonly)

A Hash holding subclasses of RStore::BaseDB

Examples:

class CompanyDB < RStore::BaseDB
  info #...
end

class MyDB < RStore::BaseDB
  info #...
end

RStore::BaseDB.db_classes
#=> {:company=>companyDB, :my=>MyDB}

Returns:

  • (Hash{Symbol=>BaseDB})

    All subclasses of RStore::BaseDB defined in the current namespace. Subclasses are added automatically via self.inherited.



23
24
25
# File 'lib/rstore/base_db.rb', line 23

def db_classes
  @db_classes
end

Class Method Details

+ connect {|db| ... }

Note:

To be called when defining a subclass of RStore::BaseDB

This method returns an undefined value.

Uses the connection info from info to connect to the database.

Examples:

class CompanyDB < RStore::BaseDB
  info(adapter: 'mysql',
       host: 'localhost',
       user: 'root',
       password: 'xxx')
end

class DataTable < RStore::BaseTable
  create do
    primary_key :id, :allow_null => false
    String      :col1
    Integer     :col2
    Float       :col3
  end
end

name = DataTable.name

#Either
DB = CompanyDB.connect
DB.drop_table(name)
DB.disconnect

#Or
CompanyDB.connect do |db|
  db.drop_table(name)
end

Yield Parameters:

  • db (Sequel::Database)

    The opened Sequel Database object, which is closed when the block exits.



103
104
105
# File 'lib/rstore/base_db.rb', line 103

def self.connect &block
  Sequel.connect(@connection_info, &block)
end

+ info(options)

Note:

To be called when defining a subclass of RStore::BaseDB

Define the database connection. Accepts the same one arity parameters as Sequel.connect

Examples:

# Using a connection string
class CompanyDB < RStore::BaseDB
  info('postgres://user:password@localhost/blog')
end

# Using an options hash
class CompanyDB < RStore::BaseDB
  info(adapter: 'mysql',
       host: 'localhost',
       user: 'root',
       password: 'xxx')
end

This method returns an undefined value.

Parameters:

  • connection_info (String, Hash)

    Either a connection string such as postgres://user:password@localhost/blog, or a Hash with the following options:

Options Hash (options):

  • :adapter (String)

    The SQL database used, such as mysql or postgres

  • :host (String)

    Example: 'localhost'

  • :user (String)
  • :password (String)
  • :database (String)

    The database name. You don't need to provide this option, as its value will be inferred from the class name.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rstore/base_db.rb', line 58

def self.info hash_or_string
  # self = CompanyDB

  class << self
    # self = #<Class:CompanyDB>
    attr_reader :connection_info
  end

  # Instance variables always belong to self.
  @connection_info = hash_or_string.is_a?(Hash) ? hash_or_string.merge(:database => self.name.to_s): hash_or_string
end

+ (Symbol) name

Returns The lower-case class name without the DB postfix

Examples:

class CompanyDB < RStore::BaseDB
  info('postgres://user:password@localhost/blog')
end

CompanyDB.name
#=> :company

Returns:

  • (Symbol)

    The lower-case class name without the DB postfix



117
118
119
# File 'lib/rstore/base_db.rb', line 117

def self.name
  super.gsub!(/DB/,'').downcase.to_sym
end