mochigome 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +2 -0
- data/lib/mochigome_ver.rb +1 -1
- data/lib/model_extensions.rb +15 -2
- data/test/app_root/config/database-my.yml +4 -0
- data/test/app_root/config/environment.rb +3 -0
- data/test/test_helper.rb +2 -1
- data/test/unit/model_extensions_test.rb +28 -2
- metadata +4 -3
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -22,6 +22,7 @@ GEM
|
|
22
22
|
minitest (2.5.0)
|
23
23
|
mynyml-redgreen (0.7.1)
|
24
24
|
term-ansicolor (>= 1.0.4)
|
25
|
+
mysql (2.8.1)
|
25
26
|
nokogiri (1.5.0)
|
26
27
|
pdf-writer (1.1.8)
|
27
28
|
color (>= 1.4.0)
|
@@ -60,6 +61,7 @@ DEPENDENCIES
|
|
60
61
|
factory_girl (= 2.0.4)
|
61
62
|
minitest
|
62
63
|
mynyml-redgreen
|
64
|
+
mysql
|
63
65
|
nokogiri
|
64
66
|
rails (= 2.3.12)
|
65
67
|
rcov
|
data/lib/mochigome_ver.rb
CHANGED
data/lib/model_extensions.rb
CHANGED
@@ -32,7 +32,7 @@ module Mochigome
|
|
32
32
|
end
|
33
33
|
|
34
34
|
AGGREGATION_FUNCS = {
|
35
|
-
'count' => 'count()',
|
35
|
+
'count' => 'count(*)',
|
36
36
|
'distinct' => 'count(distinct %s)',
|
37
37
|
'average' => 'avg(%s)',
|
38
38
|
'avg' => 'avg(%s)',
|
@@ -151,11 +151,24 @@ module Mochigome
|
|
151
151
|
else
|
152
152
|
row = assoc_object.send(assoc.name).first(:select => sel_expr, :conditions => cond_expr)
|
153
153
|
end
|
154
|
-
h[agg[:name]] = row.x
|
154
|
+
h[agg[:name]] = self.class.auto_numerify(row.x)
|
155
155
|
end
|
156
156
|
end
|
157
157
|
h
|
158
158
|
end
|
159
|
+
|
160
|
+
def self.auto_numerify(data)
|
161
|
+
# It's already some more specific type, leave it alone
|
162
|
+
return data unless data.is_a?(String)
|
163
|
+
|
164
|
+
# Try to turn data into an integer or float if appropriate
|
165
|
+
data = data.strip
|
166
|
+
if data =~ /\A[+-]?\d+(\.\d+)?\Z/
|
167
|
+
return ($1 and !$1.blank?) ? data.to_f : data.to_i
|
168
|
+
else
|
169
|
+
return data
|
170
|
+
end
|
171
|
+
end
|
159
172
|
end
|
160
173
|
|
161
174
|
class ReportFocusSettings
|
@@ -8,6 +8,9 @@ Rails::Initializer.run do |config|
|
|
8
8
|
if ENV['PSQL_TEST_MODE']
|
9
9
|
puts "Using postgresql for a test database"
|
10
10
|
config.database_configuration_file = "#{RAILS_ROOT}/config/database-pg.yml"
|
11
|
+
elsif ENV['MYSQL_TEST_MODE']
|
12
|
+
puts "Using mysql for a test database"
|
13
|
+
config.database_configuration_file = "#{RAILS_ROOT}/config/database-my.yml"
|
11
14
|
else
|
12
15
|
puts "Using sqlite for a test database"
|
13
16
|
end
|
data/test/test_helper.rb
CHANGED
@@ -3,9 +3,10 @@ ENV['RAILS_ENV'] = 'test'
|
|
3
3
|
prev_dir = Dir.getwd
|
4
4
|
begin
|
5
5
|
Dir.chdir("#{File.dirname(__FILE__)}/..")
|
6
|
-
|
6
|
+
|
7
7
|
begin
|
8
8
|
# Used when running test files directly
|
9
|
+
$LOAD_PATH << File.dirname(__FILE__)
|
9
10
|
$LOAD_PATH << "#{File.dirname(__FILE__)}/../lib"
|
10
11
|
require "#{File.dirname(__FILE__)}/app_root/config/environment"
|
11
12
|
rescue LoadError
|
@@ -1,5 +1,31 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
2
|
|
3
|
+
describe "an input string" do
|
4
|
+
it "will be converted by auto_numerify to an integer if appropriate" do
|
5
|
+
[35, -35, 0].each do |n|
|
6
|
+
result = Mochigome::ReportFocus.auto_numerify(n.to_s)
|
7
|
+
assert_equal n, result
|
8
|
+
assert_kind_of Integer, result
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "will be converted by auto_numerify to a float if appropriate" do
|
13
|
+
[-35.5, 35.5, 0.0].each do |n|
|
14
|
+
result = Mochigome::ReportFocus.auto_numerify(n.to_s)
|
15
|
+
assert_in_delta n, result
|
16
|
+
assert_kind_of Float, result
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "will remain a string if it is not numeric" do
|
21
|
+
["", "zero", "yeehah", "foo0.0" "9.2bar"].each do |s|
|
22
|
+
result = Mochigome::ReportFocus.auto_numerify(s)
|
23
|
+
assert_equal s, result
|
24
|
+
assert_kind_of String, result
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
3
29
|
describe "an ActiveRecord model" do
|
4
30
|
before do
|
5
31
|
@model_class = Class.new(ActiveRecord::Base)
|
@@ -264,7 +290,7 @@ describe "an ActiveRecord model" do
|
|
264
290
|
# Peeking in past API to make sure it set the expressions correctly
|
265
291
|
assert_equal [
|
266
292
|
{:name => "Whales average x", :expr => "avg(x)"},
|
267
|
-
{:name => "Whales Count", :expr => "count()"},
|
293
|
+
{:name => "Whales Count", :expr => "count(*)"},
|
268
294
|
{:name => "Whales sum x", :expr => "sum(x)"}
|
269
295
|
], @model_class.mochigome_aggregations
|
270
296
|
end
|
@@ -292,7 +318,7 @@ describe "an ActiveRecord model" do
|
|
292
318
|
has_mochigome_aggregations [{"Blue Sales" => ["count", "color='blue'"]}]
|
293
319
|
end
|
294
320
|
assert_equal [
|
295
|
-
{:name => "Blue Sales", :expr => "count()", :conditions => "color='blue'"}
|
321
|
+
{:name => "Blue Sales", :expr => "count(*)", :conditions => "color='blue'"}
|
296
322
|
], @model_class.mochigome_aggregations
|
297
323
|
end
|
298
324
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mochigome
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- David Mike Simon
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- test/app_root/app/models/store.rb
|
92
92
|
- test/app_root/app/models/store_product.rb
|
93
93
|
- test/app_root/config/boot.rb
|
94
|
+
- test/app_root/config/database-my.yml
|
94
95
|
- test/app_root/config/database-pg.yml
|
95
96
|
- test/app_root/config/database.yml
|
96
97
|
- test/app_root/config/environment.rb
|