passive_record 0.3.18 → 0.3.19

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: 55612b91f34d4ac833ad318343274c028a7d9db3
4
- data.tar.gz: ee4c132e59ab5c9c2eef23fa7ad6a643b50e9419
3
+ metadata.gz: 308e4dbc28c56bee86bcd17bfac0e2d88e6e4134
4
+ data.tar.gz: 26a64acb50009c2ccc9aced0c5aefef4b46560e4
5
5
  SHA512:
6
- metadata.gz: aad41fefd6877ee5d43bbba3f42efd03c89e86e2600aca84f83a69a66a1186f974beb731c8a2ae2e9aa4de7b5b1958b10df557d95bf00c6b0303cda4bfa664ad
7
- data.tar.gz: e331d278b6dcc08bfdb31312018dc8aa727dc7af6d7dd0a49e7f1205e672c6589fdd7d24672dde8d2bae09c4dee07d60158c5385c9d4a90ce6daf2f512c2952d
6
+ metadata.gz: 06b0384b55def7cab63cd01a7f6b7e142719251885f9c263e845a2ae0bdc503f7300d887fa1149298170aa29477f27fca46de09892c5f32db54146ba440283be
7
+ data.tar.gz: 6428e64b00eefb4043e8d21925c7ca39b75e83bd2ab58f86c461f87641f7b852e471ef81e548d93bf746a5d941dcf8f99abf703cc754a0ea7fec39fd62255c6b
data/README.md CHANGED
@@ -162,15 +162,23 @@ PassiveRecord may be right for you!
162
162
 
163
163
  ### Queries
164
164
 
165
- `Core::Query` objects acquired through `where` are chainable, accept nested queries and scopes, and have the following public methods:
166
-
167
- - `where(conditions).all`
168
- - `where(conditions).each` enumerates over `where(conditions).all`, so we have `where(conditions).count`, `where(conditions).first`, etc.
169
- - `where(conditions).create(attrs)`
170
- - `where(conditions).first_or_create(attrs)`
171
- - `where(conditions).where(further_conditions)` (chaining)
172
- - `where.not(conditions)` (negation)
173
- - `where.scope` (scoping with model class methods that return a query)
165
+ You can acquire `Core::Query` objects through the class method `where`. These are chainable, accept nested conditions that traverse relationships, and understand scopes defined as class methods. The query object will have the following public methods:
166
+
167
+ - `Post.where(conditions).all`
168
+ - `Post.where(conditions).each` enumerates over `where(conditions).all`, so we have `where(conditions).count`, `where(conditions).first`, etc.
169
+ - `Post.where(conditions).create(attrs)`
170
+ - `Post.where(conditions).first_or_create(attrs)`
171
+ - `Post.where(conditions).where(further_conditions)` (chaining)
172
+ - `Post.where.not(conditions)` (negation)
173
+ - `Post.where(conditions).or(Post.where(conditions))` (disjunction)
174
+ - `Post.active.recent` (scoping with class methods that return queries; supports chaining)
175
+
176
+ `conditions` here is expected to be a hash of attribute values. Note that there is special behavior for certain kinds of values.
177
+
178
+ - Ranges select models with an attribute covered by the range (behaving like `BETWEEN`)
179
+ - Arrays select models with an attribute whose value is in the array (behaving like `IN`)
180
+ - Hash values (subhashes) select models with related models who attributes match the inner hash. So `Doctor.where(appointments: { patient: patient })` finds doctors whose appointments include an appointment with `patient`.
181
+
174
182
 
175
183
  ## Hooks
176
184
 
@@ -40,6 +40,24 @@ module PassiveRecord
40
40
  all
41
41
  end
42
42
 
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
+
43
61
  def singular?
44
62
  false
45
63
  end
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.3.18"
3
+ VERSION = "0.3.19"
4
4
  end
@@ -340,6 +340,16 @@ describe "passive record models" do
340
340
  expect(parent.children.all).to eq([child, another_child])
341
341
  expect(parent.child_ids).to eq([child.id, another_child.id])
342
342
  end
343
+
344
+ it 'should provide arithmetic helpers' do
345
+ parent.create_child(age: 10)
346
+ parent.create_child(age: 10)
347
+ parent.create_child(age: 40)
348
+ expect(parent.children.pluck(:age)).to eq([10,10,40])
349
+ expect(parent.children.sum(:age)).to eq(60)
350
+ expect(parent.children.average(:age)).to eq(20)
351
+ expect(parent.children.mode(:age)).to eq(10)
352
+ end
343
353
  end
344
354
 
345
355
  context 'one-to-many through relationships' do
data/spec/spec_helper.rb CHANGED
@@ -47,6 +47,7 @@ module Family
47
47
 
48
48
  attr_reader :name
49
49
  after_create :give_name
50
+ attr_accessor :age
50
51
 
51
52
  def give_name; @name = "Alice" end
52
53
  end
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.18
4
+ version: 0.3.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman