kybus-storage 0.1.3 → 0.2.0

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: 267acec6efe373f72b3ad93d62e35024e16488f00d545e7c6397f1d84eeb6aed
4
- data.tar.gz: ffe13306342e8912e4e0099a7a3578bc31ac64c1c16b79e357ba505d7f2f9940
3
+ metadata.gz: 591101eea4d85239c641a75c954b36c887c2a55eaacec2e2715ab7e8ef67be49
4
+ data.tar.gz: 81ed03bd6d576cd6dd724e6f23a444b827f428892c5b0d0b8d190e2f55a267df
5
5
  SHA512:
6
- metadata.gz: c05cca8825043a1b24d795429b40c16bee12a312e14a055df1886d0fdb9fad2322bcd8dff739493eda0abff6a7ea635c7642283f47823a9db36c009d4a422071
7
- data.tar.gz: a15ef29b8950a5c4b94bd6d6ad4b969a17ec480ddbab72aad657d0487f156d5296ac8a26b07d7564b8b09f95dedfc7a868f547cfcf5475627e4c2bb3401b89f3
6
+ metadata.gz: 6ce7b0638fd3f7612bd8c9adbed4797911228c587a2bb21a6c97aa626643abc98ca197532c1b4180dac37822ae86e7660b5e76fe4f8d11689cf3701299b054df
7
+ data.tar.gz: 7e84e7e3f2ee6439f13b81c7da5303db7d507e814f0344b860026b8f9e0a996886e7728e08efd86b98e66c1f3c27bd3f4281fc52212fcdede02f35e4d7340063
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ module Storage
5
+ module Datasource
6
+ # Repository that fetches and stores objects using a Dynamoid connection
7
+ class DynamoidRepository < Repository
8
+ attr_reader :model_class
9
+
10
+ def self.from_config(conf)
11
+ if conf['dynamoid_config']
12
+ require 'dynamoid'
13
+ Dynamoid.configure do |config|
14
+ config.access_key = conf['access_key']
15
+ config.secret_key = conf['secret_key']
16
+ config.region = conf['region']
17
+ config.namespace = conf['namespace']
18
+ config.endpoint = conf['endpoint'] if conf['endpoint']
19
+ end
20
+ end
21
+
22
+ model_class = conf['schema'] || create_dynamic_model(conf)
23
+ new(model_class, conf['primary_key'].to_sym)
24
+ end
25
+
26
+ def self.create_dynamic_model(conf)
27
+ Class.new do
28
+ include Dynamoid::Document
29
+
30
+ table name: conf['table'], key: conf['primary_key'].to_sym, read_capacity: conf['read_capacity'],
31
+ write_capacity: conf['write_capacity']
32
+
33
+ # Dynamically add fields based on configuration
34
+ conf['fields'].each do |field, type|
35
+ field field.to_sym, type.to_sym
36
+ end
37
+
38
+ # Set a constant name for the dynamic class
39
+ class_eval { const_set("DynamicModel#{conf['table'].upcase}", self) }
40
+ end
41
+ end
42
+
43
+ def initialize(model_class, id)
44
+ @model_class = model_class
45
+ super(id, IDGenerators[:id])
46
+ end
47
+
48
+ def get(id)
49
+ result = @model_class.find(id)
50
+ raise(ObjectNotFound, id) if result.nil?
51
+
52
+ result.attributes
53
+ rescue Dynamoid::Errors::RecordNotFound
54
+ raise(ObjectNotFound, id)
55
+ end
56
+
57
+ def create_(data)
58
+ obj = @model_class.create(data)
59
+ obj.attributes
60
+ end
61
+
62
+ def store(data)
63
+ obj = @model_class.find(data[@id])
64
+ raise(ObjectNotFound, data[@id]) if obj.nil?
65
+
66
+ obj.update_attributes(data)
67
+ obj.attributes
68
+ end
69
+
70
+ def connection
71
+ @model_class.connection
72
+ end
73
+ end
74
+ Kybus::Storage::Repository.register(:repositories, :dynamoid, DynamoidRepository)
75
+ Kybus::Storage::Repository.register(:repositories, 'dynamoid', DynamoidRepository)
76
+ end
77
+ end
78
+ end
@@ -8,6 +8,7 @@ module Kybus
8
8
  # already exists.
9
9
  class ObjectAlreadyExists < Kybus::Exceptions::AntFail
10
10
  attr_reader :id
11
+
11
12
  def initialize(id, object)
12
13
  @id = id
13
14
  super("Object #{id} already exists", nil, object)
@@ -25,9 +26,10 @@ module Kybus
25
26
  # Exception used when it is requested an object that can not be found.
26
27
  class ObjectNotFound < Kybus::Exceptions::AntFail
27
28
  attr_reader :id
29
+
28
30
  def initialize(id)
29
31
  @id = id
30
- super("Object #{id} does not exist", nil, id: id)
32
+ super("Object #{id} does not exist", nil, id:)
31
33
  end
32
34
  end
33
35
  end
@@ -10,8 +10,8 @@ module Kybus
10
10
  # values are inside the restrictions. It dependes on a repository
11
11
  # for storing values.
12
12
  class Model
13
- attr_reader :data
14
- attr_writer :data
13
+ attr_accessor :data
14
+ attr_writer :repository
15
15
 
16
16
  # :nocov: #
17
17
  def run_validations!
@@ -41,8 +41,6 @@ module Kybus
41
41
  @data = data
42
42
  end
43
43
 
44
- attr_writer :repository
45
-
46
44
  def to_json(options = nil)
47
45
  @data.to_json(options)
48
46
  end
@@ -35,7 +35,7 @@ module Kybus
35
35
  def exist?(id)
36
36
  # Not the better solution, but works for objects that don't contain
