hashedrecord 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hashedrecord.rb +23 -6
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fea274de40c99fb8a717090c5fe98ff9019192142015475dfd5880b74a419355
4
- data.tar.gz: 711b012341f2d721aa496766f7668d42c3f4146f6d82f7a1a134ac181ac8df67
3
+ metadata.gz: b8e102a7551d6455e1c4c8520c53e4d88078e32a17ee077d43dee72bed1ba4c7
4
+ data.tar.gz: a2081f1f61b7e3ba946bc66ed6254ba70841e17728f734aa6daf11b7b8355c55
5
5
  SHA512:
6
- metadata.gz: 481f7e50663683ce02677ec2778e5d977bc15ea7aab6dbcd0d70e3719eaabb985d364624bffec1aec78ecb223e24950996122fdd58784c268ebc9175924f6583
7
- data.tar.gz: b47c1fc38f76cb60ab7ea819229c8faf8c47911080eee3e2919cd6c020cae944dec1c7588f0f86b9b55f819b761dbb30f63c407f7b2cfe00dfabc98c3c475967
6
+ metadata.gz: 308d3ee1b96359140decb31240c84e292fcdfc178755cf3b974b0b5b76f32d6296b09dbfa082d59dbc502352a6fdb2f1c79e5c9d5c61f05bcdb7550f1d4bbca6
7
+ data.tar.gz: fd6363e07bfa56e89ac40e4900712739e6eaa91d36ae84aa4f889b38ff3baac4289dc664da3360b8cd394b5190febd588ebe1e9c7f4d536d7b7f8c633ce1092f
@@ -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.size > 0 ? Array(0...collection.size) : []
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].send(key)
37
+ get_value(collection[collection_id], key)
37
38
  end
38
39
  end
39
- index[key].slice(*value).values.flatten(1)
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.1
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-13 00:00:00.000000000 Z
11
+ date: 2020-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec