ant-storage 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cb612bf83b1e943966b5e0bcb5c6446f8c07aa4c4d3cbb33ba643ad81ea5779c
4
+ data.tar.gz: ab1d3778a052203c30b8ba6f18cef962e2be020fc5c495edf860defa1f884303
5
+ SHA512:
6
+ metadata.gz: 9ce1084361d00bc45187e63cf19740f269f01fd8ef8be2cca60bce453c8c9157195a8b1351b0b2d792a3e9118931313dace50b2ea1d8ef2df7ac24cece2e2c80
7
+ data.tar.gz: d9f58d3fff7c708631b4d7403b89b336ac7c0cb8c5524221e55728112e6f07fdc295d89043361ccaab54443ce542558ce4df85ce49db296460429fbb4a9b7347
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ant/core'
4
+ require_relative 'storage/repository'
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ant/exceptions'
4
+ module Ant
5
+ module Storage
6
+ module Exceptions
7
+ # Exception used when there is an object trying to be created but it
8
+ # already exists.
9
+ class ObjectAlreadyExists < Ant::Exceptions::AntFail
10
+ attr_reader :id
11
+ def initialize(id, object)
12
+ @id = id
13
+ super("Object #{id} already exists", nil, object)
14
+ end
15
+ end
16
+
17
+ # Exception used when a value on a model violates the restrictions
18
+ # from the validators.
19
+ class ValidationErrors < Ant::Exceptions::AntFail
20
+ def initialize(data)
21
+ super('Error while validating object', 'ValidationErrors', data)
22
+ end
23
+ end
24
+
25
+ # Exception used when it is requested an object that can not be found.
26
+ class ObjectNotFound < Ant::Exceptions::AntFail
27
+ attr_reader :id
28
+ def initialize(id)
29
+ @id = id
30
+ super("Object #{id} does not exist", nil, id: id)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ant
4
+ module Storage
5
+ module Datasource
6
+ # This class was meant to handle the complexity of extracting the
7
+ # primary key from an object that belongs to a model. This might get
8
+ # deprecated or suffer a huge refactor.
9
+ class IDGenerators
10
+ extend DRY::ResourceInjector
11
+ def self.[](key)
12
+ resource(:generators, key)
13
+ end
14
+
15
+ def self.normalize_id(object, key)
16
+ object.is_a?(Hash) ? object[key] : object
17
+ end
18
+
19
+ id_generator = ->(id, key) { normalize_id(id, key) }
20
+ register(:generators, :id, id_generator)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'repository'
4
+ require_relative 'id_generators'
5
+ require_relative 'model'
6
+ require 'fileutils'
7
+
8
+ module Ant
9
+ module Storage
10
+ module Datasource
11
+ # Stores objects as a plain json file inside the specified folder.
12
+ # Uses this for testing purpouse.
13
+ class JSONRepository < Repository
14
+ def self.from_config(conf)
15
+ folder = conf['storage'].gsub('$name', conf['schema_name'])
16
+ if conf['schema'].nil?
17
+ # TODO: decouple use of classes
18
+ new(folder, conf['primary_key'].to_sym,
19
+ IDGenerators[:id])
20
+ else
21
+ # TODO: This line is very high coupled to ant-nanoservice
22
+ new(folder, conf['schema']::PRIMARY_KEY, IDGenerators[:id])
23
+ end
24
+ end
25
+
26
+ def initialize(folder, id, id_generator)
27
+ @path = folder
28
+ FileUtils.mkdir_p folder
29
+ super(id, id_generator)
30
+ end
31
+
32
+ def get(id)
33
+ path = full_path(id)
34
+ raise(ObjectNotFound, id) unless File.file?(path)
35
+
36
+ contents = File.read(path)
37
+ JSON.parse(contents, symbolize_names: true)
38
+ end
39
+
40
+ def create_(data)
41
+ store(data)
42
+ data
43
+ end
44
+
45
+ def store(data)
46
+ id = data[@id]
47
+ File.write(full_path(id), data.to_json)
48
+ end
49
+
50
+ private
51
+
52
+ def full_path(id)
53
+ "#{@path}/#{id}.json"
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require_relative 'id_generators'
5
+
6
+ module Ant
7
+ module Storage
8
+ module Datasource
9
+ # Wraps an object inside a model, this will allow to validate that
10
+ # values are inside the restrictions. It dependes on a repository
11
+ # for storing values.
12
+ class Model
13
+ attr_reader :data
14
+ attr_writer :data
15
+
16
+ # :nocov: #
17
+ def run_validations!
18
+ puts 'WARN: model with no validations'
19
+ end
20
+ # :nocov: #
21
+
22
+ def []=(key, val)
23
+ @data[key] = val
24
+ end
25
+
26
+ def [](key)
27
+ @data[key]
28
+ end
29
+
30
+ def store
31
+ run_validations!
32
+ @repository.store(@data)
33
+ end
34
+
35
+ def create
36
+ run_validations!
37
+ @repository.create(@data)
38
+ end
39
+
40
+ def initialize(data)
41
+ @data = data
42
+ end
43
+
44
+ attr_writer :repository
45
+
46
+ def to_json(options = nil)
47
+ @data.to_json(options)
48
+ end
49
+ end
50
+
51
+ # This class is for explicit usage of models without validations
52
+ class EmptyModel < Model
53
+ def run_validations!; end
54
+
55
+ def to_h
56
+ @data
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'exceptions'
4
+
5
+ module Ant
6
+ module Storage
7
+ module Datasource
8
+ # Base class for repositories. The ID generator might get deprecated.
9
+ # It provides the interface for storing objects inside any persistance
10
+ # provider implemented.
11
+ # TODO: Find a better strategy for primary key handling.
12
+ class Repository
13
+ include Exceptions
14
+ def initialize(id, id_generator)
15
+ @id = id
16
+ @id_generator = id_generator
17
+ end
18
+
19
+ def create_initial_object(id)
20
+ object = { @id => @id_generator.call(id, @id) }
21
+ object.merge!(id) if id.is_a?(Hash)
22
+ object
23
+ end
24
+
25
+ def create(id = nil)
26
+ data = create_initial_object(id)
27
+ existent = exist?(data[@id])
28
+ raise(ObjectAlreadyExists.new(data[@id], existent)) if existent
29
+
30
+ create_(data)
31
+ end
32
+
33
+ def exist?(id)
34
+ get(id)
35
+ rescue ObjectNotFound
36
+ nil
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'repository'
4
+
5
+ module Ant
6
+ module Storage
7
+ module Datasource
8
+ # Repository that fetch and store objects using a sequel connection
9
+ # as resource.
10
+ class Sequel < Repository
11
+ def initialize(sequel_object, id, id_generator)
12
+ @sequel = sequel_object
13
+ super(id, id_generator)
14
+ end
15
+
16
+ def get(id)
17
+ result = @sequel.where(@id.to_sym => id).first
18
+ raise(ObjectNotFound, id) if result.nil?
19
+
20
+ result
21
+ end
22
+
23
+ def create_(data)
24
+ id = @sequel.insert(data)
25
+ data[@id] ||= id
26
+ data
27
+ end
28
+
29
+ def store(data)
30
+ data2 = data.dup
31
+ data2.delete(@id)
32
+ @sequel.where(@id => data[@id]).update(data2)
33
+ end
34
+
35
+ def connection
36
+ @sequel
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ant
4
+ module Storage
5
+ # charge of sending objects into storage and fetching also from them.
6
+ class Factory
7
+ include Ant::DRY::ResourceInjector
8
+
9
+ def initialize(model)
10
+ @model = model
11
+ end
12
+
13
+ def create(data, source = resource(:default))
14
+ repository = resource(source)
15
+ model = @model.new(data)
16
+ model.repository = repository
17
+ model.create
18
+ model
19
+ end
20
+
21
+ def get(id, source = resource(:default))
22
+ repository = resource(source)
23
+ data = repository.get(id)
24
+ model = @model.new(data)
25
+ model.repository = repository
26
+ model
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'datasource/json_repository'
4
+ require_relative 'datasource/sequel'
5
+ require_relative 'factory'
6
+
7
+ module Ant
8
+ module Storage
9
+ # Singleton storing all the implemented repositories. It also allows to
10
+ # register new repositories for extension.
11
+ module Repository
12
+ extend Ant::DRY::ResourceInjector
13
+ include Ant::Storage::Datasource
14
+
15
+ register(:repositories, :json, JSONRepository)
16
+ register(:repositories, 'json', JSONRepository)
17
+ register(:repositories, :sequel, Sequel)
18
+ register(:repositories, 'sequel', Sequel)
19
+
20
+ def self.from_config(schema, sets, default)
21
+ conf = default.merge(sets || {}).merge('schema' => schema)
22
+ resource(:repositories, conf['name']).from_config(conf)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ant
4
+ module Storage
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,209 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ant-storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gilberto Vargas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ant-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mocha
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '12.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '12.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rdoc
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '6.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '6.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sequel
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '5.20'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '5.20'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.16'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.16'
139
+ - !ruby/object:Gem::Dependency
140
+ name: sqlite3
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.4'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.4'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.5'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.5'
167
+ description: |2
168
+ This module helps to design persistance modules that are very configurable
169
+ about where and how to store data.
170
+ email:
171
+ - tachoguitar@gmail.com
172
+ executables: []
173
+ extensions: []
174
+ extra_rdoc_files: []
175
+ files:
176
+ - lib/ant/storage.rb
177
+ - lib/ant/storage/datasource/exceptions.rb
178
+ - lib/ant/storage/datasource/id_generators.rb
179
+ - lib/ant/storage/datasource/json_repository.rb
180
+ - lib/ant/storage/datasource/model.rb
181
+ - lib/ant/storage/datasource/repository.rb
182
+ - lib/ant/storage/datasource/sequel.rb
183
+ - lib/ant/storage/factory.rb
184
+ - lib/ant/storage/repository.rb
185
+ - lib/ant/version.rb
186
+ homepage: https://github.com/tachomex/ant
187
+ licenses:
188
+ - MIT
189
+ metadata: {}
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ requirements: []
205
+ rubygems_version: 3.0.3
206
+ signing_key:
207
+ specification_version: 4
208
+ summary: Implements storage modules for ant framework
209
+ test_files: []