key-vortex 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f6da23cf8420370cc93a363086dbca57eb2d7302f5536f33d587f0e5a73d4ab
4
- data.tar.gz: 6b0746f97f7a85ac194cda6cd94badf07c9bafc328c72011a4095a61a3c98841
3
+ metadata.gz: a34f3ac120e1d77828821334095974c96448d82df26ab5464370e46bc610adb2
4
+ data.tar.gz: dd955d832e5a15a37362cd8fe536e0851377e7e2fd0812aebd47b64944fc8039
5
5
  SHA512:
6
- metadata.gz: 40516afe2704956138c7946941974b4e7ee6480b696d38388a8519616de9df23bd4b26d2c6b2e1779f73c696808b4426e0c037ada0f5c4a5c4ed04c56df1c701
7
- data.tar.gz: a23ebe43edd781403791da6641e47e9344b8172f71307024bc30a966c0a39797837a02f4a29851ce3e02b2d782afaab22f970c502c2499197c033bdb76623eb6
6
+ metadata.gz: e2ffcd6c05e930f8faba12aacf5ccfc2a004b5e27d8ec0785f0b9c939e61347bec7ff4e339fab98f31b948d966a4722bfd1831bbbc4da53a23d9c0a2ab115c31
7
+ data.tar.gz: 3bc5641c8b9bc9c5e55dc5dbf299e968e9793a53aba261410885d4fda9689829aaa3c052169c7b79367d414a389fc5dd33a36a87e47545869ba3fa10d5023bd6
data/.reek.yml CHANGED
@@ -2,3 +2,8 @@ detectors:
2
2
  # TODO: Remove once design is set
3
3
  IrresponsibleModule:
4
4
  enabled: false
5
+
6
+ DuplicateMethodCall:
7
+ exclude:
8
+ - KeyVortex::Record#self.define_getter
9
+ - KeyVortex::Record#self.define_setter
data/.rubocop.yml CHANGED
@@ -16,3 +16,7 @@ Layout/LineLength:
16
16
  # TODO: Remove once design is set
17
17
  Style/Documentation:
18
18
  Enabled: false
19
+
20
+ Metrics/BlockLength:
21
+ AllowedMethods:
22
+ - describe
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- key-vortex (0.1.3)
4
+ key-vortex (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Guardfile CHANGED
@@ -27,7 +27,7 @@
27
27
  # * 'just' rspec: 'rspec'
28
28
 
29
29
  group :red_green_refactor, halt_on_fail: true do
30
- guard :rspec, cmd: "bundle exec rspec", all_on_pass: true do
30
+ guard :rspec, cmd: "bundle exec rspec", failed_mode: :focus, all_on_pass: true do
31
31
  require "guard/rspec/dsl"
32
32
  dsl = Guard::RSpec::Dsl.new(self)
33
33
 
@@ -12,7 +12,7 @@ class KeyVortex
12
12
  end
13
13
 
14
14
  def save(record)
15
- @items[record.id] = record
15
+ @items[record.key] = record
16
16
  end
17
17
 
18
18
  def find(id)
@@ -8,11 +8,11 @@ class KeyVortex
8
8
  end
9
9
 
10
10
  def within?(constraint)
11
- !applies_to?(constraint) || within_applicable?(constraint)
11
+ constraint.instance_of?(self.class)
12
12
  end
13
13
 
14
14
  def to_s
15
- "#{attribute}: #{value}"
15
+ "#{attribute}: #{limit}"
16
16
  end
17
17
  end
18
18
  end
@@ -5,19 +5,23 @@ require "key_vortex/constraint/base"
5
5
  class KeyVortex
6
6
  class Constraint
7
7
  class Length < KeyVortex::Constraint::Base
8
- attr_reader :value
8
+ attr_reader :limit
9
9
 
10
- def initialize(value)
10
+ def initialize(limit)
11
11
  super()
12
- @value = value
12
+ @limit = limit
13
13
  end
14
14
 
15
15
  def attribute
16
16
  :length
17
17
  end
18
18
 
19
- def within_applicable?(constraint)
20
- value <= constraint.value
19
+ def within?(constraint)
20
+ super && limit <= constraint.limit
21
+ end
22
+
23
+ def accepts?(value)
24
+ value.length <= limit
21
25
  end
22
26
  end
23
27
  end
@@ -5,19 +5,23 @@ require "key_vortex/constraint/base"
5
5
  class KeyVortex
6
6
  class Constraint
7
7
  class Maximum < KeyVortex::Constraint::Base
8
- attr_reader :value
8
+ attr_reader :limit
9
9
 
10
- def initialize(value)
10
+ def initialize(limit)
11
11
  super()
12
- @value = value
12
+ @limit = limit
13
13
  end
14
14
 
15
15
  def attribute
16
16
  :maximum
17
17
  end
18
18
 
19
- def within_applicable?(constraint)
20
- value <= constraint.value
19
+ def within?(constraint)
20
+ super && limit <= constraint.limit
21
+ end
22
+
23
+ def accepts?(value)
24
+ value <= limit
21
25
  end
22
26
  end
23
27
  end
@@ -5,19 +5,23 @@ require "key_vortex/constraint/base"
5
5
  class KeyVortex
6
6
  class Constraint
7
7
  class Minimum < KeyVortex::Constraint::Base
8
- attr_reader :value
8
+ attr_reader :limit
9
9
 
10
- def initialize(value)
10
+ def initialize(limit)
11
11
  super()
12
- @value = value
12
+ @limit = limit
13
13
  end
14
14
 
15
15
  def attribute
16
16
  :maximum
17
17
  end
18
18
 
19
- def within_applicable?(constraint)
20
- value >= constraint.value
19
+ def within?(constraint)
20
+ super && limit >= constraint.limit
21
+ end
22
+
23
+ def accepts?(value)
24
+ value >= limit
21
25
  end
22
26
  end
23
27
  end
@@ -17,9 +17,13 @@ class KeyVortex
17
17
  end)
