dddr 1.1.4 → 2.0.0

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: a61d06c6ebc3616c3da5cb9529144c21227a87c9cc16066c3d30676f689d6315
4
- data.tar.gz: e3d69c17b85e80c0c8b78ed560c1921b4ccddb766908e2798b5fab10c998a009
3
+ metadata.gz: ee1b9dbc0e1cef170370b086fd935ff099f87efd5378b89afa82ba2ae0f0e814
4
+ data.tar.gz: f38d34f8603a8912f6c27bd888ae557b17676b8f741122fe97b05c2111337c4c
5
5
  SHA512:
6
- metadata.gz: 3439a0a2a18760fdc510927d336e3e42b17a1cb933f602f21e7ea938f60c8d8bcd4585f54d2f7fd44bcd9f12cee428e83c7b2b791aa7ea4b7b056035dd60ebbc
7
- data.tar.gz: 7a0bfeb34044c07b29a584f7000be838f0f2b48a0bc98d08e815f112ba4baf9b65b326e6c24627ae77f4ab6e90d811488a73e4ef885dc4b14d19c6d1dcc9d513
6
+ metadata.gz: 6869b30e4f3bee841144c6477426fee7234110ea4b6c3989c8bc089f656fb3927005323b914c954692fb07c11d474c0773a2925e6c2c1e97e0633ab066f9827e
7
+ data.tar.gz: 91660579732ca553de594fc54758ae83ecb2025815c10c688dd8e25befae082aee870a59c061bea9beae6b27c9e3fb090d9881f021c8ebf72f43910ae02016cc
data/lib/dddr/sdbm.rb ADDED
@@ -0,0 +1,171 @@
1
+ module Dddr
2
+ module Sdbm
3
+ module ClassMethods
4
+ def queries(&block)
5
+ @queries_module ||= Module.new
6
+ @queries_module.module_eval(&block)
7
+ end
8
+
9
+ def const_missing(name)
10
+ if name == :Repository
11
+ create_repository_for(self)
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def create_repository_for(entity_class)
20
+ repository_class = Class.new(RepositoryBase) do
21
+ define_singleton_method(:new) do |*args|
22
+ super(entity_class)
23
+ end
24
+ end
25
+
26
+ repository_class.include(@queries_module) if instance_variable_defined?(:@queries_module)
27
+
28
+ const_set(:Repository, repository_class)
29
+ end
30
+ end
31
+
32
+ class RepositoryBase
33
+ def initialize(entity_class)
34
+ @entity_class = entity_class
35
+ setup_data_directory(entity_class)
36
+ @serializer = Dddr::Serializer.new(Dddr.configuration.serializer)
37
+ end
38
+
39
+ def setup_data_directory(entity_class)
40
+ env = Dddr.configuration.env
41
+ data_dir = Dddr.configuration.data_dir
42
+ container = Dddr.configuration.container
43
+ raise Dddr::Error, "Container name is required" unless container
44
+
45
+ # Construct the target directory path
46
+ @data_dir = "#{data_dir}/#{container}/#{env}/#{entity_class.name.downcase}"
47
+
48
+ begin
49
+ # Ensure all directories in the path are created
50
+ FileUtils.mkdir_p(@data_dir)
51
+
52
+ # Change ownership only if the path starts with "/var/"
53
+ if @data_dir.start_with?("/var/")
54
+ FileUtils.chown_R(ENV["USER"], nil, @data_dir)
55
+ end
56
+ rescue Errno::ENOENT => e
57
+ # Log or handle the error
58
+ raise "Failed to create or access the directory: #{e.message}"
59
+ end
60
+ end
61
+
62
+ def count
63
+ all.count
64
+ end
65
+
66
+ def to_csv
67
+ # env = Dddr.configuration.env
68
+ # data_dir = Dddr.configuration.data_dir
69
+ # container = Dddr.configuration.container
70
+ # CSV.open("#{data_dir}/#{container}/#{env}/#{self.class.name}.csv", "w") do |csv|
71
+ # csv = all.first&.to_hash&.keys
72
+ # all.each do |entity|
73
+ # csv << entity.to_hash.values
74
+ # end
75
+ # end
76
+ end
77
+
78
+
79
+
80
+ def add(entity)
81
+ uid = SecureRandom.uuid
82
+ entity.uid = uid
83
+ entity.created_at ||= DateTime.now.to_s
84
+ entity.last_updated_at ||= entity.created_at
85
+
86
+ SDBM.open(@data_dir) do |db|
87
+ db[uid] = @serializer.dump(entity.to_hash)
88
+ end
89
+ uid
90
+ end
91
+
92
+ alias_method :create, :add
93
+ alias_method :insert, :add
94
+ alias_method :put, :add
95
+ alias_method :append, :add
96
+ alias_method :store, :add
97
+
98
+ def update(entity)
99
+ return unless entity.uid
100
+ entity.last_updated_at = DateTime.now.to_s
101
+ SDBM.open(@data_dir) do |db|
102
+ db[entity.uid] = @serializer.dump(entity.to_hash)
103
+ end
104
+ entity
105
+ end
106
+
107
+ alias_method :modify, :update
108
+ alias_method :change, :update
109
+ alias_method :edit, :update
110
+ alias_method :revise, :update
111
+ alias_method :alter, :update
112
+
113
+ def delete(entity)
114
+ return unless entity.uid
115
+ SDBM.open(@data_dir) do |db|
116
+ db.delete(entity.uid)
117
+ end
118
+ end
119
+
120
+ alias_method :remove, :delete
121
+ alias_method :erase, :delete
122
+ alias_method :discard, :delete
123
+ alias_method :destroy, :delete
124
+ alias_method :wipe, :delete
125
+
126
+ def get(uid)
127
+ SDBM.open(@data_dir) do |db|
128
+ data = db[uid]
129
+ return unless data
130
+
131
+ entity = @entity_class.new
132
+ entity.from_hash(uid, @serializer.load(data))
133
+ entity
134
+ end
135
+ end
136
+
137
+ alias_method :find, :get
138
+
139
+ def all
140
+ result = []
141
+ SDBM.open(@data_dir) do |db|
142
+ db.each do |key, value|
143
+ entity = @entity_class.new
144
+ entity.from_hash(key, @serializer.load(value))
145
+ result << entity unless entity.deleted
146
+ end
147
+ end
148
+ result
149
+ end
150
+ end
151
+
152
+ def to_hash
153
+ result = {}
154
+ instance_variables.each do |var_name|
155
+ attribute_name = var_name.to_s[1..].to_sym
156
+ result[attribute_name] = instance_variable_get(var_name)
157
+ end
158
+ result
159
+ end
160
+
161
+ def from_hash(uid, data_hash)
162
+ self.uid = uid
163
+ data_hash.each do |attribute, value|
164
+ attribute = attribute.to_sym
165
+ if respond_to?(:"#{attribute}=")
166
+ send(:"#{attribute}=", value)
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,156 @@
1
+ module Dddr
2
+ module Sequel
3
+ module ClassMethods
4
+ def queries(&block)
5
+ @queries_module ||= Module.new
6
+ @queries_module.module_eval(&block)
7
+ end
8
+
9
+ def const_missing(name)
10
+ if name == :Repository
11
+ create_repository_for(self)
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def create_repository_for(entity_class)
20
+ repository_class = Class.new(RepositoryBase) do
21
+ define_singleton_method(:new) do |*args|
22
+ super(entity_class)
23
+ end
24
+ end
25
+
26
+ repository_class.include(@queries_module) if instance_variable_defined?(:@queries_module)
27
+
28
+ const_set(:Repository, repository_class)
29
+ end
30
+ end
31
+
32
+ class RepositoryBase
33
+ def initialize(entity_class)
34
+ @entity_class = entity_class
35
+ setup_data_directory(entity_class)
36
+ end
37
+
38
+ def setup_data_directory(entity_class)
39
+ env = Dddr.configuration.env
40
+ data_dir = Dddr.configuration.data_dir
41
+ container = Dddr.configuration.container
42
+ raise Dddr::Error, "Container name is required" unless container
43
+
44
+ # Construct the target directory path
45
+ @name = "#{data_dir}/#{container}/#{env}/#{entity_class.name.downcase}"
46
+
47
+ begin
48
+ # Ensure all directories in the path are created
49
+ FileUtils.mkdir_p(@name)
50
+
51
+ # Change ownership only if the path starts with "/var/"
52
+ if @name.start_with?("/var/")
53
+ FileUtils.chown_R(ENV["USER"], nil, @name)
54
+ end
55
+ rescue Errno::ENOENT => e
56
+ # Log or handle the error
57
+ raise "Failed to create or access the directory: #{e.message}"
58
+ end
59
+ end
60
+
61
+ def count
62
+ all.count
63
+ end
64
+
65
+ def add(entity)
66
+ uid = SecureRandom.uuid
67
+ entity.uid = uid
68
+ entity.created_at ||= DateTime.now.to_s
69
+ entity.last_updated_at ||= entity.created_at
70
+
71
+ SDBM.open(@name) do |db|
72
+ db[uid] = Marshal.dump(entity.to_hash)
73
+ end
74
+ uid
75
+ end
76
+
77
+ alias_method :create, :add
78
+ alias_method :insert, :add
79
+ alias_method :put, :add
80
+ alias_method :append, :add
81
+ alias_method :store, :add
82
+
83
+ def update(entity)
84
+ return unless entity.uid
85
+ entity.last_updated_at = DateTime.now.to_s
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
+ return unless entity.uid
100
+ SDBM.open(@name) do |db|
101
+ db.delete(entity.uid)
102
+ end
103
+ end
104
+
105
+ alias_method :remove, :delete
106
+ alias_method :erase, :delete
107
+ alias_method :discard, :delete
108
+ alias_method :destroy, :delete
109
+ alias_method :wipe, :delete
110
+
111
+ def get(uid)
112
+ SDBM.open(@name) do |db|
113
+ data = db[uid]
114
+ return unless data
115
+
116
+ entity = @entity_class.new
117
+ entity.from_hash(uid, Marshal.load(data))
118
+ entity
119
+ end
120
+ end
121
+
122
+ alias_method :find, :get
123
+
124
+ def all
125
+ result = []
126
+ SDBM.open(@name) do |db|
127
+ db.each do |key, value|
128
+ entity = @entity_class.new
129
+ entity.from_hash(key, Marshal.load(value))
130
+ result << entity unless entity.deleted
131
+ end
132
+ end
133
+ result
134
+ end
135
+ end
136
+
137
+ def to_hash
138
+ result = {}
139
+ instance_variables.each do |var_name|
140
+ attribute_name = var_name.to_s[1..].to_sym
141
+ result[attribute_name] = instance_variable_get(var_name)
142
+ end
143
+ result
144
+ end
145
+
146
+ def from_hash(uid, data_hash)
147
+ self.uid = uid
148
+ data_hash.each do |attribute, value|
149
+ attribute = attribute.to_sym
150
+ if respond_to?(:"#{attribute}=")
151
+ send(:"#{attribute}=", value)
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
data/lib/dddr/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dddr
4
- VERSION = "1.1.4"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/dddr.rb CHANGED
@@ -1,20 +1,42 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "sdbm"
3
4
  require "securerandom"
