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 +4 -4
- data/CHANGELOG +5 -0
- data/README.md +8 -0
- data/lib/active_hash/base.rb +22 -2
- data/lib/active_hash/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e84505bccf077a25c58e6e57c7fe5ed5e626a0990bf7018711936d4d40374d8a
|
4
|
+
data.tar.gz: c4da9c5f5c3af7b5eb55747a91ee292052cef9cb7cfb134337a91d901534dceb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
data/lib/active_hash/base.rb
CHANGED
@@ -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
|
|
data/lib/active_hash/version.rb
CHANGED
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.
|
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-
|
32
|
+
date: 2019-09-28 00:00:00.000000000 Z
|
33
33
|
dependencies:
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: activesupport
|