active_median 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/CHANGELOG.md +4 -0
- data/Gemfile +3 -1
- data/README.md +14 -2
- data/Rakefile +1 -1
- data/active_median.gemspec +5 -5
- data/lib/active_median.rb +17 -7
- data/lib/active_median/version.rb +1 -1
- data/test/active_median_test.rb +9 -5
- data/test/gemfiles/{activerecord50.gemfile → activerecord42.gemfile} +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2e6418a4cb5e878f7f36dec69fe4bb169b666c4
|
4
|
+
data.tar.gz: b0da140de2cdb3c84d4f0e8408dc13d4339de6b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f29c2c2002f6438ab8f2d9ff209481f1785d5f8fba668ec2c0ed73bfe17deac908ca31823fb0b26e55cf06b1f9d5bab1d34d5f6c03dcd1f093adfb21fcfb271c
|
7
|
+
data.tar.gz: 0201efbeb295a121298a153e67f4985fee623529fcca873bbcfa8e70f5f551c90af7009a7489941014c9a0b2644cc746f9b0c18bc32cb71d40274fa4caef5f10
|
data/.travis.yml
CHANGED
@@ -12,11 +12,10 @@ notifications:
|
|
12
12
|
on_failure: change
|
13
13
|
gemfile:
|
14
14
|
- Gemfile
|
15
|
-
- test/gemfiles/
|
15
|
+
- test/gemfiles/activerecord42.gemfile
|
16
16
|
- test/gemfiles/activerecord41.gemfile
|
17
17
|
- test/gemfiles/activerecord40.gemfile
|
18
18
|
- test/gemfiles/activerecord32.gemfile
|
19
19
|
matrix:
|
20
20
|
allow_failures:
|
21
|
-
- gemfile: test/gemfiles/activerecord50.gemfile
|
22
21
|
- gemfile: test/gemfiles/activerecord32.gemfile
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -22,10 +22,22 @@ Add this line to your application’s Gemfile:
|
|
22
22
|
gem 'active_median'
|
23
23
|
```
|
24
24
|
|
25
|
-
And add the `median` function to the database
|
25
|
+
And create a migration to add the `median` function to the database.
|
26
|
+
|
27
|
+
```sh
|
28
|
+
rails g migration create_median_function
|
29
|
+
```
|
30
|
+
|
31
|
+
with:
|
26
32
|
|
27
33
|
```ruby
|
28
|
-
|
34
|
+
def up
|
35
|
+
ActiveMedian.create_function
|
36
|
+
end
|
37
|
+
|
38
|
+
def down
|
39
|
+
ActiveMedian.drop_function
|
40
|
+
end
|
29
41
|
```
|
30
42
|
|
31
43
|
## Contributing
|
data/Rakefile
CHANGED
data/active_median.gemspec
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require "active_median/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "active_median"
|
8
8
|
spec.version = ActiveMedian::VERSION
|
9
9
|
spec.authors = ["Andrew Kane"]
|
10
10
|
spec.email = ["acekane1@gmail.com"]
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
11
|
+
spec.description = "Median for ActiveRecord"
|
12
|
+
spec.summary = "Median for ActiveRecord"
|
13
13
|
spec.homepage = "https://github.com/ankane/active_median"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
-
spec.files = `git ls-files`.split(
|
16
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
data/lib/active_median.rb
CHANGED
@@ -42,6 +42,14 @@ module ActiveMedian
|
|
42
42
|
SQL
|
43
43
|
true
|
44
44
|
end
|
45
|
+
|
46
|
+
def self.drop_function
|
47
|
+
ActiveRecord::Base.connection.execute <<-SQL
|
48
|
+
DROP AGGREGATE IF EXISTS median(anyelement);
|
49
|
+
DROP FUNCTION IF EXISTS median(anyarray);
|
50
|
+
SQL
|
51
|
+
true
|
52
|
+
end
|
45
53
|
end
|
46
54
|
|
47
55
|
module ActiveRecord
|
@@ -54,7 +62,7 @@ end
|
|
54
62
|
|
55
63
|
module ActiveRecord
|
56
64
|
module Querying
|
57
|
-
delegate :median, :
|
65
|
+
delegate :median, to: (Gem::Version.new(Arel::VERSION) >= Gem::Version.new("4.0.1") ? :all : :scoped)
|
58
66
|
end
|
59
67
|
end
|
60
68
|
|
@@ -67,7 +75,7 @@ end
|
|
67
75
|
module Arel
|
68
76
|
module Expressions
|
69
77
|
def median
|
70
|
-
Nodes::Median.new [self], Nodes::SqlLiteral.new(
|
78
|
+
Nodes::Median.new [self], Nodes::SqlLiteral.new("median_id")
|
71
79
|
end
|
72
80
|
end
|
73
81
|
end
|
@@ -75,15 +83,17 @@ end
|
|
75
83
|
module Arel
|
76
84
|
module Visitors
|
77
85
|
class ToSql
|
78
|
-
def visit_Arel_Nodes_Median
|
86
|
+
def visit_Arel_Nodes_Median(o, a = nil)
|
79
87
|
if Gem::Version.new(Arel::VERSION) >= Gem::Version.new("6.0.0")
|
80
88
|
aggregate "MEDIAN", o, a
|
81
89
|
elsif a
|
82
|
-
"MEDIAN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map
|
83
|
-
|
90
|
+
"MEDIAN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map do |x|
|
91
|
+
visit x, a
|
92
|
+
end.join(', ')})#{o.alias ? " AS #{visit o.alias, a}" : ''}"
|
84
93
|
else
|
85
|
-
"MEDIAN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map
|
86
|
-
|
94
|
+
"MEDIAN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map do |x|
|
95
|
+
visit x
|
96
|
+
end.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
|
87
97
|
end
|
88
98
|
end
|
89
99
|
end
|
data/test/active_median_test.rb
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
3
|
class TestActiveMedian < Minitest::Test
|
4
|
-
|
5
4
|
def setup
|
6
5
|
ActiveMedian.create_function
|
7
6
|
User.delete_all
|
8
7
|
end
|
9
8
|
|
10
9
|
def test_even
|
11
|
-
[1, 1, 2, 3, 4, 100].each {|n| User.create!(visits_count: n) }
|
10
|
+
[1, 1, 2, 3, 4, 100].each { |n| User.create!(visits_count: n) }
|
12
11
|
assert_equal 2.5, User.median(:visits_count)
|
13
12
|
end
|
14
13
|
|
15
14
|
def test_odd
|
16
|
-
[1, 1, 2, 4, 100].each {|n| User.create!(visits_count: n) }
|
15
|
+
[1, 1, 2, 4, 100].each { |n| User.create!(visits_count: n) }
|
17
16
|
assert_equal 2, User.median(:visits_count)
|
18
17
|
end
|
19
18
|
|
@@ -22,13 +21,18 @@ class TestActiveMedian < Minitest::Test
|
|
22
21
|
end
|
23
22
|
|
24
23
|
def test_decimal
|
25
|
-
6.times {|n| User.create!(latitude: n * 0.1) }
|
24
|
+
6.times { |n| User.create!(latitude: n * 0.1) }
|
26
25
|
assert_equal 0.25, User.median(:latitude)
|
27
26
|
end
|
28
27
|
|
29
28
|
def test_float
|
30
|
-
6.times {|n| User.create!(rating: n * 0.1) }
|
29
|
+
6.times { |n| User.create!(rating: n * 0.1) }
|
31
30
|
assert_equal 0.25, User.median(:rating)
|
32
31
|
end
|
33
32
|
|
33
|
+
def test_drop
|
34
|
+
ActiveMedian.drop_function
|
35
|
+
error = assert_raises(ActiveRecord::StatementInvalid) { User.median(:visits_count) }
|
36
|
+
assert_includes error.message, "PG::UndefinedFunction"
|
37
|
+
end
|
34
38
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_median
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03
|
11
|
+
date: 2016-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -101,7 +101,7 @@ files:
|
|
101
101
|
- test/gemfiles/activerecord32.gemfile
|
102
102
|
- test/gemfiles/activerecord40.gemfile
|
103
103
|
- test/gemfiles/activerecord41.gemfile
|
104
|
-
- test/gemfiles/
|
104
|
+
- test/gemfiles/activerecord42.gemfile
|
105
105
|
- test/test_helper.rb
|
106
106
|
homepage: https://github.com/ankane/active_median
|
107
107
|
licenses:
|
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
123
|
version: '0'
|
124
124
|
requirements: []
|
125
125
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
126
|
+
rubygems_version: 2.5.1
|
127
127
|
signing_key:
|
128
128
|
specification_version: 4
|
129
129
|
summary: Median for ActiveRecord
|
@@ -132,6 +132,5 @@ test_files:
|
|
132
132
|
- test/gemfiles/activerecord32.gemfile
|
133
133
|
- test/gemfiles/activerecord40.gemfile
|
134
134
|
- test/gemfiles/activerecord41.gemfile
|
135
|
-
- test/gemfiles/
|
135
|
+
- test/gemfiles/activerecord42.gemfile
|
136
136
|
- test/test_helper.rb
|
137
|
-
has_rdoc:
|