mem_db 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0576e79f06874cadbf804ca183629fbe4aaa5bb11f5f428aab5e306a3d713b9
4
- data.tar.gz: f8409a01d2cc4f2e906366dd7743c53b6612b2e8e28db633f883de1c5dd47375
3
+ metadata.gz: ced4d270ba624c4f0c6ef2a8bcda1b7b3f9b600d6840ec05d3f1c0cc5c82f176
4
+ data.tar.gz: f7cd1e3f65c61dcb0eada7840165f09b4028e22af31df2d295f5c4e7d2ee2a6d
5
5
  SHA512:
6
- metadata.gz: b70381b36c4d60d7f807a064c1f368f55d3febdca24ea7d291f33765089197f401b816165d608d467b37327f23c570f80342e4b9d6e390e4a1d380290fc49b46
7
- data.tar.gz: b8ea9ca471be55b1c7c9b695299c03b155f262de90d158442a916430b28d1f961b33678b562770bd3d312e1ed62b4b2c1adc6a2521e139e06cd993708099b8a9
6
+ metadata.gz: 1f2cf1d016537298f4f0ca45b509298dc6098fded738a2018f758c1aec7ab0cfe4b05a031c1ecdf22386864da6cd7e6bff7d2e751cca19e0b778c03da8930fd1
7
+ data.tar.gz: f3a2b7025725d9592fc89d5e85d94b2fe3a2a37c77e1518eeb9f9bb16e3a7db09bd4a676a7013d6153524145cfd2288e799201b912e07073028af3398a5cf441
data/.rspec CHANGED
@@ -1,3 +1,2 @@
1
- --format documentation
2
1
  --color
3
2
  --require spec_helper
data/.rubocop.yml CHANGED
@@ -1,9 +1,12 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.5
3
+ NewCops: enable
3
4
 
4
5
  Style/StringLiterals:
5
- Enabled: true
6
- EnforcedStyle: double_quotes
6
+ Enabled: false
7
+
8
+ Style/QuotedSymbols:
9
+ Enabled: false
7
10
 
8
11
  Style/StringLiteralsInInterpolation:
9
12
  Enabled: true
@@ -41,3 +44,9 @@ Metrics/BlockLength:
41
44
 
42
45
  Metrics/MethodLength:
43
46
  Enabled: false
47
+
48
+ Layout/MultilineMethodCallIndentation:
49
+ EnforcedStyle: indented
50
+
51
+ Style/MultilineBlockChain:
52
+ Enabled: false
data/Gemfile CHANGED
@@ -8,4 +8,7 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
  gem "rspec", "~> 3.0"
10
10
  gem "rubocop", "~> 1.7"
11
+ gem "rubocop-rake"
12
+ gem "rubocop-rspec"
11
13
  gem "ruby-prof", "~> 1.4"
14
+ gem "simplecov"
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mem_db/field"
4
+ require "mem_db/field/matching"
5
+
6
+ class MemDB
7
+ module Field
8
+ class Downcase
9
+ include MemDB::Field
10
+
11
+ def initialize(original)
12
+ @original = original
13
+ end
14
+
15
+ def field
16
+ @original.field
17
+ end
18
+
19
+ def query_field
20
+ @original.query_field
21
+ end
22
+
23
+ def new_matching(value)
24
+ @original.new_matching(value)
25
+ end
26
+
27
+ def field_value(obj)
28
+ @original.field_value(obj).map(&:downcase)
29
+ end
30
+
31
+ def prepare_query(obj)
32
+ @original.prepare_query(obj).map(&:downcase)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -11,28 +11,24 @@ class MemDB
11
11
  class MultiMatching
12
12
  include MemDB::Field::Matching
13
13
 
14
- def initialize(enum, arr)
15
- @enum = enum
14
+ def initialize(arr)
16
15
  @arr = arr
17
16
  end
18
17
 
19
- def match?(query)
20
- @enum.query_value(query).any? do |el|
21
- @arr.include?(el)
22
- end
18
+ def match?(values)
19
+ values.any? { |el| @arr.include?(el) }
23
20
  end
24
21
  end
25
22
 
26
23
  class SingleMatching
27
24
  include MemDB::Field::Matching
28
25
 
29
- def initialize(enum, el)
30
- @enum = enum
26
+ def initialize(el)
31
27
  @el = el
32
28
  end
33
29
 
34
- def match?(query)
35
- @enum.query_value(query).include?(@el)
30
+ def match?(values)
31
+ values.include?(@el)
36
32
  end
37
33
  end
38
34
 
@@ -42,13 +38,11 @@ class MemDB
42
38
  @field = field
43
39
  end
44
40
 
45
- def new_matching(obj)
46
- val = obj[field]
47
-
48
- if val.is_a?(Array)
49
- MultiMatching.new(self, val)
41
+ def new_matching(value)
42
+ if value.is_a?(Array)
43
+ MultiMatching.new(value)
50
44
  else
51
- SingleMatching.new(self, val)
45
+ SingleMatching.new(value)
52
46
  end
53
47
  end
54
48
  end
@@ -3,7 +3,7 @@
3
3
  class MemDB
4
4
  module Field
5
5
  module Matching
6
- def match?(_query)
6
+ def match?(_values)
7
7
  raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
8
8
  end
9
9
  end
@@ -11,7 +11,7 @@ class MemDB
11
11
  class Any
12
12
  include MemDB::Field::Matching
13
13
 
14
- def match?(_query)
14
+ def match?(_values)
15
15
  true
16
16
  end
17
17
  end
@@ -30,15 +30,25 @@ class MemDB
30
30
  @original.query_field
31
31
  end
32
32
 
33
- def new_matching(obj)
34
- v = obj[field]
35
-
36
- if v.nil?
33
+ def new_matching(value)
34
+ if value.nil?
37
35
  ANY_MATCHING
38
36
  else
39
- @original.new_matching(obj)
37
+ @original.new_matching(value)
40
38
  end
41
39
  end
40
+
41
+ def field_value(obj)
42
+ if obj[field].nil?
43
+ nil
44
+ else
45
+ @original.field_value(obj)
46
+ end
47
+ end
48
+
49
+ def prepare_query(obj)
50
+ @original.prepare_query(obj)
51
+ end
42
52
  end
43
53
  end
44
54
  end
@@ -32,8 +32,16 @@ class MemDB
32
32
  @original.query_field
33
33
  end
34
34
 
35
- def new_matching(obj)
36
- NegativeMatching.new(@original.new_matching(obj))
35
+ def new_matching(value)
36
+ NegativeMatching.new(@original.new_matching(value))
37
+ end
38
+
39
+ def field_value(obj)
40
+ @original.field_value(obj)
41
+ end
42
+
43
+ def prepare_query(obj)
44
+ @original.prepare_query(obj)
37
45
  end
38
46
  end
39
47
  end
@@ -65,7 +65,7 @@ class MemDB
65
65
  Prefix.new(source[0..-2])
66
66
  elsif source.start_with?(WILDCARD)
67
67
  Suffix.new(source[1..-1])
68
- else
68
+ else # rubocop:disable Lint/DuplicateBranch
69
69
  Rx.new(source)
70
70
  end
71
71
  end
@@ -78,30 +78,24 @@ class MemDB
78
78
  class MultiMatching
79
79
  include MemDB::Field::Matching
80
80
 
81
- def initialize(f, arr)
82
- @f = f
81
+ def initialize(arr)
83
82
  @patterns = arr.map { |source| Pattern.new(source) }
84
83
  end
85
84
 
86
- def match?(query)
87
- @f.query_value(query).each do |str|
88
- return true if @patterns.any? { |pat| pat.match?(str) }
89
- end
90
-
91
- false
85
+ def match?(values)
86
+ values.any? { |str| @patterns.any? { |pat| pat.match?(str) } }
92
87
  end
93
88
  end
94
89
 
95
90
  class SingleMatching
96
91
  include MemDB::Field::Matching
97
92
 
98
- def initialize(f, el)
99
- @f = f
93
+ def initialize(el)
100
94
  @pat = Pattern.new(el)
101
95
  end
102
96
 
103
- def match?(query)
104
- @f.query_value(query).any? { |str| @pat.match?(str) }
97
+ def match?(values)
98
+ values.any? { |str| @pat.match?(str) }
105
99
  end
106
100
  end
107
101
 
@@ -111,13 +105,11 @@ class MemDB
111
105
  @field = field
112
106
  end
