active_median 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +5 -1
- data/lib/active_median/enumerable.rb +2 -2
- data/lib/active_median/model.rb +10 -6
- data/lib/active_median/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fde94235a65aa88d7417686a9ac804ca31bf48cb631ce6fa1f33f82f182f683
|
4
|
+
data.tar.gz: bca00d9fa0e7be65745217fc1c0934de095238302eaff1ce687fd47da73ad4aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ff7c51cd683245dd808ee4ee314e20b100b944201c623bb2bb05ab490d5b90b5331de797062f152b77cffc1adfa94796a8f21558d3223b375fa4af050ac0a0f
|
7
|
+
data.tar.gz: 841082d44d97415a8a7c56893d286399c95fa8e601deafb26f60f97c1ad10c9f64c8199b1bc172557429738348ae99021eee742924d00797615beb9155f0e69f
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -13,7 +13,7 @@ Supports:
|
|
13
13
|
|
14
14
|
:fire: Uses native functions for blazing performance
|
15
15
|
|
16
|
-
[![Build Status](https://github.com/ankane/active_median/workflows/build/badge.svg
|
16
|
+
[![Build Status](https://github.com/ankane/active_median/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/active_median/actions)
|
17
17
|
|
18
18
|
## Getting Started
|
19
19
|
|
@@ -105,6 +105,10 @@ db.load_extension("percentile.so") # or percentile.dylib
|
|
105
105
|
db.enable_load_extension(0)
|
106
106
|
```
|
107
107
|
|
108
|
+
## History
|
109
|
+
|
110
|
+
View the [changelog](https://github.com/ankane/active_median/blob/master/CHANGELOG.md)
|
111
|
+
|
108
112
|
## Contributing
|
109
113
|
|
110
114
|
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
@@ -2,7 +2,7 @@ module Enumerable
|
|
2
2
|
unless method_defined?(:median)
|
3
3
|
def median(*args, &block)
|
4
4
|
if !block && respond_to?(:scoping)
|
5
|
-
scoping {
|
5
|
+
scoping { klass.median(*args) }
|
6
6
|
elsif !block && respond_to?(:with_scope)
|
7
7
|
with_scope(self) { klass.median(*args) }
|
8
8
|
else
|
@@ -15,7 +15,7 @@ module Enumerable
|
|
15
15
|
unless method_defined?(:percentile)
|
16
16
|
def percentile(*args, &block)
|
17
17
|
if !block && respond_to?(:scoping)
|
18
|
-
scoping {
|
18
|
+
scoping { klass.percentile(*args) }
|
19
19
|
elsif !block && respond_to?(:with_scope)
|
20
20
|
with_scope(self) { klass.percentile(*args) }
|
21
21
|
else
|
data/lib/active_median/model.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
module ActiveMedian
|
2
2
|
module Model
|
3
3
|
def median(column)
|
4
|
-
|
4
|
+
connection_pool.with_connection do |connection|
|
5
|
+
calculate_percentile(column, 0.5, "median", connection)
|
6
|
+
end
|
5
7
|
end
|
6
8
|
|
7
9
|
def percentile(column, percentile)
|
8
|
-
|
10
|
+
connection_pool.with_connection do |connection|
|
11
|
+
calculate_percentile(column, percentile, "percentile", connection)
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
private
|
12
16
|
|
13
|
-
def calculate_percentile(column, percentile, operation)
|
17
|
+
def calculate_percentile(column, percentile, operation, connection)
|
14
18
|
percentile = Float(percentile, exception: false)
|
15
19
|
raise ArgumentError, "invalid percentile" if percentile.nil?
|
16
20
|
raise ArgumentError, "percentile is not between 0 and 1" if percentile < 0 || percentile > 1
|
@@ -20,7 +24,7 @@ module ActiveMedian
|
|
20
24
|
# matches table.column and column
|
21
25
|
unless column.is_a?(Symbol) || column.is_a?(Arel::Nodes::SqlLiteral)
|
22
26
|
column = column.to_s
|
23
|
-
unless /\A\w+(\.\w+)?\z/i.match(column)
|
27
|
+
unless /\A\w+(\.\w+)?\z/i.match?(column)
|
24
28
|
raise ActiveRecord::UnknownAttributeReference, "Query method called with non-attribute argument(s): #{column.inspect}. Use Arel.sql() for known-safe values."
|
25
29
|
end
|
26
30
|
end
|
@@ -34,12 +38,12 @@ module ActiveMedian
|
|
34
38
|
end
|
35
39
|
# safety check
|
36
40
|
# could quote, but want to keep consistent with Active Record
|
37
|
-
raise "Bad column alias: #{column_alias}. Please report a bug." unless
|
41
|
+
raise "Bad column alias: #{column_alias}. Please report a bug." unless /\A[a-z0-9_]+\z/.match?(column_alias)
|
38
42
|
|
39
43
|
# column resolution
|
40
44
|
node = relation.send(:arel_columns, [column]).first
|
41
45
|
node = Arel::Nodes::SqlLiteral.new(node) if node.is_a?(String)
|
42
|
-
column =
|
46
|
+
column = connection.visitor.accept(node, Arel::Collectors::SQLString.new).value
|
43
47
|
|
44
48
|
# prevent SQL injection
|
45
49
|
percentile = connection.quote(percentile)
|
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.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '7'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '7'
|
27
27
|
description:
|
28
28
|
email: andrew@ankane.org
|
29
29
|
executables: []
|
@@ -51,14 +51,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3'
|
54
|
+
version: '3.1'
|
55
55
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
56
|
requirements:
|
57
57
|
- - ">="
|
58
58
|
- !ruby/object:Gem::Version
|
59
59
|
version: '0'
|
60
60
|
requirements: []
|
61
|
-
rubygems_version: 3.
|
61
|
+
rubygems_version: 3.5.16
|
62
62
|
signing_key:
|
63
63
|
specification_version: 4
|
64
64
|
summary: Median and percentile for Active Record, Mongoid, arrays, and hashes
|