dddr 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4880615ef28be24c9e1b6db5209b712d81fa8175d969671ef36446f0605fcda8
4
+ data.tar.gz: 77f137fe4546db4d6ed5b71966479703c0ccccf494c83fbe880d337b4c8a9cfa
5
+ SHA512:
6
+ metadata.gz: 802f1e1627a22419f510b9d9bd6f7d030a4de5e904bed64eb4ce6d65212f69e51ead4a600285f72902589185ea1439ced3bcbe873570bc81704e23e0832f141c
7
+ data.tar.gz: c204fe1368f4df604f285519d61f7fe3b79514cbb2f60fc8db475084a67b23f7f05210ffcd292a7d5eae760b3932b7d678e3c42726365041a2e00455cb6cff40
data/.DS_Store ADDED
Binary file
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-10-29
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at delaney@vidtreone.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Delaney Burke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ ![Alt text](hero.png)
2
+
3
+ # DDDr: Domain Driven Design Repository
4
+
5
+ ## Overview
6
+
7
+ "DDDr" is a Ruby gem designed to simplify the implementation of data repositories in a Domain-Driven Design (DDD) architecture. It provides a clean, easy-to-use interface for abstracting data access, so you can focus on domain logic rather than database operations.
8
+
9
+ ## Features
10
+
11
+ * Easy-to-use Entity and Repository classes
12
+ * Built-in UUID generation for entities
13
+ * In-memory storage using SDBM
14
+ * Date and time tracking for entities
15
+ * CRUD operation support
16
+ * Extensible query and association methods
17
+
18
+ ## Installation
19
+
20
+ To install, add the gem to your application's @Gemfile@:
21
+
22
+ <pre>
23
+ gem 'dddr'
24
+ </pre>
25
+
26
+ Then run:
27
+
28
+ <pre>
29
+ bundle install
30
+ </pre>
31
+
32
+ Alternatively, you can install the gem manually:
33
+
34
+ <pre>
35
+ gem install dddr
36
+ </pre>
37
+
38
+ ## Usage
39
+
40
+ ### Creating an Entity
41
+
42
+ Include the @Dddr::Entity@ module in your class:
43
+
44
+ <pre>
45
+ class MyEntity
46
+ include Dddr::Entity
47
+ attr_accessor :name, :email
48
+ end
49
+ </pre>
50
+
51
+ ### Using the Repository
52
+
53
+ <pre>
54
+ repository = MyEntity::Repository.new
55
+ entity = MyEntity.new
56
+ entity.name = "John Doe"
57
+ entity.email = "john.doe@example.com"
58
+
59
+ # Adding the entity
60
+ uid = repository.add(entity)
61
+
62
+ # Updating the entity
63
+ entity.name = "Jane Doe"
64
+ updated_entity = repository.update(entity)
65
+
66
+ # Deleting the entity
67
+ repository.delete(entity)
68
+
69
+ # Fetching an entity by UID
70
+ fetched_entity = repository.get(uid)
71
+ </pre>
72
+
73
+ ### Custom Queries
74
+
75
+ Define custom queries using the @queries@ method within your entity class.
76
+
77
+ <pre>
78
+ class MyEntity
79
+ include Dddr::Entity
80
+ attr_accessor :name, :email
81
+
82
+ queries do
83
+ def find_by_email(email)
84
+ # Custom query logic here
85
+ end
86
+ end
87
+ end
88
+ </pre>
89
+
90
+ You can then execute the custom query like this:
91
+
92
+ <pre>
93
+ repository = MyEntity::Repository.new
94
+ repository.find_by_email("john.doe@example.com")
95
+ </pre>
96
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
data/hero.png ADDED
Binary file
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dddr
4
+ VERSION = "1.0.0"
5
+ end
data/lib/dddr.rb ADDED
@@ -0,0 +1,261 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sdbm"
4
+ require "securerandom"
5
+ require "date"
6
+ require "ostruct"
7
+
8
+ require_relative "dddr/version"
9
+
10
+ module Dddr
11
+
12
+ def self.configuration
13
+ @configuration ||= OpenStruct.new
14
+ end
15
+
16
+ def self.configure
17
+ configuration.env = ENV['DDDR_ENV'] || 'development'
18
+ yield(configuration)
19
+ end
20
+
21
+ class Error < StandardError; end
22
+
23
+ module Entity
24
+ def self.included(base)
25
+ base.extend(ClassMethods)
26
+ end
27
+
28
+ attr_accessor :uid, :created_at, :last_updated_at, :deleted,:deleted_at
29
+
30
+ module ClassMethods
31
+ def queries(&block)
32
+ const_set(:Queries, Module.new(&block))
33
+ self::Repository.include(const_get(:Queries)) if const_defined?(:Repository)
34
+ end
35
+
36
+ def associations(repository, &block)
37
+ const_set(:AssociationMethod, Module.new(&block))
38
+ repository.include(const_get(:AssociationMethod))
39
+ end
40
+
41
+ def const_missing(name)
42
+ @@context = self
43
+
44
+ if defined?(self::Queries)
45
+ @@queries = self::Queries
46
+ end
47
+
48
+ if name == :Repository
49
+
50
+ repository_class = Class.new do
51
+
52
+ def initialize(entity_class = @@context)
53
+ @entity_class = entity_class
54
+ env = Dddr.configuration.env
55
+ `mkdir -p ./.data/#{env}`
56
+ @name = "./.data/#{env}/#{entity_class.name}"
57
+ @cache = nil
58
+ load_cache if @cache.nil?
59
+ end
60
+
61
+ def add(entity)
62
+ uid = SecureRandom.uuid
63
+
64
+ entity.created_at.nil? ? DateTime.now.to_s : entity.created_at
65
+ entity.last_updated_at.nil? ? entity.created_at : entity.last_updated_at
66
+
67
+ @cache << entity
68
+
69
+ SDBM.open(@name) do |db|
70
+ db[uid] = Marshal.dump(entity.to_hash)
71
+ end
72
+ uid
73
+ end
74
+
75
+ alias_method :create, :add
76
+ alias_method :insert, :add
77
+ alias_method :put, :add
78
+ alias_method :append, :add
79
+ alias_method :store, :add
80
+
81
+ def update(entity)
82
+ entity.last_updated_at = DateTime.now.to_s
83
+
84
+ @cache.map! { |e| e.uid == entity.uid ? entity : e }
85
+
86
+ SDBM.open(@name) do |db|
87
+ db[entity.uid] = Marshal.dump(entity.to_hash)
88
+ end
89
+ entity
90
+ end
91
+
92
+ alias_method :modify, :update
93
+ alias_method :change, :update
94
+ alias_method :edit, :update
95
+ alias_method :revise, :update
96
+ alias_method :alter, :update
97
+
98
+ def delete(entity)
99
+ SDBM.open(@name) do |db|
100
+ entity.deleted = true
101
+ entity.deleted_at = DateTime.now
102
+ update(entity)
103
+ end
104
+
105
+ @cache.reject! { |e| e.uid == entity.uid }
106
+
107
+ end
108
+
109
+ def undelete(uid)
110
+ e = bin.find{|e| e.uid == uid}
111
+ e.deleted = false
112
+ e.deleted_at = nil
113
+ update(e)
114
+ end
115
+ # Aliases for 'undelete' method
116
+ alias restore undelete
117
+ alias recover undelete
118
+
119
+
120
+ def undelete_all
121
+ bin.each do |e|
122
+ restore(e.uid)
123
+ end
124
+ end
125
+
126
+ alias restore_all undelete_all
127
+ alias recover_all undelete_all
128
+
129
+
130
+ alias_method :remove, :delete
131
+ alias_method :erase, :delete
132
+ alias_method :discard, :delete
133
+ alias_method :destroy, :delete
134
+ alias_method :wipe, :delete
135
+
136
+ def get(uid)
137
+ all.find { |e| e.uid == uid }
138
+ end
139
+
140
+ alias_method :fetch, :get
141
+ alias_method :retrieve, :get
142
+ alias_method :find, :get
143
+ alias_method :acquire, :get
144
+ alias_method :obtain, :get
145
+
146
+ def reset!
147
+ if Dddr.configuration.env == "test"
148
+ all.map { |e| delete(e) }
149
+ return
150
+ end
151
+ puts "Warning: This will delete all records. Are you sure? (y/n)"
152
+ confirmation = gets.chomp
153
+
154
+ if confirmation.downcase == "y"
155
+ puts "Type 'reset' to confirm."
156
+ final_confirmation = gets.chomp
157
+
158
+ if final_confirmation.downcase == "reset"
159
+ all.map { |e| delete(e) }
160
+ puts "All records have been deleted."
161
+ else
162
+ puts "Reset cancelled."
163
+ end
164
+ else
165
+ puts "Reset cancelled."
166
+ end
167
+ end
168
+
169
+ def count
170
+ all.count
171
+ end
172
+
173
+ def all
174
+ load_cache if @cache.nil?
175
+ @cache
176
+ end
177
+
178
+ def clear_cache
179
+ @cache = nil
180
+ end
181
+
182
+ def refresh_cache
183
+ load_cache
184
+ end
185
+
186
+
187
+ def load_cache
188
+ result = []
189
+ SDBM.open(@name) do |db|
190
+ db.each do |key, value|
191
+ data = Marshal.load(value)
192
+ entity = @entity_class.new
193
+ entity.from_hash(key, data)
194
+ next if entity.deleted
195
+ result << entity
196
+ end
197
+ end
198
+ @cache = result
199
+ end
200
+
201
+ def bin
202
+ result = []
203
+ SDBM.open(@name) do |db|
204
+ db.each do |key, value|
205
+ data = Marshal.load(value)
206
+ entity = @entity_class.new
207
+ entity.from_hash(key, data)
208
+ next unless entity.deleted
209
+ result << entity
210
+ end
211
+ end
212
+ result
213
+ end
214
+
215
+ def empty_bin
216
+ SDBM.open(@name) do |db|
217
+ bin.each do |e|
218
+ db.delete(e.uid)
219
+ end
220
+ end
221
+ end
222
+ end
223
+
224
+ if defined?(@@queries)
225
+ repository_class.include @@queries
226
+ end
227
+
228
+ const_set(name, repository_class)
229
+ else
230
+ super
231
+ end
232
+ end
233
+ end
234
+
235
+ def to_hash
236
+ result = {}
237
+ instance_variables.each do |var_name|
238
+ attribute_name = var_name.to_s[1..].to_sym
239
+ result[attribute_name] = instance_variable_get(var_name)
240
+ end
241
+ result
242
+ end
243
+
244
+ def new?
245
+ return true if created_at.nil?
246
+ false
247
+ end
248
+
249
+ def from_hash(uid, data_hash)
250
+ self.uid = uid
251
+ data_hash.each do |attribute, value|
252
+ attribute = attribute.to_sym
253
+ if respond_to?("#{attribute}=")
254
+ send("#{attribute}=", value)
255
+ end
256
+ end
257
+ end
258
+ end
259
+
260
+ # Your code goes here...
261
+ end
data/sig/dddr.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Dddr
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dddr
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Delaney Burke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sdbm
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: |
28
+ DDDr stands for Domain Driven Design Data Repository. It's a Ruby gem that simplifies the implementation of data repositories in a Domain-Driven Design (DDD) architecture.
29
+ It offers a clean interface for abstracting data access, allowing you to focus on your domain logic rather than database operations.
30
+ With DDDr, you can easily swap out data sources without affecting your core business logic.
31
+ email:
32
+ - delaney@vidtreone.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - ".DS_Store"
38
+ - ".rspec"
39
+ - ".standard.yml"
40
+ - CHANGELOG.md
41
+ - CODE_OF_CONDUCT.md
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - hero.png
46
+ - lib/dddr.rb
47
+ - lib/dddr/version.rb
48
+ - sig/dddr.rbs
49
+ homepage: http://zero2one.ee/gems/dddr
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: http://zero2one.ee/gems/dddr
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.6.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.4.21
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Domain Driven Design Data Repository
73
+ test_files: []