113
107
 
114
- def new_matching(obj)
115
- val = obj[field]
116
-
117
- if val.is_a?(Array)
118
- MultiMatching.new(self, val)
108
+ def new_matching(value)
109
+ if value.is_a?(Array)
110
+ MultiMatching.new(value)
119
111
  else
120
- SingleMatching.new(self, val)
112
+ SingleMatching.new(value)
121
113
  end
122
114
  end
123
115
  end
@@ -9,8 +9,11 @@ class MemDB
9
9
  include MemDB::Field
10
10
 
11
11
  class Rx
12
- def initialize(source)
13
- @rx = ::Regexp.new(source, ::Regexp::MULTILINE)
12
+ def initialize(source, ignore_case:)
13
+ opts = ::Regexp::MULTILINE
14
+ opts |= ::Regexp::IGNORECASE if ignore_case
15
+
16
+ @rx = ::Regexp.new(source, opts)
14
17
  end
15
18
 
16
19
  def match?(str)
@@ -21,46 +24,39 @@ class MemDB
21
24
  class MultiMatching
22
25
  include MemDB::Field::Matching
23
26
 
24
- def initialize(field, arr)
25
- @field = field
26
- @patterns = arr.map { |source| Rx.new(source) }
27
+ def initialize(arr, ignore_case:)
28
+ @patterns = arr.map { |source| Rx.new(source, ignore_case: ignore_case) }
27
29
  end
28
30
 
29
- def match?(query)
30
- @field.query_value(query).each do |str|
31
- return true if @patterns.any? { |pat| pat.match?(str) }
32
- end
33
-
34
- false
31
+ def match?(values)
32
+ values.any? { |str| @patterns.any? { |pat| pat.match?(str) } }
35
33
  end
36
34
  end
37
35
 
38
36
  class SingleMatching
39
37
  include MemDB::Field::Matching
40
38
 
41
- def initialize(field, el)
42
- @field = field
43
- @pat = Rx.new(el)
39
+ def initialize(el, ignore_case:)
40
+ @pat = Rx.new(el, ignore_case: ignore_case)
44
41
  end
45
42
 
46
- def match?(query)
47
- @field.query_value(query).any? { |str| @pat.match?(str) }
43
+ def match?(values)
44
+ values.any? { |str| @pat.match?(str) }
48
45
  end
49
46
  end
50
47
 
51
48
  attr_reader :field
52
49
 
53
- def initialize(field)
50
+ def initialize(field, ignore_case: false)
54
51
  @field = field
52
+ @ignore_case = ignore_case
55
53
  end
56
54
 
57
- def new_matching(obj)
58
- val = obj[field]
59
-
60
- if val.is_a?(Array)
61
- MultiMatching.new(self, val)
55
+ def new_matching(value)
56
+ if value.is_a?(Array)
57
+ MultiMatching.new(value, ignore_case: @ignore_case)
62
58
  else
63
- SingleMatching.new(self, val)
59
+ SingleMatching.new(value, ignore_case: @ignore_case)
64
60
  end
65
61
  end
66
62
  end
data/lib/mem_db/field.rb CHANGED
@@ -10,6 +10,10 @@ class MemDB
10
10
  MemDB::Field::Negative.new(self)
11
11
  end
12
12
 
13
+ def downcase
14
+ MemDB::Field::Downcase.new(self)
15
+ end
16
+
13
17
  def field
14
18
  raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
15
19
  end
@@ -23,11 +27,15 @@ class MemDB
23
27
  @query_field || field
24
28
  end
25
29
 
26
- def new_matching(_obj)
30
+ def new_matching(_value)
27
31
  raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
28
32
  end
29
33
 
30
- def query_value(query)
34
+ def field_value(obj)
35
+ obj[field]
36
+ end
37
+
38
+ def prepare_query(query)
31
39
  query[query_field]
32
40
  end
33
41
  end
data/lib/mem_db/fields.rb CHANGED
@@ -4,13 +4,17 @@ class MemDB
4
4
  class Fields
5
5
  class Matching
6
6
  def initialize(fields, obj)
