key-vortex 0.1.2 → 0.1.3

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: f16908e843138ceefe28247b31cc1fe6b0a6e4c704174f7af7f98bcf8ac433dd
4
- data.tar.gz: 759988a8108b14ca1be504c1a6eeb102b1efc86b970b33c6078fa4290963b67b
3
+ metadata.gz: 9f6da23cf8420370cc93a363086dbca57eb2d7302f5536f33d587f0e5a73d4ab
4
+ data.tar.gz: 6b0746f97f7a85ac194cda6cd94badf07c9bafc328c72011a4095a61a3c98841
5
5
  SHA512:
6
- metadata.gz: 233359a3b6e85fe314abed09916491de40596ca47ac927dc18587970059c2470ef7071d6346baf67893e14416d041147130e62d2f4698011b81813b951225025
7
- data.tar.gz: e4fc5a84f2726d24cbf2362890eb7dcfd2da5a67ad32e8982b73961f8acb0b34a94ff982a0075aa7a119008ead364bfa57608616729875801ac1efa0df44e332
6
+ metadata.gz: 40516afe2704956138c7946941974b4e7ee6480b696d38388a8519616de9df23bd4b26d2c6b2e1779f73c696808b4426e0c037ada0f5c4a5c4ed04c56df1c701
7
+ data.tar.gz: a23ebe43edd781403791da6641e47e9344b8172f71307024bc30a966c0a39797837a02f4a29851ce3e02b2d782afaab22f970c502c2499197c033bdb76623eb6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- key-vortex (0.1.2)
4
+ key-vortex (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
+ !applies_to?(constraint) || within_applicable?(constraint)
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_applicable?(constraint)
20
+ 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_applicable?(constraint)
20
+ 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_applicable?(constraint)
20
+ 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,43 @@
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 accomodates?(constraint)
34
+ @constraints.all? do |con|
35
+ con.within?(constraint)
36
+ end
37
+ end
38
+
39
+ def to_s
40
+ "Limitation: #{@type}\n\t#{@constraints.join('\n\t')}"
41
+ end
42
+ end
43
+ end
@@ -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.constraints
6
- @constraints ||= {}
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.field(type, **constraints)
10
- self.constraints[type] = constraints
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
- field :id
33
+ # Long enough to accomodate a GUID
34
+ field :key, String, length: 36
14
35
 
15
36
  def initialize(fields)
16
- @fields = fields
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
- @fields[method]
45
+ @field_hash[method]
25
46
  end
26
47
 
27
48
  def self.field_constraints(field)
28
- @fields[field]
49
+ @field_hash[field]
29
50
  end
30
51
  end
31
52
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class KeyVortex
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
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.2
4
+ version: 0.1.3
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-08 00:00:00.000000000 Z
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