ugc 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ugc (0.0.4)
4
+ ugc (0.0.5)
5
5
  command_line_reporter
6
6
  gli (= 2.5.0)
7
7
  highline
data/README.md CHANGED
@@ -14,7 +14,7 @@ ugc enables convenient terminal access to Apigee's App Services (aka Usergrid).
14
14
 
15
15
  $ gem install ugc
16
16
 
17
- Note: Requires Ruby 1.9.x. If you have issues, check your version:
17
+ Note: Requires Ruby 1.9.3+. If you have issues, check your version:
18
18
 
19
19
  $ ruby -v
20
20
 
@@ -49,6 +49,14 @@ Connect to an Apigee administrator account:
49
49
 
50
50
  ## Release notes
51
51
 
52
+ ### 0.0.5
53
+ * New features
54
+ 1. smart column widths
55
+ 1. add "limit" keyword to sql syntax
56
+ * Bug fixes
57
+ 1. fixed issue with selecting a single column
58
+ 1. fixed formatting of entities with heterogeneous attributes
59
+
52
60
  ### 0.0.4
53
61
  * Bug fixes
54
62
  1. include Gemfile.lock to ensure correct version of usergrid_iron is used
@@ -22,7 +22,14 @@ command :query do |c|
22
22
  query.gsub! /from\s+#{type}/i, ''
23
23
  end
24
24
 
25
- response = $application[type].query query
25
+ params = {}
26
+ if parsed_query['limit']
27
+ limit = parsed_query['limit']
28
+ query.gsub! /limit\s+#{limit}/i, ''
29
+ params[:limit] = limit
30
+ end
31
+
32
+ response = $application[type].query query, params
26
33
 
27
34
  format_collection response.collection, parsed_query['select']
28
35
  end
@@ -13,25 +13,20 @@ COL_OVERHEAD = 3
13
13
 
14
14
  def format_collection(collection, headers=nil)
15
15
  if collection && collection.size > 0
16
+ metadata = collection_metadata collection, headers
16
17
  table border: true do
17
18
  row header: true do
18
- headers ||= collection.first.keys.reject{|e| SKIP_ATTRS.include? e}
19
+ headers ||= metadata.keys
19
20
  column '#', width: INDEX_COL_WIDTH
20
- headers.each do |r|
21
- column r, width: equal_column_size(headers.size)
22
- end
21
+ headers.each {|header| column header, width: metadata[header][:size] }
23
22
  end
24
23
  collection.each_with_index do |entity, index|
25
24
  row do
26
25
  column index+1
27
26
  if entity.is_a? Array
28
- entity.each do |v|
29
- column v
30
- end
27
+ entity.each {|v| column v }
31
28
  else
32
- entity.reject{|k,v| SKIP_ATTRS.include? k}.each_value do |v|
33
- column v
34
- end
29
+ headers.each {|header| column entity[header]}
35
30
  end
36
31
  end
37
32
  end
@@ -63,10 +58,35 @@ def format_entity(entity)
63
58
  end
64
59
  end
65
60
 
66
- def equal_column_size(num_cols)
67
- ((terminal_columns - COL_OVERHEAD - (INDEX_COL_WIDTH + COL_OVERHEAD)) / num_cols).to_i - COL_OVERHEAD
61
+ # return hash { column_name: { max_size: 12, size: 12 } }
62
+ def collection_metadata(collection, headers=nil)
63
+ result = {}
64
+ collection.each do |entity|
65
+ if entity.is_a? Array
66
+ headers.each_with_index do |header, index|
67
+ col = result[header] ||= {}
68
+ size = entity[index].to_s.size
69
+ col[:max_size] = col[:max_size] ? [col[:max_size], size].max : size
70
+ end
71
+ else
72
+ entity.reject{|k,v| headers ? !headers.include?(k) : SKIP_ATTRS.include?(k)}.each do |k,v|
73
+ col = result[k] ||= {}
74
+ size = v.to_s.size
75
+ col[:max_size] = col[:max_size] ? [col[:max_size], size].max : size
76
+ end
77
+ end
78
+ end
79
+ total_size = result.inject(0) do |total, (col,meta)|
80
+ meta[:max_size] = [col.size, meta[:max_size]].max
81
+ total + meta[:max_size]
82
+ end
83
+ terminal_columns = HighLine.new.output_cols
84
+ overhead = (result.keys.size + 2) * COL_OVERHEAD + INDEX_COL_WIDTH
85
+ if total_size + overhead < terminal_columns
86
+ result.each {|col,meta| meta[:size] = meta[:max_size]}
87
+ else
88
+ col_size = (terminal_columns - overhead) / result.keys.size
89
+ result.each {|col,meta| meta[:size] = col_size}
90
+ end
91
+ result
68
92
  end
69
-
70
- def terminal_columns
71
- HighLine.new.output_cols
72
- end
@@ -1,6 +1,6 @@
1
1
  def parse_sql(query)
2
2
  result = {}
3
- keywords = %w(select from where)
3
+ keywords = %w(select from where limit)
4
4
  current = nil
5
5
  query.downcase.split(/[\s,*]/).each do |ea|
6
6
  next if ea == ''
@@ -14,7 +14,7 @@ def parse_sql(query)
14
14
  result[current] = [result[current]] << ea
15
15
  end
16
16
  else
17
- result[current] = ea
17
+ result[current] = (current == 'select') ? [ea] : ea
18
18
  end
19
19
  end
20
20
  end
data/lib/ugc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ugc
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ugc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -185,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
185
185
  version: '0'
186
186
  segments:
187
187
  - 0
188
- hash: -2840457187789729927
188
+ hash: -192062484210983223
189
189
  required_rubygems_version: !ruby/object:Gem::Requirement
190
190
  none: false
191
191
  requirements:
@@ -194,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
194
  version: '0'
195
195
  segments:
196
196
  - 0
197
- hash: -2840457187789729927
197
+ hash: -192062484210983223
198
198
  requirements: []
199
199
  rubyforge_project:
200
200
  rubygems_version: 1.8.24