18
18
  end
19
19
 
20
- def prohibited_by?(adapter)
20
+ def within?(adapter)
21
21
  limitation = adapter.limitation_for(self)
22
- limitation&.prohibits?(self.limitation)
22
+ !limitation || self.limitation.within?(limitation)
23
+ end
24
+
25
+ def accepts?(value)
26
+ limitation.accepts?(value)
23
27
  end
24
28
  end
25
29
  end
@@ -20,22 +20,40 @@ class KeyVortex
20
20
  @constraints += constraints
21
21
  end
22
22
 
23
- def allows?(limitation)
23
+ def encompasses?(limitation)
24
24
  @constraints.all? do |constraint|
25
- limitation.accomodates?(constraint)
25
+ limitation.encompasses_constraint?(constraint)
26
26
  end
27
27
  end
28
28
 
29
- def prohibits?(limitation)
30
- !allows?(limitation)
29
+ def encompasses_constraint?(constraint)
30
+ !applicable_constraints(constraint).select do |con|
31
+ con.within?(constraint)
32
+ end.empty?
33
+ end
34
+
35
+ def within?(limitation)
36
+ limitation.constraints.all? do |constraint|
37
+ within_constraint?(constraint)
38
+ end
31
39
  end
32
40
 
33
- def accomodates?(constraint)
34
- @constraints.all? do |con|
41
+ def within_constraint?(constraint)
42
+ !applicable_constraints(constraint).select do |con|
35
43
  con.within?(constraint)
44
+ end.empty?
45
+ end
46
+
47
+ def applicable_constraints(constraint)
48
+ @constraints.select do |con|
49
+ con.applies_to?(constraint)
36
50
  end
37
51
  end
38
52
 
53
+ def accepts?(value)
54
+ value.is_a?(type) && @constraints.all? { |constraint| constraint.accepts?(value) }
55
+ end
56
+
39
57
  def to_s
40
58
  "Limitation: #{@type}\n\t#{@constraints.join('\n\t')}"
41
59
  end
@@ -19,8 +19,26 @@ class KeyVortex
19
19
  register_field(KeyVortex::Field.new(name, type, **constraints_hash))
20
20
  end
21
21
 
22
+ def self.field_constraints(field)
23
+ @field_hash[field]
24
+ end
25
+
22
26
  def self.register_field(field)
23
27
  field_hash[field.name] = field
28
+ define_getter(field)
29
+ define_setter(field)
30
+ end
31
+
32
+ def self.define_getter(field)
33
+ define_method(field.name) { @values[field.name] }
34
+ end
35
+
36
+ def self.define_setter(field)
37
+ define_method("#{field.name}=") do |val|
38
+ raise KeyVortex::Error, "Invalid value #{val} for #{field.name}" unless field.accepts?(val)
39
+
40
+ @values[field.name] = val
41
+ end
24
42
  end
25
43
 
26
44
  def self.inherited(subclass)
@@ -33,20 +51,11 @@ class KeyVortex
33
51
  # Long enough to accomodate a GUID
34
52
  field :key, String, length: 36
35
53
 
36
- def initialize(fields)
37
- @field_hash = fields
38
- end
39
-
40
- def respond_to_missing?(method, *args)
41
- args.empty? && self.class.field_constraints(method)
42
- end
43
-
44
- def method_missing(method, *_args)
45
- @field_hash[method]
46
- end
47
-
48
- def self.field_constraints(field)
49
- @field_hash[field]
54
+ def initialize(values = {})
55
+ @values = {}
56
+ values.each do |name, value|
57
+ send("#{name}=", value)
58
+ end
50
59
  end
51
60
  end
52
61
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class KeyVortex
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/key_vortex.rb CHANGED
@@ -10,7 +10,7 @@ class KeyVortex
10
10
  @record_class = record_class
11
11
 
12
12
  record_class.fields.each do |field|
13
- next unless field.prohibited_by?(adapter)
13
+ next if field.within?(adapter)
14
14
 
15
15
  raise KeyVortex::Error,
16
16
  "#{adapter.class} can only handle field #{field.name} with these limitations:\n" +
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: key-vortex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lambda Null
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-09 00:00:00.000000000 Z
11
+ date: 2023-07-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Defines abstractions that can be built on top of for key/value storage
14
14
  on different technologies (file, s3, sql, redis, etc.)