4
5
  require "date"
6
+ require 'csv'
5
7
  require "ostruct"
6
8
  require "fileutils"
7
9
 
10
+ require_relative "dddr/sdbm"
11
+ require_relative "dddr/sequel"
8
12
  require_relative "dddr/version"
9
13
 
10
14
  module Dddr
15
+ ENTITIES = []
16
+
11
17
  def self.configuration
12
18
  @configuration ||= OpenStruct.new
13
19
  end
14
20
 
21
+ class Serializer
22
+ def initialize(engine)
23
+ @engine = engine
24
+ end
25
+
26
+ def dump(obj)
27
+ @engine.dump(obj)
28
+ end
29
+
30
+ def load(obj)
31
+ @engine.load(obj)
32
+ end
33
+ end
34
+
15
35
  def self.configure
16
36
  configuration.env = ENV["DDDR_ENV"] || "development"
17
37
  configuration.data_dir = "/var/dddr"
38
+ configuration.engine = :sdbm
39
+ configuration.serializer = Marshal
18
40
  configuration.container = nil
19
41
  yield(configuration) if block_given?
20
42
  end
@@ -23,168 +45,26 @@ module Dddr
23
45
 
24
46
  module Entity
25
47
  def self.included(base)
26
- base.extend(ClassMethods)
27
- end
28
-
29
- attr_accessor :uid, :created_at, :last_updated_at, :deleted, :deleted_at
30
-
31
- module ClassMethods
32
- def queries(&block)
33
- @queries_module ||= Module.new
34
- @queries_module.module_eval(&block)
35
- end
36
-
37
- def const_missing(name)
38
- if name == :Repository
39
- create_repository_for(self)
40
- else
41
- super
42
- end
43
- end
44
-
45
- private
46
-
47
- def create_repository_for(entity_class)
48
- repository_class = Class.new(RepositoryBase) do
49
- define_singleton_method(:new) do |*args|
50
- super(entity_class)
51
- end
52
- end
53
-
54
- repository_class.include(@queries_module) if instance_variable_defined?(:@queries_module)
55
-
56
- const_set(:Repository, repository_class)
57
- end
58
- end
59
-
60
- class RepositoryBase
61
- def initialize(entity_class)
62
- @entity_class = entity_class
63
- setup_data_directory(entity_class)
64
- end
65
-
66
- def setup_data_directory(entity_class)
67
- env = Dddr.configuration.env
68
- data_dir = Dddr.configuration.data_dir
69
- container = Dddr.configuration.container
70
- raise Dddr::Error, "Container name is required" unless container
71
-
72
- # Construct the target directory path
73
- @name = "#{data_dir}/#{container}/#{env}/#{entity_class.name.downcase}"
74
-
75
- begin
76
- # Ensure all directories in the path are created
77
- FileUtils.mkdir_p(@name)
78
-
79
- # Change ownership only if the path starts with "/var/"
80
- if @name.start_with?("/var/")
81
- FileUtils.chown_R(ENV["USER"], nil, @name)
82
- end
83
- rescue Errno::ENOENT => e
84
- # Log or handle the error
85
- raise "Failed to create or access the directory: #{e.message}"
86
- end
87
- end
88
-
89
-
90
- def count
91
- all.count
92
- end
93
-
94
- def add(entity)
95
- uid = SecureRandom.uuid
96
- entity.uid = uid
97
- entity.created_at ||= DateTime.now.to_s
98
- entity.last_updated_at ||= entity.created_at
99
-
100
- SDBM.open(@name) do |db|
101
- db[uid] = Marshal.dump(entity.to_hash)
102
- end
103
- uid
104
- end
48
+ if Dddr.configuration.engine == :sdbm
105
49
 
