rgviz-rails 0.49 → 0.50

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@ module Rgviz
8
8
  @selects = []
9
9
  @joins = {}
10
10
  @labels = {}
11
+ @formats = {}
11
12
  @pivots = {}
12
13
  @group_bys = {}
13
14
  @original_columns = []
@@ -33,6 +34,7 @@ module Rgviz
33
34
 
34
35
  process_pivot
35
36
  process_labels
37
+ process_formats
36
38
 
37
39
  generate_columns
38
40
  generate_conditions
@@ -52,6 +54,14 @@ module Rgviz
52
54
  end
53
55
  end
54
56
 
57
+ def process_formats
58
+ return unless @query.formats.present?
59
+
60
+ @query.formats.each do |format|
61
+ @formats[format.column.to_s] = format.pattern
62
+ end
63
+ end
64
+
55
65
  def process_pivot
56
66
  if @query.pivot
57
67
  @query.pivot.columns.each do |column|
@@ -182,6 +192,10 @@ module Rgviz
182
192
  @table.cols.each do |col|
183
193
  hash = {}
184
194
  hash[:v] = column_value(col, result.send("c#{i}")) unless @query.options && @query.options.no_values
195
+
196
+ format = @formats[@original_columns[i]]
197
+ hash[:f] = format_value(col, format, hash[:v]) if format
198
+
185
199
  row.c << Cell.new(hash)
186
200
  i += 1
187
201
  end
@@ -261,6 +275,10 @@ module Rgviz
261
275
  if @group_bys.include?(original_column)
262
276
  hash = {}
263
277
  hash[:v] = key[group_i] unless @query.options && @query.options.no_values
278
+
279
+ format = @formats[original_column]
280
+ hash[:f] = format_value(@table.cols[i], format, hash[:v]) if format
281
+
264
282
  row.c << (Cell.new hash)
265
283
  group_i += 1
266
284
  elsif !@pivots.include?(original_column)
@@ -270,6 +288,10 @@ module Rgviz
270
288
 
271
289
  hash = {}
272
290
  hash[:v] = v unless @query.options && @query.options.no_values
291
+
292
+ format = @formats[original_column]
293
+ hash[:f] = format_value(@table.cols[i], format, hash[:v]) if format
294
+
273
295
  row.c << (Cell.new hash)
274
296
  end
275
297
  value_i += 1
@@ -370,6 +392,17 @@ module Rgviz
370
392
  @labels[string] || string
371
393
  end
372
394
 
395
+ def format_value(col, format, value)
396
+ return nil if value.nil?
397
+
398
+ case col.type
399
+ when :boolean, :number, :string
400
+ format % value
401
+ when :date, :datetime, :timeofday
402
+ value.strftime(format)
403
+ end
404
+ end
405
+
373
406
  def to_string(node, visitor_class)
374
407
  visitor = visitor_class.new self
375
408
  node.accept visitor
@@ -21,7 +21,7 @@ describe Executor do
21
21
  date.strftime "new Date(%Y, %m, %d)"
22
22
  end
23
23
 
24
- def self.it_processes_single_select_column(query, id, type, value, label, options = {}, test_options = {})
24
+ def self.it_processes_single_select_column(query, id, type, value, label, format = nil, options = {}, test_options = {})
25
25
  it "processes select #{query}", test_options do
26
26
  if block_given?
27
27
  yield
@@ -40,6 +40,7 @@ describe Executor do
40
40
  table.rows[0].c.length.should == 1
41
41
 
42
42
  table.rows[0].c[0].v.should == value
43
+ table.rows[0].c[0].f.should == format
43
44
  end
44
45
  end
45
46
 
@@ -266,11 +267,11 @@ describe Executor do
266
267
  Person.make :name => 'FOO'
267
268
  end
268
269
 
269
- it_processes_single_select_column "concat(age)", 'c0', :string, '20', "concat(age)", :extensions => true do
270
+ it_processes_single_select_column "concat(age)", 'c0', :string, '20', "concat(age)", nil, :extensions => true do
270
271
  Person.make :age => 20
271
272
  end
272
273
 
273
- it_processes_single_select_column "concat(name, 'bar')", 'c0', :string, 'foobar', "concat(name, 'bar')", :extensions => true do
274
+ it_processes_single_select_column "concat(name, 'bar')", 'c0', :string, 'foobar', "concat(name, 'bar')", nil, :extensions => true do
274
275
  Person.make :name => 'foo'
275
276
  end
276
277
 
@@ -470,4 +471,47 @@ describe Executor do
470
471
  end
471
472
  end
472
473
 
474
+ # Formatting
475
+ it_processes_single_select_column 'false format false "%s is falsey"', 'c0', :boolean, false, 'false', 'false is falsey'
476
+ it_processes_single_select_column '1 format 1 "%.2f"', 'c0', :number, 1, '1', '1.00'
477
+ it_processes_single_select_column '1.2 format 1.2 "%.2f"', 'c0', :number, 1.2, '1.2', '1.20'
478
+ it_processes_single_select_column '"hello" format "hello" "%s world"', 'c0', :string, "hello", "'hello'", 'hello world'
479
+ it_processes_single_select_column 'date "2001-01-02" format date "2001-01-02" "%Y"', 'c0', :date, Time.parse('2001-01-02').to_date, "date '2001-01-02'", '2001'
480
+
481
+ it "processes pivot with format" do
482
+ Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 1000
483
+ Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 500
484
+ Person.make :name => 'Eng', :birthday => '2000-01-13', :age => 600
485
+ Person.make :name => 'Sales', :birthday => '2000-01-12', :age => 400
486
+ Person.make :name => 'Sales', :birthday => '2000-01-12', :age => 350
487
+ Person.make :name => 'Marketing', :birthday => '2000-01-13', :age => 800
488
+
489
+ table = exec 'select name, sum(age) group by name pivot birthday order by name format name "x %s", sum(age) "%.2f"'
490
+
491
+ table.cols.length.should == 3
492
+
493
+ i = 0
494
+ [['c0', :string, 'name'],
495
+ ['c1', :number, '2000-01-12 sum(age)'],
496
+ ['c2', :number, '2000-01-13 sum(age)']].each do |id, type, label|
497
+ table.cols[i].id.should == id
498
+ table.cols[i].type.should == type
499
+ table.cols[i].label.should == label
500
+ i += 1
501
+ end
502
+
503
+ table.rows.length.should == 3
504
+
505
+ i = 0
506
+ [[['Eng', 'x Eng'], [1500, '1500.00'], [600, '600.00']],
507
+ [['Marketing', 'x Marketing'], [nil, nil], [800, '800.00']],
508
+ [['Sales', 'x Sales'], [750, '750.00'], [nil, nil]]].each do |values|
509
+ table.rows[i].c.length.should == 3
510
+ values.each_with_index do |v, j|
511
+ table.rows[i].c[j].v.should == v[0]
512
+ table.rows[i].c[j].f.should == v[1]
513
+ end
514
+ i += 1
515
+ end
516
+ end
473
517
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgviz-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.49'
4
+ version: '0.50'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rgviz
16
- requirement: &70297488435440 !ruby/object:Gem::Requirement
16
+ requirement: &70152760172220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70297488435440
24
+ version_requirements: *70152760172220
25
25
  description:
26
26
  email: aborenszweig@manas.com.ar
27
27
  executables: []