passive_record 0.3.19 → 0.3.20

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
  SHA1:
3
- metadata.gz: 308e4dbc28c56bee86bcd17bfac0e2d88e6e4134
4
- data.tar.gz: 26a64acb50009c2ccc9aced0c5aefef4b46560e4
3
+ metadata.gz: fe98ab7ab71c5ce3976bfc86076c2714714a164d
4
+ data.tar.gz: b239a92752e046940fb7e92d962126bedce4ca94
5
5
  SHA512:
6
- metadata.gz: 06b0384b55def7cab63cd01a7f6b7e142719251885f9c263e845a2ae0bdc503f7300d887fa1149298170aa29477f27fca46de09892c5f32db54146ba440283be
7
- data.tar.gz: 6428e64b00eefb4043e8d21925c7ca39b75e83bd2ab58f86c461f87641f7b852e471ef81e548d93bf746a5d941dcf8f99abf703cc754a0ea7fec39fd62255c6b
6
+ metadata.gz: 61ef80f5bbbfad3e139e6517feac7dc35b804e3c9571986da164e56013d9963b8c2dd103d7fa50ef2d758384b7bf31a882397619654d8626c3b173dfa9857fe9
7
+ data.tar.gz: 8f8fc1772b617aee99cc921a3e6eaded1c5f2ebeeca11b34f48ff6b95479bcecab2cd6702fee06164627c8e575b06a20a37b984dc3acdfc0865bfeffbd0fd173
data/README.md CHANGED
@@ -148,6 +148,10 @@ PassiveRecord may be right for you!
148
148
  - `parent.children.all?(&predicate)`
149
149
  - `parent.children.empty?`
150
150
  - `parent.children.where(conditions)` (returns a `Core::Query`)
151
+ - `parent.children.pluck(attribute)`
152
+ - `parent.children.sum(attribute)`
153
+ - `parent.children.average(attribute)`
154
+ - `parent.children.mode(attribute)`
151
155
 
152
156
  ### Relations
153
157
 
@@ -168,6 +172,10 @@ PassiveRecord may be right for you!
168
172
  - `Post.where(conditions).each` enumerates over `where(conditions).all`, so we have `where(conditions).count`, `where(conditions).first`, etc.
169
173
  - `Post.where(conditions).create(attrs)`
170
174
  - `Post.where(conditions).first_or_create(attrs)`
175
+ - `Post.where(conditions).pluck(attr)`
176
+ - `Post.where(conditions).sum(attr)`
177
+ - `Post.where(conditions).average(attr)`
178
+ - `Post.where(conditions).mode(attr)`
171
179
  - `Post.where(conditions).where(further_conditions)` (chaining)
172
180
  - `Post.where.not(conditions)` (negation)
173
181
  - `Post.where(conditions).or(Post.where(conditions))` (disjunction)
@@ -0,0 +1,21 @@
1
+ module PassiveRecord
2
+ module ArithmeticHelpers
3
+ def pluck(attr)
4
+ all.map(&attr)
5
+ end
6
+
7
+ def sum(attr)
8
+ pluck(attr).inject(&:+)
9
+ end
10
+
11
+ def average(attr)
12
+ sum(attr) / count
13
+ end
14
+
15
+ def mode(attr)
16
+ arr = pluck(attr)
17
+ freq = arr.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
18
+ arr.max_by { |v| freq[v] }
19
+ end
20
+ end
21
+ end
@@ -14,6 +14,8 @@ module PassiveRecord
14
14
  include Enumerable
15
15
  extend Forwardable
16
16
 
17
+ include PassiveRecord::ArithmeticHelpers
18
+
17
19
  def all
18
20
  child_class.where(parent_model_id_field => parent_model.id).all
19
21
  end
@@ -40,24 +42,6 @@ module PassiveRecord
40
42
  all
41
43
  end
42
44
 
43
- def pluck(attr)
44
- all.map(&attr)
45
- end
46
-
47
- def sum(attr)
48
- pluck(attr).inject(&:+)
49
- end
50
-
51
- def average(attr)
52
- sum(attr) / count
53
- end
54
-
55
- def mode(attr)
56
- arr = pluck(attr)
57
- freq = arr.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
58
- arr.max_by { |v| freq[v] }
59
- end
60
-
61
45
  def singular?
62
46
  false
63
47
  end
@@ -3,6 +3,7 @@ module PassiveRecord
3
3
  class Query
4
4
  include Enumerable
5
5
  extend Forwardable
6
+ include PassiveRecord::ArithmeticHelpers
6
7
 
7
8
  def initialize(klass,conditions={},scope=nil)
8
9
  @klass = klass
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.3.19"
3
+ VERSION = "0.3.20"
4
4
  end
@@ -6,6 +6,8 @@ require 'active_support/core_ext/numeric/time'
6
6
 
7
7
  require 'passive_record/version'
8
8
  require 'passive_record/core/identifier'
9
+
10
+ require 'passive_record/arithmetic_helpers'
9
11
  require 'passive_record/core/query'
10
12
 
11
13
  require 'passive_record/class_inheritable_attrs'
@@ -345,10 +345,14 @@ describe "passive record models" do
345
345
  parent.create_child(age: 10)
346
346
  parent.create_child(age: 10)
347
347
  parent.create_child(age: 40)
348
+
348
349
  expect(parent.children.pluck(:age)).to eq([10,10,40])
349
350
  expect(parent.children.sum(:age)).to eq(60)
350
351
  expect(parent.children.average(:age)).to eq(20)
351
352
  expect(parent.children.mode(:age)).to eq(10)
353
+
354
+ expect(parent.children.where(:age => 10).pluck(:age)).to eq([10,10])
355
+ expect(parent.children.where(:age => 10).sum(:age)).to eq(20)
352
356
  end
353
357
  end
354
358
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.19
4
+ version: 0.3.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman
@@ -148,6 +148,7 @@ files:
148
148
  - features/step_definitions/passive_record_steps.rb
149
149
  - gemspec.yml
150
150
  - lib/passive_record.rb
151
+ - lib/passive_record/arithmetic_helpers.rb
151
152
  - lib/passive_record/associations.rb
152
153
  - lib/passive_record/associations/belongs_to.rb
153
154
  - lib/passive_record/associations/has_many.rb