Module: BeerDb

Defined in:
lib/beerdb/stats.rb,
lib/beerdb/models.rb,
lib/beerdb/reader.rb,
lib/beerdb/schema.rb,
lib/beerdb/deleter.rb,
lib/beerdb/version.rb,
lib/beerdb/reader_zip.rb,
lib/beerdb/models/beer.rb,
lib/beerdb/reader_file.rb,
lib/beerdb/models/brand.rb,
lib/beerdb/readers/beer.rb,
lib/beerdb/readers/quick.rb,
lib/beerdb/models/brewery.rb,
lib/beerdb/models/forward.rb,
lib/beerdb/readers/brewery.rb

Defined Under Namespace

Modules: Matcher, Model Classes: BeerReader, BreweryReader, CreateDb, Deleter, QuickReader, Reader, ReaderBase, Stats, ZipReader

Constant Summary collapse

MAJOR =

todo: namespace inside version or something - why? why not??

1
MINOR =
2
PATCH =
0
VERSION =
[MAJOR,MINOR,PATCH].join('.')
Models =

convenience alias (for better english ;-) )

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

Class Method Summary collapse

Class Method Details



13
14
15
# File 'lib/beerdb/version.rb', line 13

def self.banner
  "beerdb-models/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
end

.connect(config = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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

.createObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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_allObject

convenience helper for all-in-one create for tables



55
56
57
58
59
60
61
# File 'lib/beerdb/models.rb', line 55

def self.create_all
  LogDb.create
  ConfDb.create
  TagDb.create
  WorldDb.create
  BeerDb.create
end

.delete!Object

delete ALL records (use with care!)



88
89
90
91
# File 'lib/beerdb/models.rb', line 88

def self.delete!
  puts '*** deleting beer table records/data...'
  Deleter.new.run
end

.delete_all!(opts = {}) ⇒ Object

method delete!



93
94
95
# File 'lib/beerdb/models.rb', line 93

def self.delete_all!( opts={} )
  # to be done
end

.read(ary, include_path) ⇒ Object



64
65
66
67
68
69
# 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 = {}) ⇒ Object

load all builtins (using plain text reader); helper for convenience



82
83
84
# 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 = {}) ⇒ Object



71
72
73
74
# 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 = {}) ⇒ Object

todo/check - use a better (shorter) name ??



76
77
78
79
80
# 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

.rootObject



17
18
19
# File 'lib/beerdb/version.rb', line 17

def self.root
  "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
end

.setup_in_memory_dbObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 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

.tablesObject



97
98
99
# File 'lib/beerdb/models.rb', line 97

def self.tables
  Stats.new.tables
end

.versionObject



9
10
11
# File 'lib/beerdb/version.rb', line 9

def self.version
  VERSION
end