calculate-all 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +40 -1
- data/calculate-all.gemspec +1 -0
- data/lib/calculate-all.rb +13 -2
- data/lib/calculate-all/version.rb +1 -1
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcc98d3d676bc5db549074f9bd4f3e182f42ecb1
|
4
|
+
data.tar.gz: 0ec0592767487a7245339f4c4f7ac0c8a29c22c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad855ac14b2c81caa8c849f0e9be8b28a8aeae0c87faf47e25a63e5b56739d63cc6c111211e170ec2b9757636d6c11b214ba937da5cd4caf05192f477ed34018
|
7
|
+
data.tar.gz: 2b3c94259c9d902722b31f203bb4620800fb863eea0fe12fb8561bb60dfc86177148704bb1b5de02ddd8b0d75290a46ed4b87843ddd2199229c2514579779685
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -14,7 +14,7 @@ results = YourModel.yourscopes.group(:grouping1).group(:grouping2)
|
|
14
14
|
column3_median: 'percentile_cont(0.5) within group (order by column3 desc)')
|
15
15
|
```
|
16
16
|
|
17
|
-
`#calculate_all` tries to mimic magic
|
17
|
+
`#calculate_all` tries to mimic magic of Active Record's `#group`, `#count` and `#pluck`
|
18
18
|
so result type depends on arguments and on groupings.
|
19
19
|
|
20
20
|
### Container
|
@@ -40,6 +40,45 @@ something like this:
|
|
40
40
|
}
|
41
41
|
```
|
42
42
|
|
43
|
+
### Conversion, formatting, value objects
|
44
|
+
|
45
|
+
You can pass block to calculate_all. Rows will be passed to it and returned value will be used instead of
|
46
|
+
row in result hash (or returned as is if there's no grouping)
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Order.group(:country_id).calculate_all(:count, :avg_price) { |count:, avg_price:|
|
50
|
+
"#{count} orders, #{avg_price.to_i} dollars average"
|
51
|
+
}
|
52
|
+
# => {
|
53
|
+
# 1 => "5 orders, 120 dollars average",
|
54
|
+
# 2 => "10 orders, 200 dollars average"
|
55
|
+
# }
|
56
|
+
|
57
|
+
Order.group(:country_id).calculate_all(:avg_price) { |avg_price| avg_price.to_i }
|
58
|
+
# => {
|
59
|
+
# 1 => 120,
|
60
|
+
# 2 => 200
|
61
|
+
# }
|
62
|
+
|
63
|
+
Order.calculate_all(:count, :max_price, &OpenStruct.method(:new))
|
64
|
+
# => #<OpenStruct max_price=500, count=15>
|
65
|
+
```
|
66
|
+
|
67
|
+
## groupdate compatibility
|
68
|
+
|
69
|
+
calculate-all should work with [groupdate](https://github.com/ankane/groupdate) too:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
Model.group_by_year(:created_at, last: 5, default_value: {}).calculate_all(:price_min, :price_max)
|
73
|
+
=> {
|
74
|
+
Sun, 01 Jan 2012 00:00:00 UTC +00:00=>{},
|
75
|
+
Tue, 01 Jan 2013 00:00:00 UTC +00:00=>{},
|
76
|
+
Wed, 01 Jan 2014 00:00:00 UTC +00:00=>{},
|
77
|
+
Thu, 01 Jan 2015 00:00:00 UTC +00:00=>{},
|
78
|
+
Fri, 01 Jan 2016 00:00:00 UTC +00:00=>{:price_min=>100, :price_max=>500}
|
79
|
+
}
|
80
|
+
```
|
81
|
+
|
43
82
|
## Installation
|
44
83
|
|
45
84
|
Add this line to your application's Gemfile:
|
data/calculate-all.gemspec
CHANGED
data/lib/calculate-all.rb
CHANGED
@@ -2,7 +2,7 @@ require "calculate-all/version"
|
|
2
2
|
require "active_record"
|
3
3
|
|
4
4
|
module CalculateAll
|
5
|
-
def calculate_all(*function_aliases, **functions)
|
5
|
+
def calculate_all(*function_aliases, **functions, &block)
|
6
6
|
if function_aliases.size == 1 && functions == {}
|
7
7
|
return_plain_values = true
|
8
8
|
end
|
@@ -12,6 +12,11 @@ module CalculateAll
|
|
12
12
|
if functions == {}
|
13
13
|
raise ArgumentError, "provide at least one function to calculate"
|
14
14
|
end
|
15
|
+
# groupdate compatibility
|
16
|
+
group_values = self.group_values
|
17
|
+
if !group_values.is_a?(Array) && group_values.respond_to?(:relation)
|
18
|
+
group_values = group_values.relation
|
19
|
+
end
|
15
20
|
if functions.size == 1 && group_values.size == 0
|
16
21
|
plain_rows = true
|
17
22
|
end
|
@@ -26,6 +31,8 @@ module CalculateAll
|
|
26
31
|
value = functions.keys.zip(row.last(functions.size)).to_h
|
27
32
|
end
|
28
33
|
|
34
|
+
value = block.call(value) if block
|
35
|
+
|
29
36
|
# Return unwrapped hash directly for scope without any .group()
|
30
37
|
return value if group_values.empty?
|
31
38
|
|
@@ -74,5 +81,9 @@ module CalculateAll
|
|
74
81
|
end
|
75
82
|
end
|
76
83
|
|
77
|
-
|
84
|
+
# should be:
|
85
|
+
# ActiveRecord::Relation.include CalculateAll
|
86
|
+
# including in module instead, for groupdate compatibility
|
87
|
+
ActiveRecord::Calculations.include CalculateAll
|
88
|
+
|
78
89
|
ActiveRecord::Base.extend CalculateAll::Querying
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calculate-all
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- codesnik
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: groupdate
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
97
111
|
description: 'Extends Active Record with #calculate_all method'
|
98
112
|
email:
|
99
113
|
- aronaxis@gmail.com
|
@@ -103,6 +117,7 @@ extra_rdoc_files: []
|
|
103
117
|
files:
|
104
118
|
- ".gitignore"
|
105
119
|
- ".travis.yml"
|
120
|
+
- CHANGELOG.md
|
106
121
|
- Gemfile
|
107
122
|
- LICENSE.txt
|
108
123
|
- README.md
|