106
- alias_method :create, :add
107
- alias_method :insert, :add
108
- alias_method :put, :add
109
- alias_method :append, :add
110
- alias_method :store, :add
111
-
112
- def update(entity)
113
- return unless entity.uid
114
- entity.last_updated_at = DateTime.now.to_s
115
- SDBM.open(@name) do |db|
116
- db[entity.uid] = Marshal.dump(entity.to_hash)
117
- end
118
- entity
119
- end
50
+ puts "Including #{base.name}"
120
51
 
121
- alias_method :modify, :update
122
- alias_method :change, :update
123
- alias_method :edit, :update
124
- alias_method :revise, :update
125
- alias_method :alter, :update
126
-
127
- def delete(entity)
128
- return unless entity.uid
129
- SDBM.open(@name) do |db|
130
- db.delete(entity.uid)
131
- end
132
- end
52
+ Dddr::ENTITIES << base
133
53
 
134
- alias_method :remove, :delete
135
- alias_method :erase, :delete
136
- alias_method :discard, :delete
137
- alias_method :destroy, :delete
138
- alias_method :wipe, :delete
139
-
140
- def get(uid)
141
- SDBM.open(@name) do |db|
142
- data = db[uid]
143
- return unless data
144
-
145
- entity = @entity_class.new
146
- entity.from_hash(uid, Marshal.load(data))
147
- entity
148
- end
54
+ include Dddr::Sdbm
55
+ base.extend(Dddr::Sdbm::ClassMethods)
149
56
  end
