key-vortex 0.1.1 → 0.1.3

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: 1039a71bf775b3754d1ae275c5d70b39921cb4e390e39158ab4b6cde49fed5c8
4
- data.tar.gz: fa53fb6bd5aeca2c436b162e0c4bb37e7571de24eb279f334790bb239dc00c63
3
+ metadata.gz: 9f6da23cf8420370cc93a363086dbca57eb2d7302f5536f33d587f0e5a73d4ab
4
+ data.tar.gz: 6b0746f97f7a85ac194cda6cd94badf07c9bafc328c72011a4095a61a3c98841
5
5
  SHA512:
6
- metadata.gz: 9b00849471bfd401f4db7ca3ce46ca5a1e47665b94c8d2f29eacba3ff55fb7174058629d82c61034c9eb6f820ee4f89be369e75249fd3f0f764d870a2071dbb2
7
- data.tar.gz: 2ff852525522a69fb833cea7acce7737687f9aa353c9f6c83a23d8abd62ec0dcf2bc1019a94d0a74f0f6f329d2cee3fc90be59636822646d74e5fac0de918110
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.1)
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"
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "key_vortex/adapter"
4
+
5
+ class KeyVortex
6
+ class Adapter
7
+ class Memory < KeyVortex::Adapter
8
+ def initialize(items, limitations: [])
9
+ super()
10
+ @items = items
11
+ limitations.each { |limit| register_limitation(limit) }
12
+ end
13
+
14
+ def save(record)
15
+ @items[record.id] = record
16
+ end
17
+
18
+ def find(id)
19
+ @items[id]
20
+ end
21
+
22
+ def remove(key)
23
+ @items.delete(key)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -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
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "key_vortex"
4
+ require "key_vortex/constraint"
5
+ require "key_vortex/field"
6
+ require "key_vortex/limitation"
7
+
8
+ class KeyVortex
9
+ class Record
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
24
+ end
25
+
26
+ def self.inherited(subclass)
27
+ super
28
+ fields.each do |field|
29
+ subclass.register_field(field)
30
+ end
31
+ end
32
+
33
+ # Long enough to accomodate a GUID
34
+ field :key, String, length: 36
35
+
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]
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class KeyVortex
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/key_vortex.rb CHANGED
@@ -4,5 +4,26 @@ require_relative "key_vortex/version"
4
4
 
5
5
  class KeyVortex
6
6
  class Error < StandardError; end
7
- # Your code goes here...
7
+
8
+ def initialize(adapter, record_class)
9
+ @adapter = adapter
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
20
+ end
21
+
22
+ def save(record)
23
+ @adapter.save(record)
24
+ end
25
+
26
+ def find(id)
27
+ @adapter.find(id)
28
+ end
8
29
  end
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.1
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-07 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,8 +32,16 @@ files:
32
32
  - bin/setup
33
33
  - key-vortex.gemspec
34
34
  - lib/key_vortex.rb
35
- - lib/key_vortex/memory.rb
36
- - lib/key_vortex/record/string.rb
35
+ - lib/key_vortex/adapter.rb
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
44
+ - lib/key_vortex/record.rb
37
45
  - lib/key_vortex/version.rb
38
46
  - sig/record/store.rbs
39
47
  homepage: https://github.com/Lambda-Null/key-vortex
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class KeyVortex
4
- class Memory
5
- def initialize(items)
6
- @items = items
7
- end
8
-
9
- def set(key, item)
10
- @items[key] = item
11
- end
12
-
13
- def get(key)
14
- @items[key]
15
- end
16
-
17
- def remove(key)
18
- @items.delete(key)
19
- end
20
- end
21
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class KeyVortex
4
- class Record
5
- class String
6
- def initialize(value)
7
- @value = value
8
- end
9
- end
10
- end
11
- end