active_median 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e6c1c8d191ba4ab10a5068885e41d72c423d25c2
4
+ data.tar.gz: 0621d24b182e8fc23dd2873c26ab4229f34a73e3
5
+ SHA512:
6
+ metadata.gz: b1e9ddd636a01f252edda5d7c07a43a4cd13f78de766eeb335e7318558fc986997b5ebec6dd4a5572acd9ecb9efe64af1d9d1a5e927b11c07a056adf3b597378
7
+ data.tar.gz: cca8d23759fb830b2033070eed56f57b2a0a68f9155ef0b8ba949665e5538e048e78ce82016c131ccc527f3e507f54e848ee04a37d136e0a012af25f27f23613
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_median.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrew Kane
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # ActiveMedian
2
+
3
+ Median for ActiveRecord
4
+
5
+ **PostgreSQL only**
6
+
7
+ ## Usage
8
+
9
+ ```ruby
10
+ Item.median(:price)
11
+ ```
12
+
13
+ Works with grouping, too.
14
+
15
+ ```ruby
16
+ Order.group("date_trunc('week', created_at)").median(:total)
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application’s Gemfile:
22
+
23
+ ```ruby
24
+ gem "active_median"
25
+ ```
26
+
27
+ And add the `median` function to the database from the Rails console.
28
+
29
+ ```ruby
30
+ ActiveMedian.create_function
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_median/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_median"
8
+ spec.version = ActiveMedian::VERSION
9
+ spec.authors = ["Andrew Kane"]
10
+ spec.email = ["acekane1@gmail.com"]
11
+ spec.description = %q{Median for ActiveRecord}
12
+ spec.summary = %q{Median for ActiveRecord}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,95 @@
1
+ require "active_median/version"
2
+ require "active_record"
3
+
4
+ module ActiveMedian
5
+ def self.create_function
6
+ # create median method
7
+ # http://www.postgresonline.com/journal/archives/67-Build-Median-Aggregate-Function-in-SQL.html
8
+ ActiveRecord::Base.connection.execute <<-SQL
9
+ CREATE OR REPLACE FUNCTION array_median(numeric[])
10
+ RETURNS numeric AS
11
+ $$
12
+ SELECT CASE
13
+ WHEN array_upper($1, 1) = 0 THEN null
14
+ WHEN mod(array_upper($1, 1), 2) = 1 THEN asorted[ceiling(array_upper(asorted, 1) / 2.0)]
15
+ ELSE (
16
+ (
17
+ asorted[ceiling(array_upper(asorted, 1) / 2.0)] +
18
+ asorted[ceiling(array_upper(asorted, 1) / 2.0) + 1]
19
+ ) / 2.0
20
+ )
21
+ END
22
+ FROM (
23
+ SELECT ARRAY(
24
+ SELECT ($1)[n] FROM generate_series(1, array_upper($1, 1)) AS n
25
+ WHERE ($1)[n] IS NOT NULL ORDER BY ($1)[n]
26
+ ) AS asorted
27
+ ) AS foo;
28
+ $$
29
+ LANGUAGE SQL;
30
+
31
+ DROP AGGREGATE IF EXISTS median(numeric);
32
+ CREATE AGGREGATE median(numeric) (
33
+ SFUNC=array_append,
34
+ STYPE=numeric[],
35
+ FINALFUNC=array_median
36
+ );
37
+ SQL
38
+ true
39
+ end
40
+ end
41
+
42
+ module ActiveRecord
43
+ module Calculations
44
+ def median(column_name, options = {})
45
+ calculate(:median, column_name, options)
46
+ end
47
+ end
48
+ end
49
+
50
+ module ActiveRecord
51
+ module Querying
52
+ delegate :median, :to => :scoped
53
+ end
54
+ end
55
+
56
+ module Arel
57
+ module Nodes
58
+ const_set("Median", Class.new(Function))
59
+ end
60
+ end
61
+
62
+ module Arel
63
+ module Expressions
64
+ def median
65
+ Nodes::Median.new [self], Nodes::SqlLiteral.new('median_id')
66
+ end
67
+ end
68
+ end
69
+
70
+ module Arel
71
+ module Visitors
72
+ class ToSql
73
+ def visit_Arel_Nodes_Median o
74
+ "MEDIAN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x|
75
+ visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ module Arel
82
+ module Visitors
83
+ class DepthFirst
84
+ alias :visit_Arel_Nodes_Median :function
85
+ end
86
+ end
87
+ end
88
+
89
+ module Arel
90
+ module Visitors
91
+ class Dot
92
+ alias :visit_Arel_Nodes_Median :function
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveMedian
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_median
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Median for ActiveRecord
42
+ email:
43
+ - acekane1@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - active_median.gemspec
54
+ - lib/active_median.rb
55
+ - lib/active_median/version.rb
56
+ homepage: ''
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.0.0
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Median for ActiveRecord
80
+ test_files: []