active_hash 2.2.1 → 2.3.0

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
  SHA256:
3
- metadata.gz: 10c4404df7bb2e1659918a3e599661a7639dd0a5ed4404a9934d114aa54c0393
4
- data.tar.gz: 528d4a632658db0ef6f6a35a3abb8783b039b153bd0b728b026d0ce7d6052c98
3
+ metadata.gz: e84505bccf077a25c58e6e57c7fe5ed5e626a0990bf7018711936d4d40374d8a
4
+ data.tar.gz: c4da9c5f5c3af7b5eb55747a91ee292052cef9cb7cfb134337a91d901534dceb
5
5
  SHA512:
6
- metadata.gz: 97f468d94928db136e9d76fd3e44e502e1e77ea2b764c1a070350d08d63fdc8ca524f81786035641681685f506dab94056b07bce05698a0a16a5a9f948e92e08
7
- data.tar.gz: 5361a8ecb16cad65278e096ad58f80103f517043f422d26032c2c213370ebcf369dab9597cba21f372d42ad198d6c22ba06b2a7b611078e78aeb7fe99477f231
6
+ metadata.gz: b03cc2c6bf1826e6e95a4277434127bed837808bcfdf1f82bbd8ad888b05f9d11e248eff8577cd0e9d51c6d2ace1d1e7688be1ddf3e09dcb38bc161b622c81a8
7
+ data.tar.gz: fe93b20edaa8152c0448ce071c9d782edb3ded08b716708309678aba1a33b513c9bb9505693a9751a7497e3cd08f44fb18ec3f314c1d040a2b08262479eb1748
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ 2019-09-28 (v2.3.0)
2
+ - Add ::scope method (inspired by ActiveRecord) [#173](https://github.com/zilkey/active_hash/pull/173)
3
+ - Let `.find(nil)` raise ActiveHash::RecordNotFound (inspired by ActiveRecord) [#174](https://github.com/zilkey/active_hash/pull/174)
4
+ - `where` clause now works with range argument [#175](https://github.com/zilkey/active_hash/pull/175)
5
+
1
6
  2019-03-06 (v2.2.1)
2
7
  - Allow empty YAML [#171](https://github.com/zilkey/active_hash/pull/171) Thanks, @ppworks
3
8
 
data/README.md CHANGED
@@ -169,6 +169,14 @@ Country.find_all_by_name "foo" # => returns an array of the objec
169
169
  Country.find_by_id_and_name 1, "Germany" # => returns the first object matching that id and name
170
170
  Country.find_all_by_id_and_name 1, "Germany" # => returns an array of objects matching that name and id
171
171
  ```
172
+
173
+ Furthermore, it allows to create custom scope query methods, similar to how it's possible with ActiveRecord:
174
+
175
+ ```ruby
176
+ Country.scope :english, -> { where(language: 'English') } # Creates a class method Country.english performing the given query
177
+ Country.scope :with_language, ->(language) { where(language: language) } # Creates a class method Country.with_language(language) performing the given query
178
+ ```
179
+
172
180
  ## Instance Methods
173
181
 
174
182
  ActiveHash objects implement enough of the ActiveRecord api to satisfy most common needs. For example:
@@ -200,6 +200,7 @@ module ActiveHash
200
200
  # use index if searching by id
201
201
  if options.key?(:id) || options.key?("id")
202
202
  ids = (options.delete(:id) || options.delete("id"))
203
+ ids = range_to_array(ids) if ids.is_a?(Range)
203
204
  candidates = Array.wrap(ids).map { |id| find_by_id(id) }.compact
204
205
  end
205
206
  return candidates if options.blank?
@@ -235,6 +236,15 @@ module ActiveHash
235
236
 
236
237
  private :normalize
237
238
 
239
+ def range_to_array(range)
240
+ return range.to_a unless range.end.nil?
241
+
242
+ e = data.last[:id]
243
+ (range.begin..e).to_a
244
+ end
245
+
246
+ private :range_to_array
247
+
238
248
  def count
239
249
  all.length
240
250
  end
@@ -261,14 +271,14 @@ module ActiveHash
261
271
 
262
272
  def find(id, * args)
263
273
  case id
264
- when nil
265
- nil
266
274
  when :all
267
275
  all
268
276
  when :first
269
277
  all(*args).first
270
278
  when Array
271
279
  id.map { |i| find(i) }
280
+ when nil
281
+ raise RecordNotFound.new("Couldn't find #{name} without an ID")
272
282
  else
273
283
  find_by_id(id) || begin
274
284
  raise RecordNotFound.new("Couldn't find #{name} with ID=#{id}")
@@ -465,6 +475,16 @@ module ActiveHash
465
475
  end
466
476
 
467
477
  private :mark_clean
478
+
479
+ def scope(name, body)
480
+ raise ArgumentError, 'body needs to be callable' unless body.respond_to?(:call)
481
+
482
+ the_meta_class.instance_eval do
483
+ define_method(name) do |*args|
484
+ instance_exec(*args, &body)
485
+ end
486
+ end
487
+ end
468
488
 
469
489
  end
470
490
 
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "2.2.1"
3
+ VERSION = "2.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dean
@@ -29,7 +29,7 @@ authors:
29
29
  autorequire:
30
30
  bindir: bin
31
31
  cert_chain: []
32
- date: 2019-03-06 00:00:00.000000000 Z
32
+ date: 2019-09-28 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: activesupport