active_median 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 898f80afa3c78ec2137b1c8f2f2dae6e4604ff657f472725f10f9486a82fc32f
4
- data.tar.gz: '080da5da53ea03e6a20f8a5e80d70afdec3e6fa2d1d140e18f680ac361bc077d'
3
+ metadata.gz: cfbbd982f15a11c2f820533f977d7d066e1a8f46a397155681ca06673cf44cc2
4
+ data.tar.gz: d2156aea58744c1d2955c00077f2b4ef064b2d541d8c69f0ca0a923d93ecbd29
5
5
  SHA512:
6
- metadata.gz: e60fa9abc00e89da32390c0608a8e297e8ba4a36a869eea44b0ac94c6dcff00a49b71823f1942313d7709489c9af78b52260f8d1c48d739ec842b2aebdc0ebf6
7
- data.tar.gz: a1ee8deb9ba6352732ed008f8bb255c4a7f248bc63beb27d3c74893306af57a0f0d00ae7848c1e6356263a345fbf70151d0b59d633071aa407685117dc6ccb1c
6
+ metadata.gz: b5ff96c9f885f59dbc83dd1744fe427f67d1e43eefdb527fcb7e8994427452de186e12e8e0849bc1b15d3bcca2f65c0ec2a7403c631d2dba0f170b87bda074df
7
+ data.tar.gz: 39671ed1587132c3e54cf9bb1e44f3d9999e7435aacdb2438d03989e94e8cb29b3448663c06bb23fb45315609098d28dc557d0726db612dc290c0229b9a2361a
@@ -1,3 +1,8 @@
1
+ ## 0.2.1
2
+
3
+ - Added support for arrays and hashes
4
+ - Added compatibility with Groupdate 4
5
+
1
6
  ## 0.2.0
2
7
 
3
8
  - Added support for MariaDB 10.3.3+ and SQLite
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Median for ActiveRecord
4
4
 
5
- Supports PostgreSQL 9.4+, MariaDB 10.3.3+, and SQLite
5
+ Supports PostgreSQL 9.4+, MariaDB 10.3.3+, and SQLite, plus arrays and hashes
6
6
 
7
7
  :fire: Uses native functions for blazing performance
8
8
 
@@ -17,7 +17,32 @@ Item.median(:price)
17
17
  Works with grouping, too.
18
18
 
19
19
  ```ruby
20
- Order.group("date_trunc('week', created_at)").median(:total)
20
+ Order.group(:store_id).median(:total)
21
+ ```
22
+
23
+ ## Arrays and Hashes
24
+
25
+ ```ruby
26
+ [1, 2, 3].median
27
+ ```
28
+
29
+ You can also pass a block.
30
+
31
+ ```ruby
32
+ {a: 1, b: 2, c: 3}.median { |k, v| v }
33
+ ```
34
+
35
+ ## User Input
36
+
37
+ If passing user input as the column, be sure to sanitize it first [like you must](https://rails-sqli.org/) with other aggregate methods like `sum`.
38
+
39
+ ```ruby
40
+ column = params[:column]
41
+
42
+ # check against permitted columns
43
+ raise "Unpermitted column" unless ["column_a", "column_b"].include?(column)
44
+
45
+ User.median(column)
21
46
  ```
22
47
 
23
48
  ## Installation
@@ -1,6 +1,7 @@
1
1
  require "active_support"
2
2
 
3
3
  require "active_median/model"
4
+ require "active_median/enumerable"
4
5
  require "active_median/version"
5
6
 
6
7
  module ActiveMedian
@@ -0,0 +1,13 @@
1
+ module Enumerable
2
+ unless method_defined?(:median)
3
+ def median(*args, &block)
4
+ if respond_to?(:scoping) && !block
5
+ scoping { @klass.median(*args) }
6
+ else
7
+ sorted = map(&block).sort
8
+ len = sorted.length
9
+ (sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0
10
+ end
11
+ end
12
+ end
13
+ end
@@ -27,11 +27,16 @@ module ActiveMedian
27
27
  rows << (result.column_types.empty? ? untyped_row : columns.each_with_index.map { |c, i| untyped_row[i] ? result.column_types[c].send(cast_method, untyped_row[i]) : untyped_row[i] })
28
28
  end
29
29
 
30
- if group_values.any?
31
- Hash[rows.map { |r| [r.size == 2 ? r[0] : r[0..-2], r[-1]] }]
32
- else
33
- rows[0] && rows[0][0]
34
- end
30
+ result =
31
+ if group_values.any?
32
+ Hash[rows.map { |r| [r.size == 2 ? r[0] : r[0..-2], r[-1]] }]
33
+ else
34
+ rows[0] && rows[0][0]
35
+ end
36
+
37
+ result = Groupdate.process_result(relation, result) if defined?(Groupdate.process_result)
38
+
39
+ result
35
40
  end
36
41
  end
37
42
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveMedian
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_median
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: groupdate
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email: andrew@chartkick.com
85
99
  executables: []
@@ -90,6 +104,7 @@ files:
90
104
  - LICENSE.txt
91
105
  - README.md
92
106
  - lib/active_median.rb
107
+ - lib/active_median/enumerable.rb
93
108
  - lib/active_median/model.rb
94
109
  - lib/active_median/version.rb
95
110
  homepage: https://github.com/ankane/active_median