calculate-all 0.1.0

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: 87793b642f35f1a0ac13c64ccc7b18f4dff5214f
4
+ data.tar.gz: 63106f78ba19a0e937389595464fe167cbd4fe78
5
+ SHA512:
6
+ metadata.gz: 195576395014c54049f6837ec4e44a4f72ace4b1e97ad4c4523bb85455c12efef59a3af9f5c0969cfb8bed01ce79e4c6c082f4bfc999dd1873682a32d413ebae
7
+ data.tar.gz: d94eb40b7d9e876d9c6c9d0fd3be3eb89132dd9c0d01ff2cb950d8887f1b17b9f952f978b568fbd1bd27b52fd418da8aebd0c72aec3133864bcd657ac20b0198
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in calculate-all.gemspec
4
+ gemspec
5
+
6
+ gem "activerecord"
7
+ gem "pry"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alexey "codesnik" Trofimenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ # CalculateAll
2
+
3
+ Provides `#calculate_all` method on your Active Record models, scopes and relations.
4
+ It's a little addition to Active Record's `#count`, `#maximum`, `#minimum`, `#average` and `#sum`.
5
+ It allows to fetch all of the above and any other aggregate functions results in one request, with respect to grouping.
6
+
7
+ Tested only with Postgres and Mysql only right now. It relies on automatic values type-casting of underlying driver.
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ results = YourModel.yourscopes.group(:grouping1).group(:grouping2)
13
+ .calculate_all(:column1_max, :column2_distinct_count,
14
+ column3_median: 'percentile_cont(0.5) within group (order by column3 desc)')
15
+ ```
16
+
17
+ `#calculate_all` tries to mimic magic level of Active Record's `#group, `#count` and `#pluck`
18
+ so result type depends on arguments and on groupings.
19
+
20
+ ### Container
21
+
22
+ If you have no `group()` on underlying scope, `#calculate_all` will return just one result.
23
+ If you have one group, it will return hash of results, with simple keys.
24
+ If you have two or more groupings, each result will have an array as a key.
25
+
26
+ ### Results
27
+
28
+ If you provide just one argument to `#calculate_all`, its calculated value will be returned as is.
29
+ Otherwise results would be returned as hash(es) with symbol keys.
30
+
31
+ so, `Model.calculate_all(:count)` will return just a single integer,
32
+ but `Model.group(:foo1, :foo2).calculate_all(expr1: 'count(expr1)', expr2: 'count(expr2)')` will return
33
+ something like this:
34
+
35
+ ```ruby
36
+ {
37
+ ['foo1_1', 'foo2_1'] => {expr1: 0, expr2: 1},
38
+ ['foo1_1', 'foo2_2'] => {expr1: 2, expr2: 3},
39
+ ...
40
+ }
41
+ ```
42
+
43
+ ## Installation
44
+
45
+ Add this line to your application's Gemfile:
46
+
47
+ ```ruby
48
+ gem 'calculate-all'
49
+ ```
50
+
51
+ And then execute:
52
+
53
+ $ bundle
54
+
55
+ Or install it yourself as:
56
+
57
+ $ gem install calculate-all
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/codesnik/calculate-all.
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "calculate-all"
5
+
6
+ require './test/test_helper'
7
+ require 'logger'
8
+ ActiveRecord::Base.logger = Logger.new(STDERR)
9
+ require "pry"
10
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'calculate-all/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "calculate-all"
8
+ spec.version = CalculateAll::VERSION
9
+ spec.authors = ["codesnik"]
10
+ spec.email = ["aronaxis@gmail.com"]
11
+
12
+ spec.summary = %q{Fetch from database results of several aggregate functions at once}
13
+ spec.description = %q{Extends Active Record with #calculate_all method}
14
+ spec.homepage = "http://github.com/codesnik/calculate-all"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "activerecord"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest"
27
+ spec.add_development_dependency "pg"
28
+ spec.add_development_dependency "mysql2"
29
+ end
@@ -0,0 +1,78 @@
1
+ require "calculate-all/version"
2
+ require "active_record"
3
+
4
+ module CalculateAll
5
+ def calculate_all(*function_aliases, **functions)
6
+ if function_aliases.size == 1 && functions == {}
7
+ return_plain_values = true
8
+ end
9
+ functions.merge!(
10
+ CalculateAll::Helpers.decode_function_aliases(function_aliases)
11
+ )
12
+ if functions == {}
13
+ raise ArgumentError, "provide at least one function to calculate"
14
+ end
15
+ if functions.size == 1 && group_values.size == 0
16
+ plain_rows = true
17
+ end
18
+
19
+ results = {}
20
+
21
+ pluck(*group_values, *functions.values).each do |row|
22
+ row = [row] if plain_rows
23
+ if return_plain_values
24
+ value = row.last
25
+ else
26
+ value = functions.keys.zip(row.last(functions.size)).to_h
27
+ end
28
+
29
+ # Return unwrapped hash directly for scope without any .group()
30
+ return value if group_values.empty?
31
+
32
+ if group_values.size == 1
33
+ key = row.first
34
+ else
35
+ key = row.first(group_values.size)
36
+ end
37
+ results[key] = value
38
+ end
39
+
40
+ results
41
+ end
42
+
43
+ module Helpers
44
+ module_function
45
+ def decode_function_aliases(aliases)
46
+ aliases.map do |key|
47
+ function =
48
+ case key
49
+ when String
50
+ key
51
+ when :count
52
+ "COUNT(*)"
53
+ when /^(.*)_distinct_count$/, /^count_distinct_(.*)$/
54
+ "COUNT(DISTINCT #{$1})"
55
+ when /^(.*)_(count|sum|max|min|avg)$/
56
+ "#{$2.upcase}(#{$1})"
57
+ when /^(count|sum|max|min|avg)_(.*)$$/
58
+ "#{$1.upcase}(#{$2})"
59
+ when /^(.*)_average$/, /^average_(.*)$/
60
+ "AVG(#{$1})"
61
+ when /^(.*)_maximum$/, /^maximum_(.*)$/
62
+ "MAX(#{$1})"
63
+ when /^(.*)_minimum$/, /^minimum_(.*)$/
64
+ "MIN(#{$1})"
65
+ else
66
+ raise ArgumentError, "Can't recognize function alias #{key}"
67
+ end
68
+ [key, function]
69
+ end.to_h
70
+ end
71
+ end
72
+ module Querying
73
+ delegate :calculate_all, to: :all
74
+ end
75
+ end
76
+
77
+ ActiveRecord::Relation.include CalculateAll
78
+ ActiveRecord::Base.extend CalculateAll::Querying
@@ -0,0 +1,3 @@
1
+ module CalculateAll
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calculate-all
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - codesnik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mysql2
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'
97
+ description: 'Extends Active Record with #calculate_all method'
98
+ email:
99
+ - aronaxis@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - calculate-all.gemspec
113
+ - lib/calculate-all.rb
114
+ - lib/calculate-all/version.rb
115
+ homepage: http://github.com/codesnik/calculate-all
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.4.5.1
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Fetch from database results of several aggregate functions at once
139
+ test_files: []