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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87793b642f35f1a0ac13c64ccc7b18f4dff5214f
4
- data.tar.gz: 63106f78ba19a0e937389595464fe167cbd4fe78
3
+ metadata.gz: bcc98d3d676bc5db549074f9bd4f3e182f42ecb1
4
+ data.tar.gz: 0ec0592767487a7245339f4c4f7ac0c8a29c22c8
5
5
  SHA512:
6
- metadata.gz: 195576395014c54049f6837ec4e44a4f72ace4b1e97ad4c4523bb85455c12efef59a3af9f5c0969cfb8bed01ce79e4c6c082f4bfc999dd1873682a32d413ebae
7
- data.tar.gz: d94eb40b7d9e876d9c6c9d0fd3be3eb89132dd9c0d01ff2cb950d8887f1b17b9f952f978b568fbd1bd27b52fd418da8aebd0c72aec3133864bcd657ac20b0198
6
+ metadata.gz: ad855ac14b2c81caa8c849f0e9be8b28a8aeae0c87faf47e25a63e5b56739d63cc6c111211e170ec2b9757636d6c11b214ba937da5cd4caf05192f477ed34018
7
+ data.tar.gz: 2b3c94259c9d902722b31f203bb4620800fb863eea0fe12fb8561bb60dfc86177148704bb1b5de02ddd8b0d75290a46ed4b87843ddd2199229c2514579779685
@@ -0,0 +1,8 @@
1
+ ## 0.1.1
2
+
3
+ * groupdate compatibility
4
+ * Use passed block to process values
5
+
6
+ ## 0.1.0
7
+
8
+ * First version
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 level of Active Record's `#group, `#count` and `#pluck`
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:
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "minitest"
27
27
  spec.add_development_dependency "pg"
28
28
  spec.add_development_dependency "mysql2"
29
+ spec.add_development_dependency "groupdate"
29
30
  end
@@ -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
- ActiveRecord::Relation.include CalculateAll
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
@@ -1,3 +1,3 @@
1
1
  module CalculateAll
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.0
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