dddr 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dddr/version.rb +1 -1
  3. data/lib/dddr.rb +16 -87
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c40da5a9a83abb1bd1f6bb3391f774a52025c598b5a9d74c52fa8c76d2695119
4
- data.tar.gz: d24a0883d829329371894ae5fa6c8360aa583c227875b6720477363c97befeaf
3
+ metadata.gz: 6bbde5292a99fb4dea6c1553e4de93e54d2deadc52f198acba249addb00cb4f0
4
+ data.tar.gz: 4f771b22d6853b38c0e5ffc28264d6a8142a94667ff82cecd6f44bcaf1275fc7
5
5
  SHA512:
6
- metadata.gz: ac30bebd6baee07b567f0d4274c189c73043def0632f58673837e00679f3102a3cbb09b23dc1d0d1dc733639c1c7c2aacf36f4a9bbb8195387d9ca018c772700
7
- data.tar.gz: 73c1ecf97505e7705a87d0d37931e88c2265bc021f1d16d31f5e7b6a4b3bedd6be69096efb69157d7699e437c79b0f5ffd001ceec01bb310a2fdef2b11f07e36
6
+ metadata.gz: 0aeee8ae9c148560dc5fdaddbad1fb2c5dac593308317d402b94555f8036c51b781f12b19f66c5f2ae433ae8b601e197bee61a5752f07f3753f3ca1f923d5f6d
7
+ data.tar.gz: 7036c9c802ed2c858aac293e8572261c5b94d66c9d4bae4f16c230a6bc7133d837a6da79a8da70ab418a3616ea16fe183f480648a549199a351eb30900f2fa0c
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.3"
4
+ VERSION = "1.0.4"
5
5
  end
data/lib/dddr.rb CHANGED
@@ -8,16 +8,14 @@ require "ostruct"
8
8
  require_relative "dddr/version"
9
9
 
10
10
  module Dddr
11
-
12
11
  def self.configuration
13
12
  @configuration ||= OpenStruct.new
14
- @configuration.env = ENV['DDDR_ENV'] || 'development'
15
- @configuration.data_dir = "./.data"
16
- @configuration
17
13
  end
18
14
 
19
15
  def self.configure
20
- yield(configuration)
16
+ configuration.env = ENV["DDDR_ENV"] || "development"
17
+ configuration.data_dir = "./.data"
18
+ yield(configuration) if block_given?
21
19
  end
22
20
 
23
21
  class Error < StandardError; end
@@ -27,7 +25,7 @@ module Dddr
27
25
  base.extend(ClassMethods)
28
26
  end
29
27
 
30
- attr_accessor :uid, :created_at, :last_updated_at, :deleted,:deleted_at
28
+ attr_accessor :uid, :created_at, :last_updated_at, :deleted, :deleted_at
31
29
 
32
30
  module ClassMethods
33
31
  def queries(&block)
@@ -48,25 +46,20 @@ module Dddr
48
46
  end
49
47
 
50
48
  if name == :Repository
51
-
49
+
52
50
  repository_class = Class.new do
53
-
54
51
  def initialize(entity_class = @@context)
55
52
  @entity_class = entity_class
56
53
  env = Dddr.configuration.env
57
54
  `mkdir -p #{Dddr.configuration.data_dir}/#{env}`
58
55
  @name = "#{Dddr.configuration.data_dir}/#{env}/#{entity_class.name}"
59
- @cache = nil
60
- load_cache if @cache.nil?
61
56
  end
62
57
 
63
58
  def add(entity)
64
59
  uid = SecureRandom.uuid
65
-
66
- entity.created_at.nil? ? DateTime.now.to_s : entity.created_at
67
- entity.last_updated_at.nil? ? entity.created_at : entity.last_updated_at
68
-
69
- @cache << entity
60
+ entity.uid = uid
61
+ entity.created_at ||= DateTime.now.to_s
62
+ entity.last_updated_at ||= entity.created_at
70
63
 
71
64
  SDBM.open(@name) do |db|
72
65
  db[uid] = Marshal.dump(entity.to_hash)
@@ -82,9 +75,6 @@ module Dddr
82
75
 
83
76
  def update(entity)
84
77
  entity.last_updated_at = DateTime.now.to_s
85
-
86
- @cache.map! { |e| e.uid == entity.uid ? entity : e }
87
-
88
78
  SDBM.open(@name) do |db|
89
79
  db[entity.uid] = Marshal.dump(entity.to_hash)
90
80
  end
@@ -99,35 +89,9 @@ module Dddr
99
89
 
100
90
  def delete(entity)
101
91
  SDBM.open(@name) do |db|
102
- entity.deleted = true
103
- entity.deleted_at = DateTime.now
104
- update(entity)
92
+ db.delete(entity.uid)
105
93
  end
106
-
107
- @cache.reject! { |e| e.uid == entity.uid }
108
-
109
94
  end
110
-
111
- def undelete(uid)
112
- e = bin.find{|e| e.uid == uid}
113
- e.deleted = false
114
- e.deleted_at = nil
115
- update(e)
116
- end
117
- # Aliases for 'undelete' method
118
- alias restore undelete
119
- alias recover undelete
120
-
121
-
122
- def undelete_all
123
- bin.each do |e|
124
- restore(e.uid)
125
- end
126
- end
127
-
128
- alias restore_all undelete_all
129
- alias recover_all undelete_all
130
-
131
95
 
132
96
  alias_method :remove, :delete
133
97
  alias_method :erase, :delete
@@ -148,8 +112,9 @@ module Dddr
148
112
  def reset!
149
113
  if Dddr.configuration.env == "test"
150
114
  all.map { |e| delete(e) }
151
- return
115
+ return
152
116
  end
117
+
153
118
  puts "Warning: This will delete all records. Are you sure? (y/n)"
154
119
  confirmation = gets.chomp
155
120
 
@@ -173,54 +138,16 @@ module Dddr
173
138
  end
174
139
 
175
140
  def all
176
- load_cache if @cache.nil?
177
- @cache
178
- end
179
-
180
- def clear_cache
181
- @cache = nil
182
- end
183
-
184
- def refresh_cache
185
- load_cache
186
- end
187
-
188
-
189
- def load_cache
190
- result = []
191
- SDBM.open(@name) do |db|
192
- db.each do |key, value|
193
- data = Marshal.load(value)
194
- entity = @entity_class.new
195
- entity.from_hash(key, data)
196
- next if entity.deleted
197
- result << entity
198
- end
199
- end
200
- @cache = result
201
- end
202
-
203
- def bin
204
141
  result = []
205
142
  SDBM.open(@name) do |db|
206
143
  db.each do |key, value|
207
- data = Marshal.load(value)
208
144
  entity = @entity_class.new
209
- entity.from_hash(key, data)
210
- next unless entity.deleted
211
- result << entity
145
+ entity.from_hash(key, Marshal.load(value))
146
+ result << entity unless entity.deleted
212
147
  end
213
148
  end
214
149
  result
215
150
  end
216
-
217
- def empty_bin
218
- SDBM.open(@name) do |db|
219
- bin.each do |e|
220
- db.delete(e.uid)
221
- end
222
- end
223
- end
224
151
  end
225
152
 
226
153
  if defined?(@@queries)
@@ -260,4 +187,6 @@ module Dddr
260
187
  end
261
188
 
262
189
  # Your code goes here...
263
- end
190
+ end
191
+
192
+ 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.3
4
+ version: 1.0.4
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-03 00:00:00.000000000 Z
11
+ date: 2023-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sdbm