7
- @matchings = fields.map { |field| field.new_matching(obj) }.reject! do |matching|
8
- matching == MemDB::Field::MayMissing::ANY_MATCHING
9
- end
7
+ @matchings = fields
8
+ .map do |field|
9
+ [field, field.new_matching(field.field_value(obj))]
10
+ end
11
+ .reject! do |_field, matching|
12
+ matching == MemDB::Field::MayMissing::ANY_MATCHING
13
+ end
10
14
  end
11
15
 
12
16
  def match?(query)
13
- @matchings.all? { |matching| matching.match?(query) }
17
+ @matchings.all? { |field, matching| matching.match?(query.field_value(field)) }
14
18
  end
15
19
  end
16
20
 
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MemDB
4
+ module Idx
5
+ class Downcase
6
+ include MemDB::Idx
7
+
8
+ def initialize(original)
9
+ @original = original
10
+ end
11
+
12
+ def field
13
+ @original.field
14
+ end
15
+
16
+ def map_value(str)
17
+ @original.map_value(str.downcase)
18
+ end
19
+
20
+ def map_query(str)
21
+ @original.map_query(str.downcase)
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/mem_db/idx.rb CHANGED
@@ -12,6 +12,10 @@ class MemDB
12
12
  default(MemDB::Idx::ANY)
13
13
  end
14
14
 
15
+ def downcase
16
+ MemDB::Idx::Downcase.new(self)
17
+ end
18
+
15
19
  def field
16
20
  raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
17
21
  end
@@ -46,9 +46,7 @@ class MemDB
46
46
  def prepare_attr(attr)
47
47
  v = @params[attr]
48
48
 
49
- if v == MemDB::Idx::ANY
50
- v
51
- elsif v.nil? || v.is_a?(Array)
49
+ if v == MemDB::Idx::ANY || v.nil? || v.is_a?(Array)
52
50
  v
53
51
  else
54
52
  [v]
data/lib/mem_db/query.rb CHANGED
@@ -6,6 +6,7 @@ class MemDB
6
6
  @params = params
7
7
  @attrs = {}
8
8
  @idx_value = {}
9
+ @field_value = {}
9
10
  end
10
11
 
11
12
  def using(index)
@@ -37,6 +38,14 @@ class MemDB
37
38
  end
38
39
  end
39
40
 
41
+ def field_value(field)
42
+ if @field_value.key?(field)
43
+ @field_value[field]
44
+ else
45
+ @field_value[field] ||= field.prepare_query(self)
46
+ end
47
+ end
48
+
40
49
  def prepare_attr(attr)
41
50
  v = @params[attr]
42
51
  if v.is_a?(Array)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class MemDB
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/mem_db.rb CHANGED
@@ -86,6 +86,7 @@ require_relative "mem_db/idx/reverse"
86
86
  require_relative "mem_db/idx/uniq"
87
87
  require_relative "mem_db/idx/pattern"
88
88
  require_relative "mem_db/idx/default"
89
+ require_relative "mem_db/idx/downcase"
89
90
 
90
91
  require_relative "mem_db/index/bucket"
91
92
  require_relative "mem_db/index/any"
@@ -100,3 +101,4 @@ require_relative "mem_db/field/negative"
100
101
  require_relative "mem_db/field/enum"
101
102
  require_relative "mem_db/field/pattern"
102
103
  require_relative "mem_db/field/regexp"
104
+ require_relative "mem_db/field/downcase"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mem_db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Bochkarev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-23 00:00:00.000000000 Z
11
+ date: 2021-10-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: MemDB is embedded database
14
14
  email:
@@ -30,6 +30,7 @@ files:
30
30
  - lib/mem_db.rb
31
31
  - lib/mem_db/bucket.rb
32
32
  - lib/mem_db/field.rb
33
+ - lib/mem_db/field/downcase.rb
33
34
  - lib/mem_db/field/enum.rb
34
35
  - lib/mem_db/field/matching.rb
35
36
  - lib/mem_db/field/may_missing.rb
@@ -41,6 +42,7 @@ files:
41
42
  - lib/mem_db/idx/bytes.rb
42
43
  - lib/mem_db/idx/chars.rb
43
44
  - lib/mem_db/idx/default.rb
45
+ - lib/mem_db/idx/downcase.rb
44
46
  - lib/mem_db/idx/itself.rb
45
47
  - lib/mem_db/idx/pattern.rb
46
48
  - lib/mem_db/idx/reverse.rb