150
-
151
- alias_method :find, :get
152
-
153
- def all
154
- result = []
155
- SDBM.open(@name) do |db|
156
- db.each do |key, value|
157
- entity = @entity_class.new
158
- entity.from_hash(key, Marshal.load(value))
159
- result << entity unless entity.deleted
160
- end
161
- end
162
- result
57
+ if Dddr.configuration.engine == :sequel
58
+ include Dddr::Sequel
59
+ base.extend(Dddr::Sdbm::ClassMethods)
163
60
  end
164
61
  end
165
62
 
166
- def to_hash
167
- result = {}
168
- instance_variables.each do |var_name|
169
- attribute_name = var_name.to_s[1..].to_sym
170
- result[attribute_name] = instance_variable_get(var_name)
171
- end
172
- result
173
- end
63
+ attr_accessor :uid, :created_at, :last_updated_at, :deleted, :deleted_at
174
64
 
175
65
  def new?
176
66
  created_at.nil?
177
67
  end
178
-
179
- def from_hash(uid, data_hash)
180
- self.uid = uid
181
- data_hash.each do |attribute, value|
182
- attribute = attribute.to_sym
183
- if respond_to?(:"#{attribute}=")
184
- send(:"#{attribute}=", value)
185
- end
186
- end
187
- end
188
68
  end
