beerdb 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -26,11 +26,11 @@ Hoe.spec 'beerdb' do
26
26
  # - logutils
27
27
  # - textutils
28
28
 
29
- ## 3rd party
30
- ['commander', '~> 4.1.3'] # remove? -- already included as dep in worlddb
29
+ ## 3rd party
30
+ ['gli', '>= 2.5.6']
31
31
  ]
32
32
 
33
- self.licenses = ['Public Domain']
33
+ self.licenses = ['Public Domain']
34
34
 
35
35
  self.spec_extras = {
36
36
  :required_ruby_version => '>= 1.9.2'
data/bin/beerdb CHANGED
File without changes
@@ -1,37 +1,45 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'commander/import'
3
+ require 'gli'
4
+
5
+ include GLI::App
6
+
4
7
 
5
8
  require 'logutils/db' # add support for logging to db
6
9
  require 'beerdb/cli/opts'
7
10
 
8
- LogUtils::Logger.root.level = :info # set logging level to info
9
11
 
10
12
 
11
- program :name, 'beerdb'
12
- program :version, BeerDb::VERSION
13
- program :description, "beer.db command line tool, version #{BeerDb::VERSION}"
13
+ program_desc 'beer.db command line tool'
14
+ version BeerDb::VERSION
14
15
 
15
16
 
16
- # default_command :help
17
- default_command :load
17
+ LogUtils::Logger.root.level = :info # set logging level to info
18
+ logger = LogUtils::Logger.root
18
19
 
19
- program :help_formatter, Commander::HelpFormatter::TerminalCompact
20
20
 
21
+ opts = BeerDb::Opts.new
21
22
 
22
- ## todo: find a better name e.g. change to settings? config? safe_opts? why? why not?
23
- myopts = BeerDb::Opts.new
24
23
 
25
24
  ### global option (required)
26
25
  ## todo: add check that path is valid?? possible?
27
26
 
28
- global_option '-i', '--include PATH', String, "Data path (default is #{myopts.data_path})"
29
- global_option '-d', '--dbpath PATH', String, "Database path (default is #{myopts.db_path})"
30
- global_option '-n', '--dbname NAME', String, "Database name (datault is #{myopts.db_name})"
27
+ desc 'Database path'
28
+ arg_name 'PATH'
29
+ default_value opts.db_path
30
+ flag [:d, :dbpath]
31
+
32
+ desc 'Database name'
33
+ arg_name 'NAME'
34
+ default_value opts.db_name
35
+ flag [:n, :dbname]
36
+
37
+ desc '(Debug) Show debug messages'
38
+ switch [:verbose], negatable: false ## todo: use -w for short form? check ruby interpreter if in use too?
39
+
40
+ desc 'Only show warnings, errors and fatal messages'
41
+ switch [:q, :quiet], negatable: false
31
42
 
32
- global_option '-q', '--quiet', "Only show warnings, errors and fatal messages"
33
- ### todo/fix: just want --debug/--verbose flag (no single letter option wanted) - fix
34
- global_option '-w', '--verbose', "Show debug messages"
35
43
 
36
44
 
37
45
  def connect_to_db( options )
@@ -53,21 +61,17 @@ def connect_to_db( options )
53
61
  end
54
62
 
55
63
 
56
- command :create do |c|
57
- c.syntax = 'beerdb create [options]'
58
- c.description = 'Create DB schema'
64
+ desc 'Create DB schema'
65
+ command [:create] do |c|
59
66
 
60
- c.option '--extras', 'Extra tables (drinks,bookmarks,users)'
67
+ c.desc 'Extra tables (notes,drinks,marks,users)'
68
+ c.switch [:extras], negatable: false
61
69
 
62
- c.action do |args, options|
70
+ c.action do |g,o,args|
63
71
 
64
- LogUtils::Logger.root.level = :warn if options.quiet.present?
65
- LogUtils::Logger.root.level = :debug if options.verbose.present?
72
+ connect_to_db( opts )
66
73
 
67
- myopts.merge_commander_options!( options.__hash__ )
68
- connect_to_db( myopts )
69
-
70
- if options.extras.present?
74
+ if o[:extras].present?
71
75
  # quick hack: only create extra tables
72
76
  BeerDb::CreateDbExtrasUsers.new.up
73
77
  BeerDb::CreateDbExtrasBookmarks.new.up
@@ -78,133 +82,139 @@ command :create do |c|
78
82
  WorldDb.create
79
83
  BeerDb.create
80
84
  end
81
-
82
85
  puts 'Done.'
83
86
  end # action
84
87
  end # command create
85
88
 
86
- command :setup do |c|
87
- c.syntax = 'beerdb setup [options]'
88
- c.description = "Create DB schema 'n' load all data"
89
89
 
90
- c.option '--world', 'Populate world tables'
91
- ## todo: use --world-include - how? find better name?
92
- c.option '--worldinclude PATH', String, 'World data path'
90
+ desc "Create DB schema 'n' load all world and beer data"
91
+ arg_name 'NAME' # optional setup profile name
92
+ command [:setup,:s] do |c|
93
93
 
94
- c.option '--beer', 'Populate beer tables'
95
- c.option '--delete', 'Delete all records'
94
+ c.desc 'Beer data path'
95
+ c.arg_name 'PATH'
96
+ c.default_value opts.data_path
97
+ c.flag [:i,:include]
96
98
 
97
- c.action do |args, options|
99
+ c.desc 'World data path'
100
+ c.arg_name 'PATH'
101
+ c.flag [:worldinclude] ## todo: use --world-include - how? find better name? add :'world-include' ???
98
102
 
99
- LogUtils::Logger.root.level = :warn if options.quiet.present?
100
- LogUtils::Logger.root.level = :debug if options.verbose.present?
101
-
102
- myopts.merge_commander_options!( options.__hash__ )
103
- connect_to_db( myopts )
103
+ c.action do |g,o,args|
104
104
 
105
+ connect_to_db( opts )
106
+
105
107
  ## todo: document optional setup profile arg (defaults to all)
106
108
  setup = args[0] || 'all'
107
109
 
108
- if options.world.present? || options.beer.present?
109
-
110
- ## todo: check order for reference integrity
111
- # not really possible to delete world data if sport data is present
112
- # delete sport first
113
-
114
- if options.delete.present?
115
- BeerDb.delete! if options.beer.present?
116
- WorldDb.delete! if options.world.present?
117
- end
118
-
119
- if options.world.present?
120
- WorldDb.read_all( myopts.world_data_path )
121
- end
122
-
123
- if options.beer.present?
124
- BeerDb.read_setup( "setups/#{setup}", myopts.data_path )
125
- end
126
-
127
- else # assume "plain" regular setup
128
- LogDb.create
129
- WorldDb.create
130
- BeerDb.create
110
+ LogDb.create
111
+ WorldDb.create
112
+ BeerDb.create
131
113
 
132
- WorldDb.read_all( myopts.world_data_path )
133
- BeerDb.read_setup( "setups/#{setup}", myopts.data_path )
134
- end
114
+ WorldDb.read_all( opts.world_data_path )
115
+ BeerDb.read_setup( "setups/#{setup}", opts.data_path )
116
+ puts 'Done.'
117
+ end # action
118
+ end # command setup
135
119
 
120
+
121
+ desc 'Update all beer data'
122
+ arg_name 'NAME' # optional setup profile name
123
+ command [:update,:up,:u] do |c|
124
+
125
+ c.desc 'Beer data path'
126
+ c.arg_name 'PATH'
127
+ c.default_value opts.data_path
128
+ c.flag [:i,:include]
129
+
130
+ c.desc 'Delete all beer data records'
131
+ c.switch [:delete], negatable: false
132
+
133
+ c.action do |g,o,args|
134
+
135
+ connect_to_db( opts )
136
+
137
+ ## todo: document optional setup profile arg (defaults to all)
138
+ setup = args[0] || 'all'
139
+
140
+ BeerDb.delete! if o[:delete].present?
141
+
142
+ BeerDb.read_setup( "setups/#{setup}", opts.data_path )
136
143
  puts 'Done.'
137
144
  end # action
138
145
  end # command setup
139
146
 
140
- command :load do |c|
141
- ## todo: how to specify many fixutes <>... ??? in syntax
142
- c.syntax = 'beerdb load [options] <fixtures>'
143
- c.description = 'Load fixtures'
144
147
 
145
- c.option '--delete', 'Delete all records'
146
148
 
147
- c.action do |args, options|
149
+ desc 'Load beer fixtures'
150
+ arg_name 'NAME' # multiple fixture names - todo/fix: use multiple option
151
+ command [:load, :l] do |c|
148
152
 
149
- LogUtils::Logger.root.level = :warn if options.quiet.present?
150
- LogUtils::Logger.root.level = :debug if options.verbose.present?
153
+ c.desc 'Delete all beer data records'
154
+ c.switch [:delete], negatable: false
151
155
 
152
- myopts.merge_commander_options!( options.__hash__ )
153
- connect_to_db( myopts )
156
+ c.action do |g,o,args|
157
+
158
+ connect_to_db( opts )
154
159
 
155
- if options.delete.present?
156
- LogDb.delete!
157
- BeerDb.delete!
158
- end
160
+ BeerDb.delete! if o[:delete].present?
161
+
162
+ reader = BeerDb::Reader.new( opts.data_path )
159
163
 
160
- # read plain text country/region/city fixtures
161
- reader = BeerDb::Reader.new( myopts.data_path )
162
164
  args.each do |arg|
163
165
  name = arg # File.basename( arg, '.*' )
164
166
  reader.load( name )
165
167
  end # each arg
166
-
168
+
167
169
  puts 'Done.'
168
170
  end
169
171
  end # command load
170
172
 
171
173
 
172
- ## fix/todo: add server alias (serve/server)
173
174
 
174
- command :serve do |c|
175
- ## todo: how to specify many fixutes <>... ??? in syntax
176
- c.syntax = 'beerdb serve [options]'
177
- c.description = 'Start web service (HTTP JSON API)'
175
+ ## fix/todo: add server alias (serve/server)
178
176
 
179
- c.action do |args, options|
177
+ desc 'Start web service (HTTP JSON API)'
178
+ command [:serve,:server] do |c|
180
179
 
181
- LogUtils::Logger.root.level = :warn if options.quiet.present?
182
- LogUtils::Logger.root.level = :debug if options.verbose.present?
180
+ c.action do |g,o,args|
183
181
 
184
- myopts.merge_commander_options!( options.__hash__ )
185
- connect_to_db( myopts )
182
+ connect_to_db( opts )
186
183
 
187
184
  # NB: server (HTTP service) not included in standard default require
188
185
  require 'beerdb/server'
189
186
 
190
- ### fix: add ActiveRecord rack Middleware to close connection!! how?
187
+ # make sure connections get closed after every request e.g.
188
+ #
189
+ # after do
190
+ # ActiveRecord::Base.connection.close
191
+ # end
192
+ #
193
+
194
+ puts 'before add middleware ConnectionManagement'
195
+ BeerDb::Server.use ActiveRecord::ConnectionAdapters::ConnectionManagement
196
+ puts 'after add middleware ConnectionManagement'
197
+ ## todo: check if we can check on/dump middleware stack
198
+
199
+ ## rack middleware might not work with multi-threaded thin web server; close it ourselfs
200
+ BeerDb::Server.after do
201
+ puts " #{Thread.current.object_id} -- make sure db connections gets closed after request"
202
+ # todo: check if connection is open - how?
203
+ ActiveRecord::Base.connection.close
204
+ end
205
+
191
206
  BeerDb::Server.run!
192
-
207
+
193
208
  puts 'Done.'
194
209
  end
195
- end # command load
210
+ end # command serve
196
211
 
197
212
 
213
+ desc 'Show stats'
198
214
  command :stats do |c|
199
- c.syntax = 'beerdb stats [options]'
200
- c.description = 'Show stats'
201
- c.action do |args, options|
202
-
203
- LogUtils::Logger.root.level = :warn if options.quiet.present?
204
- LogUtils::Logger.root.level = :debug if options.verbose.present?
215
+ c.action do |g,o,args|
205
216
 
206
- myopts.merge_commander_options!( options.__hash__ )
207
- connect_to_db( myopts )
217
+ connect_to_db( opts )
208
218
 
209
219
  BeerDb.tables
210
220
 
@@ -213,16 +223,11 @@ command :stats do |c|
213
223
  end
214
224
 
215
225
 
226
+ desc 'Show props'
216
227
  command :props do |c|
217
- c.syntax = 'beerdb props [options]'
218
- c.description = 'Show props'
219
- c.action do |args, options|
228
+ c.action do |g,o,args|
220
229
 
221
- LogUtils::Logger.root.level = :warn if options.quiet.present?
222
- LogUtils::Logger.root.level = :debug if options.verbose.present?
223
-
224
- myopts.merge_commander_options!( options.__hash__ )
225
- connect_to_db( myopts )
230
+ connect_to_db( opts )
226
231
 
227
232
  BeerDb.props
228
233
 
@@ -230,17 +235,11 @@ command :props do |c|
230
235
  end
231
236
  end
232
237
 
233
-
238
+ desc 'Show logs'
234
239
  command :logs do |c|
235
- c.syntax = 'beerdb logs [options]'
236
- c.description = 'Show logs'
237
- c.action do |args, options|
240
+ c.action do |g,o,args|
238
241
 
239
- LogUtils::Logger.root.level = :warn if options.quiet.present?
240
- LogUtils::Logger.root.level = :debug if options.verbose.present?
241
-
242
- myopts.merge_commander_options!( options.__hash__ )
243
- connect_to_db( myopts )
242
+ connect_to_db( opts )
244
243
 
245
244
  LogDb::Models::Log.all.each do |log|
246
245
  puts "[#{log.level}] -- #{log.msg}"
@@ -252,17 +251,58 @@ end
252
251
 
253
252
 
254
253
 
254
+ desc '(Debug) Test command suite'
255
255
  command :test do |c|
256
- c.syntax = 'beerdb test [options]'
257
- c.description = 'Debug/test command suite'
258
- c.action do |args, options|
256
+ c.action do |g,o,args|
257
+
259
258
  puts "hello from test command"
260
259
  puts "args (#{args.class.name}):"
261
260
  pp args
262
- puts "options:"
263
- pp options
264
- puts "options.__hash__:"
265
- pp options.__hash__
261
+ puts "o (#{o.class.name}):"
262
+ pp o
263
+ puts "g (#{g.class.name}):"
264
+ pp g
265
+
266
+ LogUtils::Logger.root.debug 'test debug msg'
267
+ LogUtils::Logger.root.info 'test info msg'
268
+ LogUtils::Logger.root.warn 'test warn msg'
269
+
266
270
  puts 'Done.'
267
271
  end
268
272
  end
273
+
274
+
275
+
276
+ pre do |g,c,o,args|
277
+ opts.merge_gli_options!( g )
278
+ opts.merge_gli_options!( o )
279
+
280
+ puts BeerDb.banner
281
+
282
+ if opts.verbose?
283
+ LogUtils::Logger.root.level = :debug
284
+ end
285
+
286
+ logger.debug "Executing #{c.name}"
287
+ true
288
+ end
289
+
290
+ post do |global,c,o,args|
291
+ logger.debug "Executed #{c.name}"
292
+ true
293
+ end
294
+
295
+
296
+ on_error do |e|
297
+ puts
298
+ puts "*** error: #{e.message}"
299
+
300
+ if opts.verbose?
301
+ puts e.backtrace
302
+ end
303
+
304
+ false # skip default error handling
305
+ end
306
+
307
+
308
+ exit run(ARGV)
@@ -2,17 +2,26 @@ module BeerDb
2
2
 
3
3
  class Opts
4
4
 
5
-
6
- def merge_commander_options!( options = {} )
5
+ def merge_gli_options!( options = {} )
7
6
  @db_path = options[:dbpath] if options[:dbpath].present?
8
7
  @db_name = options[:dbname] if options[:dbname].present?
9
8
 
10
- @data_path = options[:include] if options[:include].present?
11
-
9
+ @verbose = true if options[:verbose] == true
10
+
11
+ @data_path = options[:include] if options[:include].present?
12
12
  @world_data_path = options[:worldinclude] if options[:worldinclude].present?
13
13
  end
14
14
 
15
15
 
16
+ def verbose=(boolean) # add: alias for debug ??
17
+ @verbose = boolean
18
+ end
19
+
20
+ def verbose?
21
+ return false if @verbose.nil? # default verbose/debug flag is false
22
+ @verbose == true
23
+ end
24
+
16
25
 
17
26
  def db_path
18
27
  @db_path || '.'
data/lib/beerdb/server.rb CHANGED
@@ -45,12 +45,15 @@ class Server < Sinatra::Base
45
45
 
46
46
  ##################
47
47
  # Helpers
48
-
49
- helpers do
50
- def path_prefix
51
- request.env['SCRIPT_NAME']
52
- end
53
- end
48
+ #
49
+ # NB: helpers are just instance methods! no need in modular apps to use
50
+ # helpers do
51
+ # <code>
52
+ # end
53
+
54
+ def path_prefix
55
+ request.env['SCRIPT_NAME']
56
+ end
54
57
 
55
58
  ##############################################
56
59
  # Controllers / Routing / Request Handlers
@@ -1,4 +1,4 @@
1
1
 
2
2
  module BeerDb
3
- VERSION = '0.8.1'
3
+ VERSION = '0.8.2'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beerdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-15 00:00:00.000000000 Z
12
+ date: 2013-07-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &77241100 !ruby/object:Gem::Requirement
16
+ requirement: &19996224 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *77241100
24
+ version_requirements: *19996224
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: worlddb
27
- requirement: &77240880 !ruby/object:Gem::Requirement
27
+ requirement: &19995804 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,47 +32,49 @@ dependencies:
32
32
  version: '1.7'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *77240880
35
+ version_requirements: *19995804
36
36
  - !ruby/object:Gem::Dependency
37
- name: commander
38
- requirement: &77240660 !ruby/object:Gem::Requirement
37
+ name: gli
38
+ requirement: &19995360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
- - - ~>
41
+ - - ! '>='
42
42
  - !ruby/object:Gem::Version
43
- version: 4.1.3
43
+ version: 2.5.6
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *77240660
46
+ version_requirements: *19995360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rdoc
49
- requirement: &77240440 !ruby/object:Gem::Requirement
49
+ requirement: &19995060 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: '3.10'
54
+ version: '4.0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *77240440
57
+ version_requirements: *19995060
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: hoe
60
- requirement: &77240220 !ruby/object:Gem::Requirement
60
+ requirement: &19994712 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
64
64
  - !ruby/object:Gem::Version
65
- version: '3.3'
65
+ version: '3.6'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *77240220
68
+ version_requirements: *19994712
69
69
  description: beerdb - beer.db command line tool
70
70
  email: beerdb@googlegroups.com
71
71
  executables:
72
72
  - beerdb
73
73
  extensions: []
74
74
  extra_rdoc_files:
75
+ - History.md
75
76
  - Manifest.txt
77
+ - README.md
76
78
  files:
77
79
  - History.md
78
80
  - Manifest.txt
@@ -136,10 +138,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
138
  version: '0'
137
139
  requirements: []
138
140
  rubyforge_project: beerdb
139
- rubygems_version: 1.8.17
141
+ rubygems_version: 1.7.2
140
142
  signing_key:
141
143
  specification_version: 3
142
144
  summary: beerdb - beer.db command line tool
143
145
  test_files:
144
- - test/test_values.rb
145
146
  - test/test_fixture_matchers.rb
147
+ - test/test_values.rb