dddr 1.0.8 → 1.1.1

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dddr/version.rb +1 -1
  3. data/lib/dddr.rb +113 -134
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4646e8a727ff71b363a29bf0efe1bc952e999ecc726c51e898e3013734992b4d
4
- data.tar.gz: 31d7837d050a1cdba539cd7705a5ad52d2cbb1006998d0c34179bbdc99e82d1e
3
+ metadata.gz: ac6037def8474e0a19cb71d65912d75c63d4ed0bf23a02f7a42d5ac6202b1fc3
4
+ data.tar.gz: f706863b32ef9fb10038b36e45414027c1f3f6a31afdaf715de8c726fc897ba2
5
5
  SHA512:
6
- metadata.gz: ff6a74ec83c1846a561804fffe3d6d0e96a0b618ab0d9672b2fd130f8d3620a6dc105029a0d64e0639d0795ecfa1eb46a9ffc9c42fd775ef371db49d643809a5
7
- data.tar.gz: ca5cfacbc23a24b65dc55c606d4052d213c53b84ee645e9f721c1052a05175ba5f29e84c6296810b8dbba898d27303dfb4a5cf549e4f77de295d2a98dde05085
6
+ metadata.gz: 4dcfc32439afd20298a0aa34c9d2db345ed3a0abf02cd8821c4e09a955c168e7a24a536e165e2c82ff99bf2548b3a710449c4900b1f88a9628839d159bc9ba8c
7
+ data.tar.gz: '0096a5f6de876e4e4cafc4f12c18b3e42252699bfed22c0e1d1cfc6e62bf2ed825a50c5dd4f53fd4d2223460a10c872f4599b558a3f5505abce84b43ae74c876'
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.0.8"
4
+ VERSION = "1.1.1"
5
5
  end
data/lib/dddr.rb CHANGED
@@ -15,7 +15,7 @@ module Dddr
15
15
  def self.configure
16
16
  configuration.env = ENV["DDDR_ENV"] || "development"
17
17
  configuration.data_dir = "/var/dddr"
18
- configuration.container = "#{File.basename(Dir.pwd)}"
18
+ configuration.container = nil
19
19
  yield(configuration) if block_given?
20
20
  end
21
21
 
@@ -30,148 +30,130 @@ module Dddr
30
30
 
31
31
  module ClassMethods
32
32
  def queries(&block)
33
- const_set(:Queries, Module.new(&block))
34
- self::Repository.include(const_get(:Queries)) if const_defined?(:Repository)
35
- end
36
-
37
- def associations(repository, &block)
38
- const_set(:AssociationMethod, Module.new(&block))
39
- repository.include(const_get(:AssociationMethod))
33
+ @queries_module ||= Module.new
34
+ @queries_module.module_eval(&block)
40
35
  end
41
36
 
42
37
  def const_missing(name)
38
+ if name == :Repository
39
+ create_repository_for(self)
40
+ else
41
+ super
42
+ end
43
+ end
43
44
 
44
- @@context = self
45
+ private
45
46
 
46
- if defined?(self::Queries)
47
- @@queries = self::Queries
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
48
52
  end
49
53
 
50
- if name == :Repository
54
+ repository_class.include(@queries_module) if instance_variable_defined?(:@queries_module)
51
55
 
52
- repository_class = Class.new do
53
- def initialize(entity_class = @@context)
54
- @entity_class = entity_class
55
- env = Dddr.configuration.env
56
-
57
- unless Dir.exist? Dddr.configuration.data_dir
58
-
59
- if Dddr.configuration.data_dir.include? "/var/"
60
- `sudo mkdir -p #{Dddr.configuration.data_dir}/#{Dddr.configuration.container}/#{env}/`
61
- `sudo chown $USER #{Dddr.configuration.data_dir}/#{Dddr.configuration.container}/#{env}/`
62
- else
63
- `mkdir -p #{Dddr.configuration.data_dir}/#{Dddr.configuration.container}/#{env}/`
64
- end
65
-
66
- end
67
-
68
- @name = "#{Dddr.configuration.data_dir}/#{Dddr.configuration.container}/#{env}/#{entity_class.name.downcase}"
69
- end
70
-
71
- def add(entity)
72
- uid = SecureRandom.uuid
73
- entity.uid = uid
74
- entity.created_at ||= DateTime.now.to_s
75
- entity.last_updated_at ||= entity.created_at
76
-
77
- SDBM.open(@name) do |db|
78
- db[uid] = Marshal.dump(entity.to_hash)
79
- end
80
- uid
81
- end
82
-
83
- alias_method :create, :add
84
- alias_method :insert, :add
85
- alias_method :put, :add
86
- alias_method :append, :add
87
- alias_method :store, :add
88
-
89
- def update(entity)
90
- entity.last_updated_at = DateTime.now.to_s
91
- SDBM.open(@name) do |db|
92
- db[entity.uid] = Marshal.dump(entity.to_hash)
93
- end
94
- entity
95
- end
96
-
97
- alias_method :modify, :update
98
- alias_method :change, :update
99
- alias_method :edit, :update
100
- alias_method :revise, :update
101
- alias_method :alter, :update
102
-
103
- def delete(entity)
104
- SDBM.open(@name) do |db|
105
- db.delete(entity.uid)
106
- end
107
- end
108
-
109
- alias_method :remove, :delete
110
- alias_method :erase, :delete
111
- alias_method :discard, :delete
112
- alias_method :destroy, :delete
113
- alias_method :wipe, :delete
114
-
115
- def get(uid)
116
- all.find { |e| e.uid == uid }
117
- end
118
-
119
- alias_method :fetch, :get
120
- alias_method :retrieve, :get
121
- alias_method :find, :get
122
- alias_method :acquire, :get
123
- alias_method :obtain, :get
124
-
125
- def reset!
126
- if Dddr.configuration.env == "test"
127
- all.map { |e| delete(e) }
128
- return
129
- end
130
-
131
- puts "Warning: This will delete all records. Are you sure? (y/n)"
132
- confirmation = gets.chomp
133
-
134
- if confirmation.downcase == "y"
135
- puts "Type 'reset' to confirm."
136
- final_confirmation = gets.chomp
137
-
138
- if final_confirmation.downcase == "reset"
139
- all.map { |e| delete(e) }
140
- puts "All records have been deleted."
141
- else
142
- puts "Reset cancelled."
143
- end
144
- else
145
- puts "Reset cancelled."
146
- end
147
- end
148
-
149
- def count
150
- all.count
151
- end
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
163
- end
164
- end
56
+ const_set(:Repository, repository_class)
57
+ end
58
+ end
165
59
 
