hashedrecord 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hashedrecord.rb +23 -6
- 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: b8e102a7551d6455e1c4c8520c53e4d88078e32a17ee077d43dee72bed1ba4c7
|
4
|
+
data.tar.gz: a2081f1f61b7e3ba946bc66ed6254ba70841e17728f734aa6daf11b7b8355c55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 308d3ee1b96359140decb31240c84e292fcdfc178755cf3b974b0b5b76f32d6296b09dbfa082d59dbc502352a6fdb2f1c79e5c9d5c61f05bcdb7550f1d4bbca6
|
7
|
+
data.tar.gz: fd6363e07bfa56e89ac40e4900712739e6eaa91d36ae84aa4f889b38ff3baac4289dc664da3360b8cd394b5190febd588ebe1e9c7f4d536d7b7f8c633ce1092f
|
data/lib/hashedrecord.rb
CHANGED
@@ -22,21 +22,22 @@ class HashedRecord
|
|
22
22
|
include Enumerable
|
23
23
|
include Chainable
|
24
24
|
|
25
|
-
def initialize(collection)
|
25
|
+
def initialize(collection, access_method: nil)
|
26
26
|
@collection = collection
|
27
|
-
@collection_ids = collection.
|
27
|
+
@collection_ids = !collection.empty? ? Array(0...collection.size) : []
|
28
28
|
@index = {}
|
29
|
+
@access_method = access_method
|
29
30
|
end
|
30
31
|
|
31
|
-
def call(chain=[])
|
32
|
+
def call(chain = [])
|
32
33
|
chain.inject(collection_ids) do |result, (params, method)|
|
33
34
|
subsets = params.map do |key, value|
|
34
35
|
unless index.key? key
|
35
36
|
index[key] = collection_ids.group_by do |collection_id|
|
36
|
-
collection[collection_id]
|
37
|
+
get_value(collection[collection_id], key)
|
37
38
|
end
|
38
39
|
end
|
39
|
-
|
40
|
+
index[key].slice(*value).values.flatten(1)
|
40
41
|
end
|
41
42
|
subsets.inject(result) do |result, subset|
|
42
43
|
result.send(method, subset)
|
@@ -46,7 +47,23 @@ class HashedRecord
|
|
46
47
|
|
47
48
|
private
|
48
49
|
|
49
|
-
attr_reader :collection_ids, :collection, :index
|
50
|
+
attr_reader :collection_ids, :collection, :index, :access_method
|
51
|
+
|
52
|
+
def get_value(record, key)
|
53
|
+
if @access_method.nil?
|
54
|
+
@access_method = if record.respond_to?(key)
|
55
|
+
->(record, key) { record.send(key) }
|
56
|
+
elsif record.respond_to?(:[]) && record.respond_to?(:key)
|
57
|
+
if record.key? key
|
58
|
+
->(record, key) { record.send(:[], key) }
|
59
|
+
elsif record.key? key.to_s
|
60
|
+
->(record, key) { record.send(:[], key.to_s) }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
raise 'Cannot determinate access method' if @access_method.nil?
|
64
|
+
end
|
65
|
+
access_method.call(record, key)
|
66
|
+
end
|
50
67
|
end
|
51
68
|
|
52
69
|
require 'hashedrecord/filtered'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashedrecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Serg Tyatin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06-
|
11
|
+
date: 2020-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|