dddr 1.0.8 → 1.1.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dddr/version.rb +1 -1
  3. data/lib/dddr.rb +110 -133
  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: 46a9b18b953d2365bebe6f55b8772d89489133bbca231e7622318b3e8fd44ed1
4
+ data.tar.gz: 07fa42336f6a55f867eeb111b459803126aa62fd2dc694c5733fb1bf74a36371
5
5
  SHA512:
6
- metadata.gz: ff6a74ec83c1846a561804fffe3d6d0e96a0b618ab0d9672b2fd130f8d3620a6dc105029a0d64e0639d0795ecfa1eb46a9ffc9c42fd775ef371db49d643809a5
7
- data.tar.gz: ca5cfacbc23a24b65dc55c606d4052d213c53b84ee645e9f721c1052a05175ba5f29e84c6296810b8dbba898d27303dfb4a5cf549e4f77de295d2a98dde05085
6
+ metadata.gz: c660616726df5f0a8a6f267c0c303bc77b79323751db7da29e87d18214582c26b6977cc243bd75b32ebf64cf1635d159ef16dc642066e13dd4bfa688383ba68b
7
+ data.tar.gz: 6b333f66013aff5cc3938d199b1d0580d7f350a073304289e20de9afd55c053ee6117b8e0ce5886e9c3cd9896f8c531cd7b7d1a9be9e82b2cf57e8013a73b895
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.0"
5
5
  end
data/lib/dddr.rb CHANGED
@@ -30,148 +30,128 @@ 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
+ unless Dir.exist?(data_dir)
71
+ if data_dir.include?("/var/")
72
+ `sudo mkdir -p #{data_dir}/#{container}/#{env}/`
73
+ `sudo chown $USER #{data_dir}/#{container}/#{env}/`
74
+ else
75
+ `mkdir -p #{data_dir}/#{container}/#{env}/`
168
76
  end
77
+ end
78
+ @name = "#{data_dir}/#{container}/#{env}/#{entity_class.name.downcase}"
79
+ end
169
80
 
170
- const_set(name, repository_class)
171
- else
172
- super
81
+ def count
82
+ all.count
83
+ end
84
+
85
+ def add(entity)
86
+ uid = SecureRandom.uuid
87
+ entity.uid = uid
88
+ entity.created_at ||= DateTime.now.to_s
89
+ entity.last_updated_at ||= entity.created_at
90
+
91
+ SDBM.open(@name) do |db|
92
+ db[uid] = Marshal.dump(entity.to_hash)
93
+ end
94
+ uid
95
+ end
96
+
97
+ alias_method :create, :add
98
+ alias_method :insert, :add
99
+ alias_method :put, :add
100
+ alias_method :append, :add
101
+ alias_method :store, :add
102
+
103
+ def update(entity)
104
+ return unless entity.uid
105
+ entity.last_updated_at = DateTime.now.to_s
106
+ SDBM.open(@name) do |db|
107
+ db[entity.uid] = Marshal.dump(entity.to_hash)
108
+ end
109
+ entity
110
+ end
111
+
112
+ alias_method :modify, :update
113
+ alias_method :change, :update
114
+ alias_method :edit, :update
115
+ alias_method :revise, :update
116
+ alias_method :alter, :update
117
+
118
+ def delete(entity)
119
+ return unless entity.uid
120
+ SDBM.open(@name) do |db|
121
+ db.delete(entity.uid)
173
122
  end
174
123
  end
124
+
125
+ alias_method :remove, :delete
126
+ alias_method :erase, :delete
127
+ alias_method :discard, :delete
128
+ alias_method :destroy, :delete
129
+ alias_method :wipe, :delete
130
+
131
+ def get(uid)
132
+ SDBM.open(@name) do |db|
133
+ data = db[uid]
134
+ return unless data
135
+
136
+ entity = @entity_class.new
137
+ entity.from_hash(uid, Marshal.load(data))
138
+ entity
139
+ end
140
+ end
141
+
142
+ alias_method :find, :get
143
+
144
+ def all
145
+ result = []
146
+ SDBM.open(@name) do |db|
147
+ db.each do |key, value|
148
+ entity = @entity_class.new
149
+ entity.from_hash(key, Marshal.load(value))
150
+ result << entity unless entity.deleted
151
+ end
152
+ end
153
+ result
154
+ end
175
155
  end
176
156
 
177
157
  def to_hash
@@ -184,8 +164,7 @@ module Dddr
184
164
  end
185
165
 
186
166
  def new?
187
- return true if created_at.nil?
188
- false
167
+ created_at.nil?
189
168
  end
190
169
 
191
170
  def from_hash(uid, data_hash)
@@ -198,8 +177,6 @@ module Dddr
198
177
  end
199
178
  end
200
179
  end
201
-
202
- # Your code goes here...
203
180
  end
204
181
 
205
182
  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.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: 2023-12-28 00:00:00.000000000 Z
11
+ date: 2024-01-04 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