hashedrecord 1.0.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 +7 -0
  2. data/lib/hashedrecord.rb +52 -0
  3. metadata +71 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '089560fa0eda8ecef92f28f200341a8c3c354b465f0530223908cbfa92a65970'
4
+ data.tar.gz: ea768130452876302503b42a82094e785de1dca204a2d0da5c495913a260acce
5
+ SHA512:
6
+ metadata.gz: 21f444096dcc1b34a53e3b45c2765ed0313e653115e0bf9f015805b93753d8a5dc5378d42463815d39caf944322b49b28fde59105a64b00e274a2194c90e9bb1
7
+ data.tar.gz: 80dbf4464d26142b6b8260b9c5bac389c596454b326153e0157774d777c8fb1ecbd5d1ddb97b080da782973a2e13d79e5a6d2bc52cec59783e3e1713a778f7f3
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HashedRecord
4
+ module Chainable
5
+ def where(params)
6
+ Hashedrecord::Filtered.new(self, [params, :&])
7
+ end
8
+
9
+ def not(params)
10
+ Hashedrecord::Filtered.new(self, [params, :-])
11
+ end
12
+
13
+ def each(&block)
14
+ if block_given?
15
+ call.each(&block)
16
+ else
17
+ to_enum(:each)
18
+ end
19
+ end
20
+ end
21
+
22
+ include Enumerable
23
+ include Chainable
24
+
25
+ def initialize(collection)
26
+ @collection = collection
27
+ @collection_ids = collection.size > 0 ? Array(0...collection.size) : []
28
+ @index = {}
29
+ end
30
+
31
+ def call(chain=[])
32
+ chain.inject(collection_ids) do |result, (params, method)|
33
+ subsets = params.map do |key, value|
34
+ unless index.key? key
35
+ index[key] = collection_ids.group_by do |collection_id|
36
+ collection[collection_id].send(key)
37
+ end
38
+ end
39
+ index[key].slice(*value).values.flatten(1)
40
+ end
41
+ subsets.inject(result) do |result, subset|
42
+ result.send(method, subset)
43
+ end
44
+ end.map { |object_id| collection[object_id] }
45
+ end
46
+
47
+ private
48
+
49
+ attr_reader :collection_ids, :collection, :index
50
+ end
51
+
52
+ require 'hashedrecord/filtered'
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashedrecord
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Serg Tyatin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Hashed access to records with ActiveRecord like interface
42
+ email: hashedrecord@2rba.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/hashedrecord.rb
48
+ homepage: https://github.com/2rba/hashedrecord
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.0.8
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: In memory collection filtering
71
+ test_files: []