key-vortex 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/Guardfile +1 -1
- data/key-vortex.gemspec +0 -3
- data/lib/key_vortex/adapter/memory.rb +4 -1
- data/lib/key_vortex/adapter.rb +17 -0
- data/lib/key_vortex/constraint/base.rb +19 -0
- data/lib/key_vortex/constraint/length.rb +24 -0
- data/lib/key_vortex/constraint/maximum.rb +24 -0
- data/lib/key_vortex/constraint/minimum.rb +24 -0
- data/lib/key_vortex/constraint.rb +23 -0
- data/lib/key_vortex/field.rb +25 -0
- data/lib/key_vortex/limitation.rb +49 -0
- data/lib/key_vortex/record.rb +29 -8
- data/lib/key_vortex/version.rb +1 -1
- data/lib/key_vortex.rb +9 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ef4caa4048de91fa4f99f928fd154c9ae3950a971fb6d8ad34149070f900914
|
4
|
+
data.tar.gz: 179229a1dd86d3e31fa2dca1d43089ab11000625a5260c5aaf8c31c7a691f3c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c847153af457304066f16e28436a018f289e73b1a929811e506c83d5bfb1da3d45be68a89b6c3781207dd11481753cfd9ec6b95335da58c6a88fc072d65546f
|
7
|
+
data.tar.gz: cfddc7282c2775b03595c679a3369c61584069b2cb961eeb68194ecb9d62fa1c204602611a1be473c410271648bcb3f7fe4b6b8230dacd874e3b2daea66a119a
|
data/Gemfile.lock
CHANGED
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
|
|
data/key-vortex.gemspec
CHANGED
@@ -29,9 +29,6 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
30
|
spec.require_paths = ["lib"]
|
31
31
|
|
32
|
-
# Uncomment to register a new dependency of your gem
|
33
|
-
# spec.add_dependency "example-gem", "~> 1.0"
|
34
|
-
|
35
32
|
# For more information and examples about making a new gem, checkout our
|
36
33
|
# guide at: https://bundler.io/guides/creating_gem.html
|
37
34
|
spec.metadata["rubygems_mfa_required"] = "true"
|
@@ -1,11 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "key_vortex/adapter"
|
4
|
+
|
3
5
|
class KeyVortex
|
4
6
|
class Adapter
|
5
7
|
class Memory < KeyVortex::Adapter
|
6
|
-
def initialize(items)
|
8
|
+
def initialize(items, limitations: [])
|
7
9
|
super()
|
8
10
|
@items = items
|
11
|
+
limitations.each { |limit| register_limitation(limit) }
|
9
12
|
end
|
10
13
|
|
11
14
|
def save(record)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class KeyVortex
|
4
|
+
class Adapter
|
5
|
+
def initialize
|
6
|
+
@limitations = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def limitation_for(field)
|
10
|
+
@limitations[field.limitation.type]
|
11
|
+
end
|
12
|
+
|
13
|
+
def register_limitation(limitation)
|
14
|
+
@limitations[limitation.type] = limitation
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class KeyVortex
|
4
|
+
class Constraint
|
5
|
+
class Base
|
6
|
+
def applies_to?(constraint)
|
7
|
+
attribute == constraint.attribute
|
8
|
+
end
|
9
|
+
|
10
|
+
def within?(constraint)
|
11
|
+
constraint.instance_of?(self.class)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
"#{attribute}: #{value}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "key_vortex/constraint/base"
|
4
|
+
|
5
|
+
class KeyVortex
|
6
|
+
class Constraint
|
7
|
+
class Length < KeyVortex::Constraint::Base
|
8
|
+
attr_reader :value
|
9
|
+
|
10
|
+
def initialize(value)
|
11
|
+
super()
|
12
|
+
@value = value
|
13
|
+
end
|
14
|
+
|
15
|
+
def attribute
|
16
|
+
:length
|
17
|
+
end
|
18
|
+
|
19
|
+
def within?(constraint)
|
20
|
+
super && value <= constraint.value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "key_vortex/constraint/base"
|
4
|
+
|
5
|
+
class KeyVortex
|
6
|
+
class Constraint
|
7
|
+
class Maximum < KeyVortex::Constraint::Base
|
8
|
+
attr_reader :value
|
9
|
+
|
10
|
+
def initialize(value)
|
11
|
+
super()
|
12
|
+
@value = value
|
13
|
+
end
|
14
|
+
|
15
|
+
def attribute
|
16
|
+
:maximum
|
17
|
+
end
|
18
|
+
|
19
|
+
def within?(constraint)
|
20
|
+
super && value <= constraint.value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "key_vortex/constraint/base"
|
4
|
+
|
5
|
+
class KeyVortex
|
6
|
+
class Constraint
|
7
|
+
class Minimum < KeyVortex::Constraint::Base
|
8
|
+
attr_reader :value
|
9
|
+
|
10
|
+
def initialize(value)
|
11
|
+
super()
|
12
|
+
@value = value
|
13
|
+
end
|
14
|
+
|
15
|
+
def attribute
|
16
|
+
:maximum
|
17
|
+
end
|
18
|
+
|
19
|
+
def within?(constraint)
|
20
|
+
super && value >= constraint.value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "key_vortex/constraint/base"
|
4
|
+
require "key_vortex/constraint/length"
|
5
|
+
require "key_vortex/constraint/maximum"
|
6
|
+
require "key_vortex/constraint/minimum"
|
7
|
+
|
8
|
+
class KeyVortex
|
9
|
+
class Constraint
|
10
|
+
def self.build(attribute, value)
|
11
|
+
case attribute
|
12
|
+
when :length
|
13
|
+
KeyVortex::Constraint::Length.new(value)
|
14
|
+
when :maximum
|
15
|
+
KeyVortex::Constraint::Maximum.new(value)
|
16
|
+
when :minimum
|
17
|
+
KeyVortex::Constraint::Minimum.new(value)
|
18
|
+
else
|
19
|
+
raise KeyVortex::Error, "Unexpected attribute: #{attribute}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "key_vortex/constraint"
|
4
|
+
require "key_vortex/limitation"
|
5
|
+
|
6
|
+
class KeyVortex
|
7
|
+
class Field
|
8
|
+
attr_reader :name, :limitation
|
9
|
+
|
10
|
+
def initialize(name, type, *constraints_array, **constraints_hash)
|
11
|
+
@name = name
|
12
|
+
@limitation = KeyVortex::Limitation.new(type)
|
13
|
+
|
14
|
+
@limitation.add_constraint(*constraints_array)
|
15
|
+
@limitation.add_constraint(*constraints_hash.map do |attribute, value|
|
16
|
+
KeyVortex::Constraint.build(attribute, value)
|
17
|
+
end)
|
18
|
+
end
|
19
|
+
|
20
|
+
def prohibited_by?(adapter)
|
21
|
+
limitation = adapter.limitation_for(self)
|
22
|
+
limitation&.prohibits?(self.limitation)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class KeyVortex
|
4
|
+
class Limitation
|
5
|
+
attr_reader :type, :constraints
|
6
|
+
|
7
|
+
def initialize(type, *constraints)
|
8
|
+
@type = type
|
9
|
+
@constraints = constraints
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_constraint(*constraints)
|
13
|
+
constraints.each do |constraint|
|
14
|
+
unless constraint.is_a?(KeyVortex::Constraint::Base)
|
15
|
+
raise KeyVortex::Error,
|
16
|
+
"Not a constraint: #{constraint.class}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
@constraints += constraints
|
21
|
+
end
|
22
|
+
|
23
|
+
def allows?(limitation)
|
24
|
+
@constraints.all? do |constraint|
|
25
|
+
limitation.accomodates?(constraint)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def prohibits?(limitation)
|
30
|
+
!allows?(limitation)
|
31
|
+
end
|
32
|
+
|
33
|
+
def applicable_constraints(constraint)
|
34
|
+
@constraints.select do |con|
|
35
|
+
con.applies_to?(constraint)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def accomodates?(constraint)
|
40
|
+
!applicable_constraints(constraint).select do |con|
|
41
|
+
con.within?(constraint)
|
42
|
+
end.empty?
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
"Limitation: #{@type}\n\t#{@constraints.join('\n\t')}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/key_vortex/record.rb
CHANGED
@@ -1,19 +1,40 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "key_vortex"
|
4
|
+
require "key_vortex/constraint"
|
5
|
+
require "key_vortex/field"
|
6
|
+
require "key_vortex/limitation"
|
7
|
+
|
3
8
|
class KeyVortex
|
4
9
|
class Record
|
5
|
-
def self.
|
6
|
-
|
10
|
+
def self.fields
|
11
|
+
field_hash.values
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.field_hash
|
15
|
+
@field_hash ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.field(name, type, **constraints_hash)
|
19
|
+
register_field(KeyVortex::Field.new(name, type, **constraints_hash))
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.register_field(field)
|
23
|
+
field_hash[field.name] = field
|
7
24
|
end
|
8
25
|
|
9
|
-
def self.
|
10
|
-
|
26
|
+
def self.inherited(subclass)
|
27
|
+
super
|
28
|
+
fields.each do |field|
|
29
|
+
subclass.register_field(field)
|
30
|
+
end
|
11
31
|
end
|
12
32
|
|
13
|
-
|
33
|
+
# Long enough to accomodate a GUID
|
34
|
+
field :key, String, length: 36
|
14
35
|
|
15
36
|
def initialize(fields)
|
16
|
-
@
|
37
|
+
@field_hash = fields
|
17
38
|
end
|
18
39
|
|
19
40
|
def respond_to_missing?(method, *args)
|
@@ -21,11 +42,11 @@ class KeyVortex
|
|
21
42
|
end
|
22
43
|
|
23
44
|
def method_missing(method, *_args)
|
24
|
-
@
|
45
|
+
@field_hash[method]
|
25
46
|
end
|
26
47
|
|
27
48
|
def self.field_constraints(field)
|
28
|
-
@
|
49
|
+
@field_hash[field]
|
29
50
|
end
|
30
51
|
end
|
31
52
|
end
|
data/lib/key_vortex/version.rb
CHANGED
data/lib/key_vortex.rb
CHANGED
@@ -8,6 +8,15 @@ class KeyVortex
|
|
8
8
|
def initialize(adapter, record_class)
|
9
9
|
@adapter = adapter
|
10
10
|
@record_class = record_class
|
11
|
+
|
12
|
+
record_class.fields.each do |field|
|
13
|
+
next unless field.prohibited_by?(adapter)
|
14
|
+
|
15
|
+
raise KeyVortex::Error,
|
16
|
+
"#{adapter.class} can only handle field #{field.name} with these limitations:\n" +
|
17
|
+
adapter.limitation_for(field).to_s +
|
18
|
+
"\n\nThe following record violates these limitations:\n#{field.limitation}"
|
19
|
+
end
|
11
20
|
end
|
12
21
|
|
13
22
|
def save(record)
|
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.
|
4
|
+
version: 0.1.4
|
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-
|
11
|
+
date: 2023-07-09 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.)
|
@@ -32,7 +32,15 @@ files:
|
|
32
32
|
- bin/setup
|
33
33
|
- key-vortex.gemspec
|
34
34
|
- lib/key_vortex.rb
|
35
|
+
- lib/key_vortex/adapter.rb
|
35
36
|
- lib/key_vortex/adapter/memory.rb
|
37
|
+
- lib/key_vortex/constraint.rb
|
38
|
+
- lib/key_vortex/constraint/base.rb
|
39
|
+
- lib/key_vortex/constraint/length.rb
|
40
|
+
- lib/key_vortex/constraint/maximum.rb
|
41
|
+
- lib/key_vortex/constraint/minimum.rb
|
42
|
+
- lib/key_vortex/field.rb
|
43
|
+
- lib/key_vortex/limitation.rb
|
36
44
|
- lib/key_vortex/record.rb
|
37
45
|
- lib/key_vortex/version.rb
|
38
46
|
- sig/record/store.rbs
|