active_hash 3.1.0 → 3.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +37 -20
- data/README.md +39 -9
- data/active_hash.gemspec +1 -1
- data/lib/active_file/base.rb +2 -2
- data/lib/active_hash/base.rb +18 -8
- data/lib/active_hash/relation.rb +45 -26
- data/lib/active_hash/version.rb +1 -1
- data/lib/active_yaml/aliases.rb +11 -6
- data/lib/active_yaml/base.rb +7 -1
- data/lib/associations/associations.rb +5 -7
- metadata +7 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 819c44015340a15c0fbdd202345818e8fd8a056672e09fe196cf90789fb53f59
|
4
|
+
data.tar.gz: bafc27b0aff5c96b068b3dd58216fdd054fda429c332febeb47b0e9c538a0d2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f729484d6e9cd6c5c20a6a8682a6b6c0a52ae71dbee77c9279c5c88c5206a1504d14bf6a46b795a77e7a982ed13f7e03370f5decc1672ed0f0651781ddc04953
|
7
|
+
data.tar.gz: 59bbb5a4027531f1f5e18bba31ad7dd4723df0e47d1d8b9b9472fa8ccc8f3d765814d937e8651cdbccffc12ee43035efc567b946c00e463b8518d8e2892c3318
|
data/CHANGELOG
CHANGED
@@ -1,41 +1,58 @@
|
|
1
|
+
2022-07-14 (v3.1.1)
|
2
|
+
- Make scopes chainable [#248](https://github.com/active-hash/active_hash/pull/248) @andreynering
|
3
|
+
- Set default key attributes [#251](https://github.com/active-hash/active_hash/pull/251/commits/68a0a121d110ac83f4bbf0024f027714fd24debf) @adampal
|
4
|
+
- Migrate from Travis to GitHub Actions for CI @kbrock
|
5
|
+
- Add primary_key support for has_one [#218](https://github.com/active-hash/active_hash/pull/218) @yujideveloper
|
6
|
+
- Return a chainable relation when using .not [#205](https://github.com/active-hash/active_hash/pull/205) @pfeiffer
|
7
|
+
- Correct fields with YAML aliases in array style [#226](https://github.com/active-hash/active_hash/pull/226) @stomk
|
8
|
+
- Add ActiveHash::Relation#size method for compatibily [#227](https://github.com/active-hash/active_hash/pull/227) @sluceno
|
9
|
+
- Implement ActiveRecord::RecordNotFound interface [#207](https://github.com/active-hash/active_hash/pull/207) @ChrisBr
|
10
|
+
- Fix find_by_id with filter chain [#210](https://github.com/active-hash/active_hash/pull/210) @ChrisBr
|
11
|
+
- Suppress Ruby 2.7 kwargs warnings [#206](https://github.com/active-hash/active_hash/pull/206) @yhirano55
|
12
|
+
- Call reload if @records is not defined [#208](https://github.com/active-hash/active_hash/pull/208) @jonmagic
|
13
|
+
- Switch to rspec3 (and update the Gemfile) [#209](https://github.com/active-hash/active_hash/pull/209) @djberg96
|
14
|
+
- Implement filter by RegEx [#211](https://github.com/active-hash/active_hash/pull/211) @ChrisBr
|
15
|
+
- Supports .pick method [#195](https://github.com/active-hash/active_hash/pull/195/files) @yhirano55
|
16
|
+
- Lots of other small performance improvements, documentation and testing. Thanks to everyone who contributed!
|
17
|
+
|
1
18
|
2020-01-15 (v3.1.0)
|
2
|
-
- Add ActiveHash::Base.order method inspired by ActiveRecord [#177](https://github.com/
|
3
|
-
- Add #to_ary to ActiveHash::Relation [#182](https://github.com/
|
4
|
-
- Allow #find to behave like Enumerable#find if id is nil and a block is given [#183](https://github.com/
|
5
|
-
- Delegate :sample to `records` [#189](https://github.com/
|
19
|
+
- Add ActiveHash::Base.order method inspired by ActiveRecord [#177](https://github.com/active-hash/active_hash/pull/177)
|
20
|
+
- Add #to_ary to ActiveHash::Relation [#182](https://github.com/active-hash/active_hash/pull/182)
|
21
|
+
- Allow #find to behave like Enumerable#find if id is nil and a block is given [#183](https://github.com/active-hash/active_hash/pull/183)
|
22
|
+
- Delegate :sample to `records` [#189](https://github.com/active-hash/active_hash/pull/189)
|
6
23
|
|
7
24
|
2019-09-28 (v3.0.0)
|
8
|
-
- Make #where chainable [#178](https://github.com/
|
25
|
+
- Make #where chainable [#178](https://github.com/active-hash/active_hash/pull/178)
|
9
26
|
|
10
27
|
2019-09-28 (v2.3.0)
|
11
|
-
- Add ::scope method (inspired by ActiveRecord) [#173](https://github.com/
|
12
|
-
- Let `.find(nil)` raise ActiveHash::RecordNotFound (inspired by ActiveRecord) [#174](https://github.com/
|
13
|
-
- `where` clause now works with range argument [#175](https://github.com/
|
28
|
+
- Add ::scope method (inspired by ActiveRecord) [#173](https://github.com/active-hash/active_hash/pull/173)
|
29
|
+
- Let `.find(nil)` raise ActiveHash::RecordNotFound (inspired by ActiveRecord) [#174](https://github.com/active-hash/active_hash/pull/174)
|
30
|
+
- `where` clause now works with range argument [#175](https://github.com/active-hash/active_hash/pull/175)
|
14
31
|
|
15
32
|
2019-03-06 (v2.2.1)
|
16
|
-
- Allow empty YAML [#171](https://github.com/
|
33
|
+
- Allow empty YAML [#171](https://github.com/active-hash/active_hash/pull/171) Thanks, @ppworks
|
17
34
|
|
18
35
|
2018-11-22 (v2.2.0)
|
19
|
-
- Support pluck method [#164](https://github.com/
|
20
|
-
- Support where.not method [#167](https://github.com/
|
36
|
+
- Support pluck method [#164](https://github.com/active-hash/active_hash/pull/164) Thanks, @ihatov08
|
37
|
+
- Support where.not method [#167](https://github.com/active-hash/active_hash/pull/167) Thanks, @DialBird
|
21
38
|
|
22
39
|
2018-04-05 (v2.1.0)
|
23
|
-
- Allow to use ERB (embedded ruby) in yml files [#160](https://github.com/
|
24
|
-
- Add `ActiveHash::Base.polymorphic_name` [#162](https://github.com/
|
25
|
-
- Fix to be able to use enum accessor constant with same name as top-level constant[#161](https://github.com/
|
40
|
+
- Allow to use ERB (embedded ruby) in yml files [#160](https://github.com/active-hash/active_hash/pull/160) Thanks, @UgoMare
|
41
|
+
- Add `ActiveHash::Base.polymorphic_name` [#162](https://github.com/active-hash/active_hash/pull/162)
|
42
|
+
- Fix to be able to use enum accessor constant with same name as top-level constant[#161](https://github.com/active-hash/active_hash/pull/161) Thanks, @yujideveloper
|
26
43
|
|
27
44
|
2018-02-27 (v2.0.0)
|
28
|
-
- Drop old Ruby and Rails support [#157](https://github.com/
|
29
|
-
- Don't generate instance accessors for class attributes [#136](https://github.com/
|
45
|
+
- Drop old Ruby and Rails support [#157](https://github.com/active-hash/active_hash/pull/157)
|
46
|
+
- Don't generate instance accessors for class attributes [#136](https://github.com/active-hash/active_hash/pull/136) Thanks, @rainhead
|
30
47
|
|
31
48
|
2017-06-14 (v1.5.3)
|
32
|
-
- Support symbol values in where and find_by [#156](https://github.com/
|
49
|
+
- Support symbol values in where and find_by [#156](https://github.com/active-hash/active_hash/pull/156) Thanks, @south37
|
33
50
|
|
34
51
|
2017-06-14 (v1.5.2)
|
35
|
-
- Fix find_by when passed an invalid id [#152](https://github.com/
|
52
|
+
- Fix find_by when passed an invalid id [#152](https://github.com/active-hash/active_hash/pull/152) Thanks, @davidstosik
|
36
53
|
|
37
54
|
2017-04-20 (v1.5.1)
|
38
|
-
- Fix a bug on `.where` [#147](https://github.com/
|
55
|
+
- Fix a bug on `.where` [#147](https://github.com/active-hash/active_hash/pull/147)
|
39
56
|
|
40
57
|
2017-03-24 (v1.5.0)
|
41
58
|
- add support for `.find_by!`(@syguer)
|
@@ -123,7 +140,7 @@
|
|
123
140
|
|
124
141
|
2011-01-22
|
125
142
|
- improved method_missing errors for dynamic finders
|
126
|
-
- prevent users from trying to overwrite :attributes (https://github.com/
|
143
|
+
- prevent users from trying to overwrite :attributes (https://github.com/active-hash/active_hash/issues/#issue/33)
|
127
144
|
|
128
145
|
2010-12-08
|
129
146
|
- ruby 1.9.2 compatibility
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ActiveHash
|
2
2
|
|
3
|
-
[![Build Status](https://
|
3
|
+
[![Build Status](https://github.com/active-hash/active_hash/actions/workflows/ruby.yml/badge.svg)](https://github.com/active-hash/active_hash/actions/workflows/ruby.yml)
|
4
4
|
|
5
5
|
ActiveHash is a simple base class that allows you to use a ruby hash as a readonly datasource for an ActiveRecord-like model.
|
6
6
|
|
@@ -14,10 +14,10 @@ ActiveHash also ships with:
|
|
14
14
|
|
15
15
|
* ActiveFile: a base class that you can use to create file data sources
|
16
16
|
* ActiveYaml: a base class that will turn YAML into a hash and load the data into an ActiveHash object
|
17
|
-
|
17
|
+
F
|
18
18
|
## !!! Important notice !!!
|
19
19
|
We have changed returned value to chainable by v3.0.0. It's not just an `Array` instance anymore.
|
20
|
-
If it breaks your application, please report us on [issues](https://github.com/
|
20
|
+
If it breaks your application, please report us on [issues](https://github.com/active-hash/active_hash/issues), and use v2.x.x as following..
|
21
21
|
|
22
22
|
```ruby
|
23
23
|
gem 'active_hash', '~> 2.3.0'
|
@@ -349,7 +349,7 @@ end
|
|
349
349
|
The above example will look for the file "/u/data/sample.yml".
|
350
350
|
|
351
351
|
Since ActiveYaml just creates a hash from the YAML file, you will have all fields specified in YAML auto-defined for you. You can format your YAML as an array, or as a hash:
|
352
|
-
```
|
352
|
+
```yaml
|
353
353
|
# array style
|
354
354
|
- id: 1
|
355
355
|
name: US
|
@@ -369,6 +369,35 @@ mexico:
|
|
369
369
|
id: 3
|
370
370
|
name: Mexico
|
371
371
|
```
|
372
|
+
|
373
|
+
### Automatic Key Attribute
|
374
|
+
|
375
|
+
When using the hash format for your YAML file, ActiveYaml will automatically add a `key` attribute with the name of the object. You can overwrite this by setting the key attribute in the YAML file.
|
376
|
+
For example:
|
377
|
+
```
|
378
|
+
au:
|
379
|
+
id: 1
|
380
|
+
name: Australia
|
381
|
+
```
|
382
|
+
|
383
|
+
When you access the object you can do `Country.find(1).key => 'au'`. Or `Country.find_by_key('au')`
|
384
|
+
|
385
|
+
If you want a different key on only some objects you can mix and match:
|
386
|
+
|
387
|
+
```
|
388
|
+
au:
|
389
|
+
id: 1
|
390
|
+
key: aus
|
391
|
+
name: Australia
|
392
|
+
nz:
|
393
|
+
id: 2
|
394
|
+
name: New Zealand
|
395
|
+
```
|
396
|
+
|
397
|
+
`Country.find(1).key => 'aus'`
|
398
|
+
|
399
|
+
`Country.find(2).key => 'nz'`
|
400
|
+
|
372
401
|
### Multiple files per model
|
373
402
|
|
374
403
|
You can use multiple files to store your data. You will have to choose between hash or array style as you cannot use both for one model.
|
@@ -382,7 +411,7 @@ end
|
|
382
411
|
|
383
412
|
Aliases can be used in ActiveYaml using either array or hash style by including `ActiveYaml::Aliases`.
|
384
413
|
With that module included, keys beginning with a '/' character can be safely added, and will be ignored, allowing you to add aliases anywhere in your code:
|
385
|
-
```
|
414
|
+
```yaml
|
386
415
|
# Array Style
|
387
416
|
- /aliases:
|
388
417
|
soda_flavor: &soda_flavor
|
@@ -408,7 +437,8 @@ coke:
|
|
408
437
|
name: Coke
|
409
438
|
flavor: *soda_flavor
|
410
439
|
price: *soda_price
|
411
|
-
|
440
|
+
```
|
441
|
+
```ruby
|
412
442
|
class Soda < ActiveYaml::Base
|
413
443
|
include ActiveYaml::Aliases
|
414
444
|
end
|
@@ -420,9 +450,9 @@ Soda.first.price # => 1.0
|
|
420
450
|
|
421
451
|
### Using ERB ruby in YAML
|
422
452
|
|
423
|
-
Embedded ruby can
|
453
|
+
Embedded ruby can be used in ActiveYaml using erb brackets `<% %>` and `<%= %>` to set the result of a ruby operation as a value in the yaml file.
|
424
454
|
|
425
|
-
```
|
455
|
+
```yaml
|
426
456
|
- id: 1
|
427
457
|
email: <%= "user#{rand(100)}@email.com" %>
|
428
458
|
password: <%= ENV['USER_PASSWORD'] %>
|
@@ -492,7 +522,7 @@ class Country < ActiveFile::Base
|
|
492
522
|
|
493
523
|
class << self
|
494
524
|
def extension
|
495
|
-
"
|
525
|
+
"super_secret"
|
496
526
|
end
|
497
527
|
|
498
528
|
def load_file
|
data/active_hash.gemspec
CHANGED
@@ -33,7 +33,7 @@ Gem::Specification.new do |s|
|
|
33
33
|
s.email = %q{jeff@zilkey.com}
|
34
34
|
s.summary = %q{An ActiveRecord-like model that uses a hash or file as a datasource}
|
35
35
|
s.description = %q{Includes the ability to specify data using hashes, yml files or JSON files}
|
36
|
-
s.homepage = %q{http://github.com/
|
36
|
+
s.homepage = %q{http://github.com/active-hash/active_hash}
|
37
37
|
s.license = "MIT"
|
38
38
|
|
39
39
|
s.files = [
|
data/lib/active_file/base.rb
CHANGED
@@ -47,9 +47,9 @@ module ActiveFile
|
|
47
47
|
protected :actual_root_path
|
48
48
|
|
49
49
|
[:find, :find_by_id, :all, :where, :method_missing].each do |method|
|
50
|
-
define_method(method) do |*args|
|
50
|
+
define_method(method) do |*args, &block|
|
51
51
|
reload unless data_loaded
|
52
|
-
return super(*args)
|
52
|
+
return super(*args, &block)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
data/lib/active_hash/base.rb
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
module ActiveHash
|
2
|
-
|
3
2
|
class RecordNotFound < StandardError
|
3
|
+
attr_reader :model, :primary_key, :id
|
4
|
+
|
5
|
+
def initialize(message = nil, model = nil, primary_key = nil, id = nil)
|
6
|
+
@primary_key = primary_key
|
7
|
+
@model = model
|
8
|
+
@id = id
|
9
|
+
|
10
|
+
super(message)
|
11
|
+
end
|
4
12
|
end
|
5
13
|
|
6
14
|
class ReservedFieldError < StandardError
|
@@ -14,7 +22,7 @@ module ActiveHash
|
|
14
22
|
|
15
23
|
class Base
|
16
24
|
|
17
|
-
class_attribute :_data, :dirty, :default_attributes
|
25
|
+
class_attribute :_data, :dirty, :default_attributes, :scopes
|
18
26
|
|
19
27
|
class WhereChain
|
20
28
|
def initialize(scope)
|
@@ -30,12 +38,11 @@ module ActiveHash
|
|
30
38
|
ids = @scope.pluck(:id) - Array.wrap(options.delete(:id) || options.delete("id"))
|
31
39
|
candidates = ids.map { |id| @scope.find_by_id(id) }.compact
|
32
40
|
end
|
33
|
-
return candidates if options.blank?
|
34
41
|
|
35
42
|
filtered_records = (candidates || @records || []).reject do |record|
|
36
|
-
match_options?(record, options)
|
43
|
+
options.present? && match_options?(record, options)
|
37
44
|
end
|
38
|
-
|
45
|
+
|
39
46
|
ActiveHash::Relation.new(@scope.klass, filtered_records, {})
|
40
47
|
end
|
41
48
|
|
@@ -188,7 +195,7 @@ module ActiveHash
|
|
188
195
|
ActiveHash::Relation.new(self, @records || [], options[:conditions] || {})
|
189
196
|
end
|
190
197
|
|
191
|
-
delegate :where, :find, :find_by, :find_by!, :find_by_id, :count, :pluck, :first, :last, :order, to: :all
|
198
|
+
delegate :where, :find, :find_by, :find_by!, :find_by_id, :count, :pluck, :ids, :pick, :first, :last, :order, to: :all
|
192
199
|
|
193
200
|
def transaction
|
194
201
|
yield
|
@@ -388,10 +395,13 @@ module ActiveHash
|
|
388
395
|
end
|
389
396
|
|
390
397
|
private :mark_clean
|
391
|
-
|
398
|
+
|
392
399
|
def scope(name, body)
|
393
400
|
raise ArgumentError, 'body needs to be callable' unless body.respond_to?(:call)
|
394
|
-
|
401
|
+
|
402
|
+
self.scopes ||= {}
|
403
|
+
self.scopes[name] = body
|
404
|
+
|
395
405
|
the_meta_class.instance_eval do
|
396
406
|
define_method(name) do |*args|
|
397
407
|
instance_exec(*args, &body)
|
data/lib/active_hash/relation.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
module ActiveHash
|
2
2
|
class Relation
|
3
3
|
include Enumerable
|
4
|
-
|
4
|
+
|
5
5
|
delegate :each, to: :records # Make Enumerable work
|
6
6
|
delegate :equal?, :==, :===, :eql?, :sort!, to: :records
|
7
7
|
delegate :empty?, :length, :first, :second, :third, :last, to: :records
|
8
8
|
delegate :sample, to: :records
|
9
|
-
|
9
|
+
|
10
10
|
def initialize(klass, all_records, query_hash = nil)
|
11
11
|
self.klass = klass
|
12
12
|
self.all_records = all_records
|
@@ -14,15 +14,15 @@ module ActiveHash
|
|
14
14
|
self.records_dirty = false
|
15
15
|
self
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def where(query_hash = :chain)
|
19
19
|
return ActiveHash::Base::WhereChain.new(self) if query_hash == :chain
|
20
|
-
|
20
|
+
|
21
21
|
self.records_dirty = true unless query_hash.nil? || query_hash.keys.empty?
|
22
22
|
self.query_hash.merge!(query_hash || {})
|
23
23
|
self
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def all(options = {})
|
27
27
|
if options.has_key?(:conditions)
|
28
28
|
where(options[:conditions])
|
@@ -30,15 +30,15 @@ module ActiveHash
|
|
30
30
|
where({})
|
31
31
|
end
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def find_by(options)
|
35
35
|
where(options).first
|
36
36
|
end
|
37
37
|
|
38
38
|
def find_by!(options)
|
39
|
-
find_by(options) || (raise RecordNotFound.new("Couldn't find #{klass.name}"))
|
39
|
+
find_by(options) || (raise RecordNotFound.new("Couldn't find #{klass.name}", klass.name))
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def find(id = nil, *args, &block)
|
43
43
|
case id
|
44
44
|
when :all
|
@@ -48,28 +48,42 @@ module ActiveHash
|
|
48
48
|
when Array
|
49
49
|
id.map { |i| find(i) }
|
50
50
|
when nil
|
51
|
-
raise RecordNotFound.new("Couldn't find #{klass.name} without an ID") unless block_given?
|
51
|
+
raise RecordNotFound.new("Couldn't find #{klass.name} without an ID", klass.name, "id") unless block_given?
|
52
52
|
records.find(&block) # delegate to Enumerable#find if a block is given
|
53
53
|
else
|
54
54
|
find_by_id(id) || begin
|
55
|
-
raise RecordNotFound.new("Couldn't find #{klass.name} with ID=#{id}")
|
55
|
+
raise RecordNotFound.new("Couldn't find #{klass.name} with ID=#{id}", klass.name, "id", id)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
def find_by_id(id)
|
61
|
+
return where(id: id).first if query_hash.present?
|
62
|
+
|
61
63
|
index = klass.send(:record_index)[id.to_s] # TODO: Make index in Base publicly readable instead of using send?
|
62
64
|
index and records[index]
|
63
65
|
end
|
64
|
-
|
66
|
+
|
65
67
|
def count
|
66
68
|
length
|
67
69
|
end
|
68
|
-
|
70
|
+
|
71
|
+
def size
|
72
|
+
length
|
73
|
+
end
|
74
|
+
|
69
75
|
def pluck(*column_names)
|
70
76
|
column_names.map { |column_name| all.map(&column_name.to_sym) }.inject(&:zip)
|
71
77
|
end
|
72
|
-
|
78
|
+
|
79
|
+
def ids
|
80
|
+
pluck(:id)
|
81
|
+
end
|
82
|
+
|
83
|
+
def pick(*column_names)
|
84
|
+
pluck(*column_names).first
|
85
|
+
end
|
86
|
+
|
73
87
|
def reload
|
74
88
|
@records = filter_all_records_by_query_hash
|
75
89
|
end
|
@@ -86,50 +100,55 @@ module ActiveHash
|
|
86
100
|
|
87
101
|
candidates
|
88
102
|
end
|
89
|
-
|
103
|
+
|
90
104
|
def to_ary
|
91
105
|
records.dup
|
92
106
|
end
|
93
|
-
|
107
|
+
|
108
|
+
def method_missing(method_name, *args)
|
109
|
+
return super unless self.klass.scopes.key?(method_name)
|
110
|
+
|
111
|
+
instance_exec(*args, &self.klass.scopes[method_name])
|
112
|
+
end
|
94
113
|
|
95
114
|
attr_reader :query_hash, :klass, :all_records, :records_dirty
|
96
|
-
|
115
|
+
|
97
116
|
private
|
98
|
-
|
117
|
+
|
99
118
|
attr_writer :query_hash, :klass, :all_records, :records_dirty
|
100
|
-
|
119
|
+
|
101
120
|
def records
|
102
|
-
if @records.nil? || records_dirty
|
121
|
+
if !defined?(@records) || @records.nil? || records_dirty
|
103
122
|
reload
|
104
123
|
else
|
105
124
|
@records
|
106
125
|
end
|
107
126
|
end
|
108
|
-
|
127
|
+
|
109
128
|
def filter_all_records_by_query_hash
|
110
129
|
self.records_dirty = false
|
111
130
|
return all_records if query_hash.blank?
|
112
|
-
|
131
|
+
|
113
132
|
# use index if searching by id
|
114
133
|
if query_hash.key?(:id) || query_hash.key?("id")
|
115
134
|
ids = (query_hash.delete(:id) || query_hash.delete("id"))
|
116
135
|
ids = range_to_array(ids) if ids.is_a?(Range)
|
117
136
|
candidates = Array.wrap(ids).map { |id| klass.find_by_id(id) }.compact
|
118
137
|
end
|
119
|
-
|
138
|
+
|
120
139
|
return candidates if query_hash.blank?
|
121
140
|
|
122
141
|
(candidates || all_records || []).select do |record|
|
123
142
|
match_options?(record, query_hash)
|
124
143
|
end
|
125
144
|
end
|
126
|
-
|
145
|
+
|
127
146
|
def match_options?(record, options)
|
128
147
|
options.all? do |col, match|
|
129
148
|
if match.kind_of?(Array)
|
130
149
|
match.any? { |v| normalize(v) == normalize(record[col]) }
|
131
150
|
else
|
132
|
-
normalize(
|
151
|
+
normalize(match) === normalize(record[col])
|
133
152
|
end
|
134
153
|
end
|
135
154
|
end
|
@@ -137,7 +156,7 @@ module ActiveHash
|
|
137
156
|
def normalize(v)
|
138
157
|
v.respond_to?(:to_sym) ? v.to_sym : v
|
139
158
|
end
|
140
|
-
|
159
|
+
|
141
160
|
def range_to_array(range)
|
142
161
|
return range.to_a unless range.end.nil?
|
143
162
|
|
data/lib/active_hash/version.rb
CHANGED
data/lib/active_yaml/aliases.rb
CHANGED
@@ -5,6 +5,8 @@ module ActiveYaml
|
|
5
5
|
base.extend(ClassMethods)
|
6
6
|
end
|
7
7
|
|
8
|
+
ALIAS_KEY_REGEXP = /^\//.freeze
|
9
|
+
|
8
10
|
module ClassMethods
|
9
11
|
|
10
12
|
def insert(record)
|
@@ -12,16 +14,19 @@ module ActiveYaml
|
|
12
14
|
end
|
13
15
|
|
14
16
|
def raw_data
|
15
|
-
|
16
|
-
|
17
|
+
d = super
|
18
|
+
if d.kind_of?(Array)
|
19
|
+
d.reject do |h|
|
20
|
+
h.keys.any? { |k| k.match(ALIAS_KEY_REGEXP) }
|
21
|
+
end
|
22
|
+
else
|
23
|
+
d.reject do |k, v|
|
24
|
+
v.kind_of?(Hash) && k.match(ALIAS_KEY_REGEXP)
|
25
|
+
end
|
17
26
|
end
|
18
27
|
end
|
19
28
|
|
20
29
|
end
|
21
|
-
|
22
|
-
def initialize(attributes={})
|
23
|
-
super unless attributes.keys.index { |k| k.to_s.match(/^\//i) }
|
24
|
-
end
|
25
30
|
end
|
26
31
|
|
27
32
|
end
|
data/lib/active_yaml/base.rb
CHANGED
@@ -9,7 +9,7 @@ module ActiveYaml
|
|
9
9
|
if (data = raw_data).is_a?(Array)
|
10
10
|
data
|
11
11
|
elsif data.respond_to?(:values)
|
12
|
-
data.
|
12
|
+
data.map{ |key, value| {"key" => key}.merge(value) }
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -18,9 +18,15 @@ module ActiveYaml
|
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
21
|
+
if Psych::VERSION >= "4.0.0"
|
22
|
+
def load_path(path)
|
23
|
+
YAML.unsafe_load(ERB.new(File.read(path)).result)
|
24
|
+
end
|
25
|
+
else
|
21
26
|
def load_path(path)
|
22
27
|
YAML.load(ERB.new(File.read(path)).result)
|
23
28
|
end
|
29
|
+
end
|
24
30
|
end
|
25
31
|
end
|
26
32
|
end
|
@@ -3,10 +3,7 @@ module ActiveHash
|
|
3
3
|
|
4
4
|
module ActiveRecordExtensions
|
5
5
|
|
6
|
-
def belongs_to(
|
7
|
-
our_args = args.dup
|
8
|
-
options = our_args.extract_options!
|
9
|
-
name = our_args.shift
|
6
|
+
def belongs_to(name, scope = nil, **options)
|
10
7
|
options = {:class_name => name.to_s.camelize }.merge(options)
|
11
8
|
klass =
|
12
9
|
begin
|
@@ -19,7 +16,7 @@ module ActiveHash
|
|
19
16
|
if klass && klass < ActiveHash::Base
|
20
17
|
belongs_to_active_hash(name, options)
|
21
18
|
else
|
22
|
-
super
|
19
|
+
super(name, **options)
|
23
20
|
end
|
24
21
|
end
|
25
22
|
|
@@ -119,7 +116,8 @@ module ActiveHash
|
|
119
116
|
define_method(association_id) do
|
120
117
|
options = {
|
121
118
|
:class_name => association_id.to_s.classify,
|
122
|
-
:foreign_key => self.class.to_s.foreign_key
|
119
|
+
:foreign_key => self.class.to_s.foreign_key,
|
120
|
+
:primary_key => self.class.primary_key
|
123
121
|
}.merge(options)
|
124
122
|
|
125
123
|
scope = options[:class_name].constantize
|
@@ -127,7 +125,7 @@ module ActiveHash
|
|
127
125
|
if scope.respond_to?(:scoped) && options[:conditions]
|
128
126
|
scope = scope.scoped(:conditions => options[:conditions])
|
129
127
|
end
|
130
|
-
scope.send("find_by_#{options[:foreign_key]}",
|
128
|
+
scope.send("find_by_#{options[:foreign_key]}", send(options[:primary_key]))
|
131
129
|
end
|
132
130
|
end
|
133
131
|
|
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: 3.1.
|
4
|
+
version: 3.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Dean
|
@@ -26,10 +26,10 @@ authors:
|
|
26
26
|
- Brett Richardson
|
27
27
|
- Rachel Heaton
|
28
28
|
- Keisuke Izumiya
|
29
|
-
autorequire:
|
29
|
+
autorequire:
|
30
30
|
bindir: bin
|
31
31
|
cert_chain: []
|
32
|
-
date:
|
32
|
+
date: 2022-07-14 00:00:00.000000000 Z
|
33
33
|
dependencies:
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: activesupport
|
@@ -82,11 +82,11 @@ files:
|
|
82
82
|
- lib/active_yaml/base.rb
|
83
83
|
- lib/associations/associations.rb
|
84
84
|
- lib/enum/enum.rb
|
85
|
-
homepage: http://github.com/
|
85
|
+
homepage: http://github.com/active-hash/active_hash
|
86
86
|
licenses:
|
87
87
|
- MIT
|
88
88
|
metadata: {}
|
89
|
-
post_install_message:
|
89
|
+
post_install_message:
|
90
90
|
rdoc_options: []
|
91
91
|
require_paths:
|
92
92
|
- lib
|
@@ -101,9 +101,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
101
|
- !ruby/object:Gem::Version
|
102
102
|
version: '0'
|
103
103
|
requirements: []
|
104
|
-
|
105
|
-
|
106
|
-
signing_key:
|
104
|
+
rubygems_version: 3.2.22
|
105
|
+
signing_key:
|
107
106
|
specification_version: 4
|
108
107
|
summary: An ActiveRecord-like model that uses a hash or file as a datasource
|
109
108
|
test_files: []
|