189
69
  end
190
70
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dddr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delaney Burke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-14 00:00:00.000000000 Z
11
+ date: 2024-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sdbm
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sequel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.32'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.32'
41
+ - !ruby/object:Gem::Dependency
42
+ name: oj
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.11'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.11'
27
55
  description: |
28
56
  DDDr stands for Domain Driven Design Repository. It's a Ruby gem that simplifies the implementation of data repositories in a Domain-Driven Design (DDD) architecture.
29
57
  It offers a clean interface for abstracting data access, allowing you to focus on your domain logic rather than database operations.
@@ -34,7 +62,6 @@ executables: []
34
62
  extensions: []
35
63
  extra_rdoc_files: []
36
64
  files:
37
- - ".DS_Store"
38
65
  - ".rspec"
39
66
  - ".standard.yml"
40
67
  - CHANGELOG.md
@@ -44,6 +71,8 @@ files:
44
71
  - Rakefile
45
72
  - hero.png
46
73
  - lib/dddr.rb
74
+ - lib/dddr/sdbm.rb
75
+ - lib/dddr/sequel.rb
47
76
  - lib/dddr/version.rb
48
77
  - sig/dddr.rbs
49
78
  homepage: https://gitlab.com/dekubu/dddr
@@ -66,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
95
  - !ruby/object:Gem::Version
67
96
  version: '0'
68
97
  requirements: []
69
- rubygems_version: 3.5.3
98
+ rubygems_version: 3.5.5
70
99
  signing_key:
71
100
  specification_version: 4
72
101
  summary: Domain Driven Design Repository
data/.DS_Store DELETED
Binary file