166
- if defined?(@@queries)
167
- repository_class.include @@queries
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
+ unless Dir.exist?(data_dir)
73
+ if data_dir.include?("/var/")
74
+ `sudo mkdir -p #{data_dir}/#{container}/#{env}/`
75
+ `sudo chown $USER #{data_dir}/#{container}/#{env}/`
76
+ else
77
+ `mkdir -p #{data_dir}/#{container}/#{env}/`
168
78
  end
79
+ end
80
+ @name = "#{data_dir}/#{container}/#{env}/#{entity_class.name.downcase}"
81
+ end
169
82
 
170
- const_set(name, repository_class)
171
- else
172
- super
83
+ def count
84
+ all.count
85
+ end
86
+
87
+ def add(entity)
88
+ uid = SecureRandom.uuid
89
+ entity.uid = uid
90
+ entity.created_at ||= DateTime.now.to_s
91
+ entity.last_updated_at ||= entity.created_at
92
+
93
+ SDBM.open(@name) do |db|
94
+ db[uid] = Marshal.dump(entity.to_hash)
95
+ end
96
+ uid
97
+ end
98
+
99
+ alias_method :create, :add
100
+ alias_method :insert, :add
101
+ alias_method :put, :add
102
+ alias_method :append, :add
103
+ alias_method :store, :add
104
+
105
+ def update(entity)
106
+ return unless entity.uid
107
+ entity.last_updated_at = DateTime.now.to_s
108
+ SDBM.open(@name) do |db|
109
+ db[entity.uid] = Marshal.dump(entity.to_hash)
110
+ end
111
+ entity
112
+ end
113
+
114
+ alias_method :modify, :update
115
+ alias_method :change, :update
116
+ alias_method :edit, :update
117
+ alias_method :revise, :update
118
+ alias_method :alter, :update
119
+
120
+ def delete(entity)
121
+ return unless entity.uid
122
+ SDBM.open(@name) do |db|
123
+ db.delete(entity.uid)
173
124
  end
174
125
  end
126
+
127
+ alias_method :remove, :delete
128
+ alias_method :erase, :delete
129
+ alias_method :discard, :delete
130
+ alias_method :destroy, :delete
131
+ alias_method :wipe, :delete
132
+
133
+ def get(uid)
134
+ SDBM.open(@name) do |db|
135
+ data = db[uid]
136
+ return unless data
137
+
138
+ entity = @entity_class.new
139
+ entity.from_hash(uid, Marshal.load(data))
140
+ entity
141
+ end
142
+ end
143
+
144
+ alias_method :find, :get
145
+
146
+ def all
147
+ result = []
148
+ SDBM.open(@name) do |db|
149
+ db.each do |key, value|
150
+ entity = @entity_class.new
151
+ entity.from_hash(key, Marshal.load(value))
152
+ result << entity unless entity.deleted
153
+ end
154
+ end
155
+ result
156
+ end
175
157
  end
176
158
 
177
159
  def to_hash
@@ -184,8 +166,7 @@ module Dddr
184
166
  end
185
167
 
186
168
  def new?
187
- return true if created_at.nil?
188
- false
169
+ created_at.nil?
189
170
  end
190
171
 
191
172
  def from_hash(uid, data_hash)
@@ -198,8 +179,6 @@ module Dddr
198
179
  end
199
180
  end
200
181
  end
201
-
202
- # Your code goes here...
203
182
  end
204
183
 
205
184
  Dddr.configure
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.0.8
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delaney Burke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-28 00:00:00.000000000 Z
11
+ date: 2024-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sdbm
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
68
  requirements: []
69
- rubygems_version: 3.4.21
69
+ rubygems_version: 3.5.3
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: Domain Driven Design Repository