beerdb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.markdown +3 -0
- data/Manifest.txt +14 -0
- data/README.markdown +23 -0
- data/Rakefile +25 -0
- data/bin/beerdb +7 -0
- data/lib/beerdb/cli/opts.rb +56 -0
- data/lib/beerdb/cli/runner.rb +124 -0
- data/lib/beerdb/models/beer.rb +12 -0
- data/lib/beerdb/models/brewery.rb +13 -0
- data/lib/beerdb/models/country.rb +26 -0
- data/lib/beerdb/models/prop.rb +12 -0
- data/lib/beerdb/schema.rb +54 -0
- data/lib/beerdb/version.rb +4 -0
- data/lib/beerdb.rb +48 -0
- metadata +123 -0
data/History.markdown
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
History.markdown
|
2
|
+
Manifest.txt
|
3
|
+
README.markdown
|
4
|
+
Rakefile
|
5
|
+
bin/beerdb
|
6
|
+
lib/beerdb.rb
|
7
|
+
lib/beerdb/cli/opts.rb
|
8
|
+
lib/beerdb/cli/runner.rb
|
9
|
+
lib/beerdb/models/beer.rb
|
10
|
+
lib/beerdb/models/brewery.rb
|
11
|
+
lib/beerdb/models/country.rb
|
12
|
+
lib/beerdb/models/prop.rb
|
13
|
+
lib/beerdb/schema.rb
|
14
|
+
lib/beerdb/version.rb
|
data/README.markdown
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# beerdb
|
2
|
+
|
3
|
+
beer.db Command Line Tool in Ruby
|
4
|
+
|
5
|
+
* [geraldb.github.com/beer.db](http://geraldb.github.com/beer.db)
|
6
|
+
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
TBD
|
11
|
+
|
12
|
+
|
13
|
+
## Install
|
14
|
+
|
15
|
+
Just install the gem:
|
16
|
+
|
17
|
+
$ gem install beerdb
|
18
|
+
|
19
|
+
|
20
|
+
## License
|
21
|
+
|
22
|
+
The `beerdb` scripts are dedicated to the public domain.
|
23
|
+
Use it as you please with no restrictions whatsoever.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'hoe'
|
2
|
+
require './lib/beerdb/version.rb'
|
3
|
+
|
4
|
+
Hoe.spec 'beerdb' do
|
5
|
+
|
6
|
+
self.version = BeerDB::VERSION
|
7
|
+
|
8
|
+
self.summary = 'beerdb - beer.db command line tool'
|
9
|
+
self.description = summary
|
10
|
+
|
11
|
+
self.urls = ['http://geraldb.github.com/beer.db']
|
12
|
+
|
13
|
+
self.author = 'Gerald Bauer'
|
14
|
+
self.email = 'tobedone@googlegroups.com'
|
15
|
+
|
16
|
+
# switch extension to .markdown for gihub formatting
|
17
|
+
self.readme_file = 'README.markdown'
|
18
|
+
self.history_file = 'History.markdown'
|
19
|
+
|
20
|
+
self.extra_deps = [
|
21
|
+
['activerecord', '~> 3.2'] # NB: will include activesupport,etc.
|
22
|
+
### ['sqlite3', '~> 1.3'] # NB: install on your own; remove dependency
|
23
|
+
]
|
24
|
+
|
25
|
+
end
|
data/bin/beerdb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module BeerDB
|
2
|
+
|
3
|
+
class Opts
|
4
|
+
|
5
|
+
def create=(boolean)
|
6
|
+
@create = boolean
|
7
|
+
end
|
8
|
+
|
9
|
+
def create?
|
10
|
+
return false if @create.nil? # default create flag is false
|
11
|
+
@create == true
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def delete=(boolean)
|
16
|
+
@delete = boolean
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete?
|
20
|
+
return false if @delete.nil? # default create flag is false
|
21
|
+
@delete == true
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
# use loader? (that is, built-in seed data)
|
26
|
+
def load=(boolean)
|
27
|
+
@load = boolean
|
28
|
+
end
|
29
|
+
|
30
|
+
def load?
|
31
|
+
return false if @load.nil? # default create flag is false
|
32
|
+
@load == true
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def output_path=(value)
|
37
|
+
@output_path = value
|
38
|
+
end
|
39
|
+
|
40
|
+
def output_path
|
41
|
+
@output_path || '.'
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def data_path=(value)
|
46
|
+
@data_path = value
|
47
|
+
end
|
48
|
+
|
49
|
+
def data_path
|
50
|
+
@data_path || '.'
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
end # class Opts
|
55
|
+
|
56
|
+
end # module BeerDB
|
@@ -0,0 +1,124 @@
|
|
1
|
+
|
2
|
+
module BeerDB
|
3
|
+
|
4
|
+
class Runner
|
5
|
+
|
6
|
+
|
7
|
+
include BeerDB::Models
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@logger = Logger.new(STDOUT)
|
11
|
+
@logger.level = Logger::INFO
|
12
|
+
|
13
|
+
@opts = Opts.new
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :logger, :opts
|
17
|
+
|
18
|
+
|
19
|
+
def run( args )
|
20
|
+
opt=OptionParser.new do |cmd|
|
21
|
+
|
22
|
+
cmd.banner = "Usage: beerdb [options]"
|
23
|
+
|
24
|
+
## todo: change to different flag?? use -c/--config ???
|
25
|
+
cmd.on( '-c', '--create', 'Create DB Schema' ) { opts.create = true }
|
26
|
+
|
27
|
+
cmd.on( '--delete', 'Delete all records' ) { opts.delete = true }
|
28
|
+
|
29
|
+
cmd.on( '--load', 'Use Loader for Builtin Beer Data' ) { opts.load = true }
|
30
|
+
|
31
|
+
### todo: in future allow multiple search path??
|
32
|
+
cmd.on( '-i', '--include PATH', "Data Path (default is #{opts.data_path})" ) { |path| opts.data_path = path }
|
33
|
+
|
34
|
+
cmd.on( '-v', '--version', "Show version" ) do
|
35
|
+
puts BeerDB.banner
|
36
|
+
exit
|
37
|
+
end
|
38
|
+
|
39
|
+
cmd.on( "--verbose", "Show debug trace" ) do
|
40
|
+
logger.datetime_format = "%H:%H:%S"
|
41
|
+
logger.level = Logger::DEBUG
|
42
|
+
|
43
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
44
|
+
end
|
45
|
+
|
46
|
+
cmd.on_tail( "-h", "--help", "Show this message" ) do
|
47
|
+
puts <<EOS
|
48
|
+
|
49
|
+
beerdb - beer.db command line tool, version #{VERSION}
|
50
|
+
|
51
|
+
#{cmd.help}
|
52
|
+
|
53
|
+
Examples:
|
54
|
+
beerdb at # import austrian beers
|
55
|
+
beerdb -c # create database schema
|
56
|
+
|
57
|
+
More Examples:
|
58
|
+
beerdb # show stats (table counts, table props)
|
59
|
+
|
60
|
+
Further information:
|
61
|
+
http://geraldb.github.com/beer.db
|
62
|
+
|
63
|
+
EOS
|
64
|
+
exit
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
opt.parse!( args )
|
69
|
+
|
70
|
+
puts BeerDB.banner
|
71
|
+
|
72
|
+
puts "working directory: #{Dir.pwd}"
|
73
|
+
|
74
|
+
db_config = {
|
75
|
+
:adapter => 'sqlite3',
|
76
|
+
:database => "#{opts.output_path}/beer.db"
|
77
|
+
}
|
78
|
+
|
79
|
+
puts "Connecting to db using settings: "
|
80
|
+
pp db_config
|
81
|
+
|
82
|
+
ActiveRecord::Base.establish_connection( db_config )
|
83
|
+
|
84
|
+
if opts.create?
|
85
|
+
CreateDB.up
|
86
|
+
end
|
87
|
+
|
88
|
+
if opts.delete?
|
89
|
+
# tbd
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
args.each do |arg|
|
94
|
+
name = arg # File.basename( arg, '.*' )
|
95
|
+
|
96
|
+
# tbd
|
97
|
+
end
|
98
|
+
|
99
|
+
dump_stats
|
100
|
+
dump_props
|
101
|
+
|
102
|
+
puts 'Done.'
|
103
|
+
|
104
|
+
end # method run
|
105
|
+
|
106
|
+
|
107
|
+
def dump_stats
|
108
|
+
# todo: use %5d or similar to format string
|
109
|
+
puts "Stats:"
|
110
|
+
puts " #{Beer.count} beers"
|
111
|
+
puts " #{Brewery.count} breweries"
|
112
|
+
puts " #{Country.count} countries"
|
113
|
+
end
|
114
|
+
|
115
|
+
def dump_props
|
116
|
+
# todo: use %5 or similar to format string
|
117
|
+
puts "Props:"
|
118
|
+
Prop.order( 'created_at asc' ).all.each do |prop|
|
119
|
+
puts " #{prop.key} / #{prop.value} || #{prop.created_at}"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
end # class Runner
|
124
|
+
end # module BeerDB
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module BeerDB
|
2
|
+
module Models
|
3
|
+
|
4
|
+
|
5
|
+
class Country < ActiveRecord::Base
|
6
|
+
self.table_name = 'countries'
|
7
|
+
|
8
|
+
def self.create_from_ary!( countries )
|
9
|
+
countries.each do |values|
|
10
|
+
|
11
|
+
## key & title required
|
12
|
+
attr = {
|
13
|
+
:key => values[0],
|
14
|
+
:title => values[1],
|
15
|
+
:tag => values[2]
|
16
|
+
}
|
17
|
+
|
18
|
+
Country.create!( attr )
|
19
|
+
end # each country
|
20
|
+
end
|
21
|
+
|
22
|
+
end # class Country
|
23
|
+
|
24
|
+
|
25
|
+
end # module Models
|
26
|
+
end # module BeerDB
|
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
module BeerDB
|
3
|
+
|
4
|
+
class CreateDB
|
5
|
+
|
6
|
+
include BeerDB::Models
|
7
|
+
|
8
|
+
|
9
|
+
def self.up
|
10
|
+
|
11
|
+
ActiveRecord::Schema.define do
|
12
|
+
|
13
|
+
create_table :countries do |t|
|
14
|
+
t.string :title, :null => false
|
15
|
+
t.string :tag, :null => false # short three letter tag
|
16
|
+
t.string :key, :null => false
|
17
|
+
t.timestamps
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table :props do |t|
|
21
|
+
t.string :key, :null => false
|
22
|
+
t.string :value, :null => false
|
23
|
+
t.timestamps
|
24
|
+
end
|
25
|
+
|
26
|
+
create_table :beers do |t|
|
27
|
+
t.string :title, :null => false
|
28
|
+
t.string :key, :null => false # import/export key
|
29
|
+
t.string :synonyms # comma separated list of synonyms
|
30
|
+
t.boolean :bottle, :null => false, :default => false # Flaschenbier
|
31
|
+
t.boolean :draft, :null => false, :default => false # Fassbier
|
32
|
+
## todo: check seasonal is it proper english?
|
33
|
+
t.boolean :seasonal, :null => false, :default => false # all year or just eg. Festbier/Oktoberfest Special
|
34
|
+
## todo: add microbrew/brewpub flag?
|
35
|
+
#### t.boolean :brewpub, :null => false, :default => false
|
36
|
+
t.timestamps
|
37
|
+
end
|
38
|
+
|
39
|
+
create_table :breweries do |t|
|
40
|
+
t.string :title
|
41
|
+
t.references :country, :null => false
|
42
|
+
t.timestamps
|
43
|
+
end
|
44
|
+
|
45
|
+
end # block Schema.define
|
46
|
+
|
47
|
+
|
48
|
+
Prop.create!( key: 'db.schema.version', value: BeerDB::VERSION )
|
49
|
+
|
50
|
+
end # method up
|
51
|
+
|
52
|
+
end # class CreateDB
|
53
|
+
|
54
|
+
end # module BeerDB
|
data/lib/beerdb.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
###
|
2
|
+
# NB: for local testing run like:
|
3
|
+
#
|
4
|
+
# 1.9.x: ruby -Ilib lib/beerdb.rb
|
5
|
+
|
6
|
+
# core and stlibs
|
7
|
+
|
8
|
+
require 'yaml'
|
9
|
+
require 'pp'
|
10
|
+
require 'logger'
|
11
|
+
require 'optparse'
|
12
|
+
require 'fileutils'
|
13
|
+
require 'erb'
|
14
|
+
|
15
|
+
# rubygems
|
16
|
+
|
17
|
+
require 'active_record' ## todo: add sqlite3? etc.
|
18
|
+
|
19
|
+
|
20
|
+
# our own code
|
21
|
+
|
22
|
+
require 'beerdb/models/country'
|
23
|
+
require 'beerdb/models/beer'
|
24
|
+
require 'beerdb/models/brewery'
|
25
|
+
require 'beerdb/models/prop'
|
26
|
+
require 'beerdb/schema' # NB: requires beerdb/models (include BeerDB::Models)
|
27
|
+
require 'beerdb/version'
|
28
|
+
require 'beerdb/cli/opts'
|
29
|
+
require 'beerdb/cli/runner'
|
30
|
+
|
31
|
+
module BeerDB
|
32
|
+
|
33
|
+
def self.banner
|
34
|
+
"beerdb #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.root
|
38
|
+
"#{File.expand_path( File.dirname(File.dirname(__FILE__)) )}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.main
|
42
|
+
Runner.new.run(ARGV)
|
43
|
+
end
|
44
|
+
|
45
|
+
end # module BeerDB
|
46
|
+
|
47
|
+
|
48
|
+
BeerDB.main if __FILE__ == $0
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: beerdb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Gerald Bauer
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-17 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activerecord
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
- 2
|
32
|
+
version: "3.2"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rdoc
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 19
|
44
|
+
segments:
|
45
|
+
- 3
|
46
|
+
- 10
|
47
|
+
version: "3.10"
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: hoe
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 7
|
59
|
+
segments:
|
60
|
+
- 3
|
61
|
+
- 0
|
62
|
+
version: "3.0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
description: beerdb - beer.db command line tool
|
66
|
+
email: tobedone@googlegroups.com
|
67
|
+
executables:
|
68
|
+
- beerdb
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files:
|
72
|
+
- Manifest.txt
|
73
|
+
files:
|
74
|
+
- History.markdown
|
75
|
+
- Manifest.txt
|
76
|
+
- README.markdown
|
77
|
+
- Rakefile
|
78
|
+
- bin/beerdb
|
79
|
+
- lib/beerdb.rb
|
80
|
+
- lib/beerdb/cli/opts.rb
|
81
|
+
- lib/beerdb/cli/runner.rb
|
82
|
+
- lib/beerdb/models/beer.rb
|
83
|
+
- lib/beerdb/models/brewery.rb
|
84
|
+
- lib/beerdb/models/country.rb
|
85
|
+
- lib/beerdb/models/prop.rb
|
86
|
+
- lib/beerdb/schema.rb
|
87
|
+
- lib/beerdb/version.rb
|
88
|
+
homepage: http://geraldb.github.com/beer.db
|
89
|
+
licenses: []
|
90
|
+
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options:
|
93
|
+
- --main
|
94
|
+
- README.markdown
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
requirements: []
|
116
|
+
|
117
|
+
rubyforge_project: beerdb
|
118
|
+
rubygems_version: 1.8.24
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: beerdb - beer.db command line tool
|
122
|
+
test_files: []
|
123
|
+
|