module BeerDb

forward references

require first to resolve circular references

quick (all-in-one-file) reader

reads breweries n beers “mixed-up” in a single-file

Constants

MAJOR
MINOR
Models

convenience alias (for better english ;-) )

lets you use =>  include Models  (instead of include Model)
PATCH
VERSION

Public Class Methods

banner() click to toggle source
connect( config={} ) click to toggle source
# File lib/beerdb/models.rb, line 102
def self.connect( config={} )

  if config.empty?
    puts "ENV['DATBASE_URL'] - >#{ENV['DATABASE_URL']}<"

    db = URI.parse( ENV['DATABASE_URL'] || 'sqlite3:///beer.db' )

    if db.scheme == 'postgres'
      config = {
        adapter: 'postgresql',
        host: db.host,
        port: db.port,
        username: db.user,
        password: db.password,
        database: db.path[1..-1],
        encoding: 'utf8'
      }
    else # assume sqlite3
     config = {
       adapter: db.scheme, # sqlite3
       database: db.path[1..-1] # beer.db (Note: cut off leading /, thus 1..-1)
    }
    end
  end

  puts "Connecting to db using settings: "
  pp config
  ActiveRecord::Base.establish_connection( config )
  # ActiveRecord::Base.logger = Logger.new( STDOUT )


  ## if sqlite3 add (use) some pragmas for speedups
  if config[:adapter] == 'sqlite3'
    if config[:database] == ':memory:'
      ## do nothing for in memory database; no pragmas needed
      puts "sqlite3 - no pragmas; using in memory database"
    else
      puts "sqlite3 - pragmas for speedup"
      con = ActiveRecord::Base.connection
      con.execute( 'PRAGMA synchronous=OFF;' )
      con.execute( 'PRAGMA journal_mode=OFF;' )
      con.execute( 'PRAGMA temp_store=MEMORY;' )
    end
  end
end
create() click to toggle source
# File lib/beerdb/models.rb, line 39
def self.create
  CreateDb.new.up

  ### fix: make optional do NOT auto create here
  ### fix: use if defined? BeerDbNote or similar or/and check if table exist ??
  ###      or move to beerdb-note ??

  # CreateDbExtrasUsers.new.up
  # CreateDbExtrasBookmarks.new.up
  # CreateDbExtrasDrinks.new.up
  # CreateDbExtrasNotes.new.up

  ConfDb::Model::Prop.create!( key: 'db.schema.beer.version', value: VERSION )
end
create_all() click to toggle source

convenience helper for all-in-one create for tables

# File lib/beerdb/models.rb, line 55
def self.create_all
  LogDb.create
  ConfDb.create
  TagDb.create
  WorldDb.create
  BeerDb.create
end
delete!() click to toggle source

delete ALL records (use with care!)

# File lib/beerdb/models.rb, line 88
def self.delete!
  puts '*** deleting beer table records/data...'
  Deleter.new.run
end
delete_all!( opts={} ) click to toggle source
# File lib/beerdb/models.rb, line 93
def self.delete_all!( opts={} )
  # to be done
end
read( ary, include_path ) click to toggle source
# File lib/beerdb/models.rb, line 64
def self.read( ary, include_path )
  reader = Reader.new( include_path )
  ary.each do |name|
    reader.load( name )
  end
end
read_all( include_path, opts={} ) click to toggle source
# File lib/beerdb/models.rb, line 82
def self.read_all( include_path, opts={} )  # load all builtins (using plain text reader); helper for convenience
  read_setup( 'setups/all', include_path, opts )
end
read_setup( setup, include_path, opts={} ) click to toggle source
# File lib/beerdb/models.rb, line 71
def self.read_setup( setup, include_path, opts={} )
  reader = Reader.new( include_path, opts )
  reader.load_setup( setup )
end
read_setup_from_zip( zip_name, setup, include_path, opts={} ) click to toggle source
# File lib/beerdb/models.rb, line 76
def self.read_setup_from_zip( zip_name, setup, include_path, opts={} )  ## todo/check - use a better (shorter) name ??
  reader = ZipReader.new( zip_name, include_path, opts )
  reader.load_setup( setup )
  reader.close
end
root() click to toggle source
# File lib/beerdb/version.rb, line 17
def self.root
  "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
end
setup_in_memory_db() click to toggle source
# File lib/beerdb/models.rb, line 149
def self.setup_in_memory_db
  # Database Setup & Config

  config = {
    adapter:  'sqlite3',
    database: ':memory:'
  }

  pp config

  ActiveRecord::Base.logger = Logger.new( STDOUT )
  ## ActiveRecord::Base.colorize_logging = false  - no longer exists - check new api/config setting?

  ## Note: every connect will create a new empty in memory db
  ActiveRecord::Base.establish_connection( config )

  ## build schema
  BeerDb.create_all
end
tables() click to toggle source
# File lib/beerdb/models.rb, line 97
def self.tables
  Stats.new.tables
end
version() click to toggle source
# File lib/beerdb/version.rb, line 9
def self.version
  VERSION
end