37
37
  # a unique id.
38
- return if id.nil?
38
+ return false if id.nil?
39
39
 
40
40
  get(id)
41
41
  rescue ObjectNotFound
@@ -9,6 +9,7 @@ module Kybus
9
9
  # as resource.
10
10
  class Sequel < Repository
11
11
  def self.from_config(conf)
12
+ require 'sequel'
12
13
  conf['table'] ||= conf['schema_name']
13
14
  conn = ::Sequel.connect(conf['endpoint'], conf)[conf['table'].to_sym]
14
15
  if conf['schema'].nil?
@@ -24,3 +24,5 @@ module Kybus
24
24
  end
25
25
  end
26
26
  end
27
+
28
+ require_relative 'datasource/dynamoid'
data/lib/kybus/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Kybus
4
4
  module Storage
5
- VERSION = '0.1.3'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,169 +1,183 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kybus-storage
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
  - Gilberto Vargas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-15 00:00:00.000000000 Z
11
+ date: 2024-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dynamoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: kybus-core
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - "~>"
31
+ - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: '0.1'
33
+ version: '0'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - "~>"
38
+ - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: '0.1'
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: minitest
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: '5.11'
47
+ version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '5.11'
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: mocha
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - "~>"
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
- version: '1.8'
61
+ version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - "~>"
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
- version: '1.8'
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: pry
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - "~>"
73
+ - - ">="
60
74
  - !ruby/object:Gem::Version
61
- version: '0.12'
75
+ version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - "~>"
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
- version: '0.12'
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rack-minitest
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - "~>"
87
+ - - ">="
74
88
  - !ruby/object:Gem::Version
75
- version: '0.0'
89
+ version: '0'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - "~>"
94
+ - - ">="
81
95
  - !ruby/object:Gem::Version
82
- version: '0.0'
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rake
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - "~>"
101
+ - - ">="
88
102
  - !ruby/object:Gem::Version
89
- version: '12.3'
103
+ version: '0'
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - "~>"
108
+ - - ">="
95
109
  - !ruby/object:Gem::Version
96
- version: '12.3'
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rdoc
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
- - - "~>"
115
+ - - ">="
102
116
  - !ruby/object:Gem::Version
103
- version: '6.1'
117
+ version: '0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
- - - "~>"
122
+ - - ">="
109
123
  - !ruby/object:Gem::Version
110
- version: '6.1'
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: sequel
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
- - - "~>"
129
+ - - ">="
116
130
  - !ruby/object:Gem::Version
117
- version: '5.20'
131
+ version: '0'
118
132
  type: :development
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
- - - "~>"
136
+ - - ">="
123
137
  - !ruby/object:Gem::Version
124
- version: '5.20'
138
+ version: '0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: simplecov
127
141
  requirement: !ruby/object:Gem::Requirement
128
142
  requirements:
129
- - - "~>"
143
+ - - ">="
130
144
  - !ruby/object:Gem::Version
131
- version: '0.16'
145
+ version: '0'
132
146
  type: :development
133
147
  prerelease: false
134
148
  version_requirements: !ruby/object:Gem::Requirement
135
149
  requirements:
136
- - - "~>"
150
+ - - ">="
137
151
  - !ruby/object:Gem::Version
138
- version: '0.16'
152
+ version: '0'
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: sqlite3
141
155
  requirement: !ruby/object:Gem::Requirement
142
156
  requirements:
143
- - - "~>"
157
+ - - ">="
144
158
  - !ruby/object:Gem::Version
145
- version: '1.4'
159
+ version: '0'
146
160
  type: :development
147
161
  prerelease: false
148
162
  version_requirements: !ruby/object:Gem::Requirement
149
163
  requirements:
150
- - - "~>"
164
+ - - ">="
151
165
  - !ruby/object:Gem::Version
152
- version: '1.4'
166
+ version: '0'
153
167
  - !ruby/object:Gem::Dependency
154
168
  name: webmock
155
169
  requirement: !ruby/object:Gem::Requirement
156
170
  requirements:
157
- - - "~>"
171
+ - - ">="
158
172
  - !ruby/object:Gem::Version
159
- version: '3.5'
173
+ version: '0'
160
174
  type: :development
161
175
  prerelease: false
162
176
  version_requirements: !ruby/object:Gem::Requirement
163
177
  requirements:
164
- - - "~>"
178
+ - - ">="
165
179
  - !ruby/object:Gem::Version
166
- version: '3.5'
180
+ version: '0'
167
181
  description: |2
168
182
  This module helps to design persistance modules that are very configurable
169
183
  about where and how to store data.
@@ -174,6 +188,7 @@ extensions: []
174
188
  extra_rdoc_files: []
175
189
  files:
176
190
  - lib/kybus/storage.rb
191
+ - lib/kybus/storage/datasource/dynamoid.rb
177
192
  - lib/kybus/storage/datasource/exceptions.rb
178
193
  - lib/kybus/storage/datasource/id_generators.rb
179
194
  - lib/kybus/storage/datasource/json_repository.rb
@@ -186,7 +201,8 @@ files:
186
201
  homepage: https://github.com/tachomex/kybus
187
202
  licenses:
188
203
  - MIT
189
- metadata: {}
204
+ metadata:
205
+ rubygems_mfa_required: 'true'
190
206
  post_install_message:
191
207
  rdoc_options: []
192
208
  require_paths:
@@ -202,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
218
  - !ruby/object:Gem::Version
203
219
  version: '0'
204
220
  requirements: []
205
- rubygems_version: 3.1.4
221
+ rubygems_version: 3.5.14
206
222
  signing_key:
207
223
  specification_version: 4
208
224
  summary: Implements storage modules for kybus framework