dddr 2.1.0 → 2.2.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: 348b677b23aa6781f82ed84d35a72e2cbb2873245380194cb2a33d017bd4eb29
4
- data.tar.gz: 174ee9771fcb8633cf1661e96e1b419c78ba7dc98b47f333665c8bbf1b7d4cfa
3
+ metadata.gz: d7ad39d7137de961ad7d5a3998b4e47acf2e500b18c4c22872d6d4267c921c14
4
+ data.tar.gz: 482665fcf1eb5722ebf0942c6e521d0f4b673cb51e30378cdb0e72a448997728
5
5
  SHA512:
6
- metadata.gz: 717aa4966cdf9b930d223b48257cef1b0de8a8397017b0d13dab766f662d1a1f91b55baf1e6d6de98a580e1f887d5fb57af5f5050fede90172b18a839e9b9216
7
- data.tar.gz: a3fb0a6b030793c96a688abf6f803dfd9324132d53f479353fbe4edb67f30325110d17d57204dc295606f494eba8492784520fb9c3405c1aacc00d2568996136
6
+ metadata.gz: d5b1958c7a3c414c8a7b2ae32d94f84ca21c615d0752cab9d41676f2cc2edca33d16107235ec52d665ecaed04a9ababa88f855c68a6b7f063ba5cd896aef9584
7
+ data.tar.gz: c5b55bf5b7f9567999159a528739a24e687afcdbffab569b5b037bf184990eb98cdc47068b83bf4683eabfa27a921029e9e8876905c4e886298e9769f05cf406
data/lib/dddr/sequel.rb CHANGED
@@ -1,6 +1,30 @@
1
+ require 'sequel'
2
+
3
+ ::Sequel::Model.plugin :timestamps
4
+
1
5
  module Dddr
2
6
  module Sequel
3
7
  module ClassMethods
8
+
9
+ def create_properties(*names)
10
+ names.each do |name|
11
+ name.each do |name|
12
+ attr_accessor name
13
+ end
14
+ end
15
+ end
16
+
17
+ def from_row(row)
18
+ item = new
19
+ item.from_hash(row[:uid],row)
20
+ item
21
+ end
22
+
23
+ def table(name = nil)
24
+ @@table_name = name unless name.nil?
25
+ @@table_name
26
+ end
27
+
4
28
  def queries(&block)
5
29
  @queries_module ||= Module.new
6
30
  @queries_module.module_eval(&block)
@@ -30,64 +54,32 @@ module Dddr
30
54
  end
31
55
 
32
56
  class RepositoryBase
57
+
58
+ attr_reader :dataset
59
+
33
60
  def initialize(entity_class)
34
61
  @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
62
+ @dataset = Dddr.configuration.db[@entity_class.table]
63
+ @entity_class.create_properties(@dataset.columns)
59
64
  end
60
65
 
61
66
  def count
62
- all.count
67
+ @dataset.count
63
68
  end
64
69
 
65
70
  def add(entity)
66
71
  uid = SecureRandom.uuid
67
72
  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
73
+ @dataset.insert(entity.to_hash)
74
74
  uid
75
75
  end
76
-
77
76
  alias_method :create, :add
78
77
  alias_method :insert, :add
79
- alias_method :put, :add
80
- alias_method :append, :add
81
- alias_method :store, :add
82
78
 
83
79
  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
80
+ @dataset.where(uid: entity.uid).update(entity.to_hash)
81
+
82
+ end
91
83
 
92
84
  alias_method :modify, :update
93
85
  alias_method :change, :update
@@ -96,10 +88,7 @@ module Dddr
96
88
  alias_method :alter, :update
97
89
 
98
90
  def delete(entity)
99
- return unless entity.uid
100
- SDBM.open(@name) do |db|
101
- db.delete(entity.uid)
102
- end
91
+ @dataset.where(uid: entity.uid).delete
103
92
  end
104
93
 
105
94
  alias_method :remove, :delete
@@ -109,28 +98,21 @@ module Dddr
109
98
  alias_method :wipe, :delete
110
99
 
111
100
  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
101
+ row = @dataset.where(uid: uid).first
102
+ raise Dddr::Error, "UID is required" unless row
103
+ item = Item.new
104
+ item.from_hash(uid,row)
105
+ item
120
106
  end
121
107
 
122
108
  alias_method :find, :get
123
109
 
124
110
  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
111
+ @dataset.all.map do |row|
112
+ item = Item.new
113
+ item.from_hash(row[:uid],row)
114
+ item
132
115
  end
133
- result
134
116
  end
135
117
  end
136
118
 
@@ -142,7 +124,7 @@ module Dddr
142
124
  end
143
125
  result
144
126
  end
145
-
127
+
146
128
  def from_hash(uid, data_hash)
147
129
  self.uid = uid
148
130
  data_hash.each do |attribute, value|
@@ -152,5 +134,6 @@ module Dddr
152
134
  end
153
135
  end
154
136
  end
137
+
155
138
  end
156
139
  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 = "2.1.0"
4
+ VERSION = "2.2.0"
5
5
  end
data/lib/dddr.rb CHANGED
@@ -55,6 +55,13 @@ module Dddr
55
55
  class Error < StandardError; end
56
56
 
57
57
  module Entity
58
+
59
+ def self.create_properties(*names)
60
+ names.each do |name|
61
+ attr_accessor name
62
+ end
63
+ end
64
+
58
65
  def self.included(base)
59
66
  if Dddr.configuration.engine == :sdbm
60
67
  Dddr::ENTITIES << base
@@ -63,7 +70,7 @@ module Dddr
63
70
  end
64
71
  if Dddr.configuration.engine == :sequel
65
72
  include Dddr::Sequel
66
- base.extend(Dddr::Sdbm::ClassMethods)
73
+ base.extend(Dddr::Sequel::ClassMethods)
67
74
  end
68
75
  end
69
76
 
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: 2.1.0
4
+ version: 2.2.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-02-12 00:00:00.000000000 Z
11
+ date: 2024-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sdbm