dddr 2.0.3 → 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: da1458c32e2bba6a0a94f1014aac39675ca2267790a50d7fcb09e1c8af0cc2d9
4
- data.tar.gz: 96b8c4d7f5d2adea851ba8abd61d5a3e2a5dd587523a0b789459d1e1b9a76d64
3
+ metadata.gz: d7ad39d7137de961ad7d5a3998b4e47acf2e500b18c4c22872d6d4267c921c14
4
+ data.tar.gz: 482665fcf1eb5722ebf0942c6e521d0f4b673cb51e30378cdb0e72a448997728
5
5
  SHA512:
6
- metadata.gz: 390527b09424ef5cde4da811fbc32cab8823af20001473ca975c4d629842294133676000d9eefe1b1d606ecf286860fa4d5e284b55790bc0f7a4e2e750368ac2
7
- data.tar.gz: 66c3b7cd6f43cb8d6559e643c3627e9accb7e269d43b9b730398b8ad8b11e8e51047c76759566579a38ccb4847772f043172ef921b4aadcc680719a726794408
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.0.3"
4
+ VERSION = "2.2.0"
5
5
  end
data/lib/dddr.rb CHANGED
@@ -7,6 +7,7 @@ require 'csv'
7
7
  require "ostruct"
8
8
  require "fileutils"
9
9
  require "oj"
10
+ require "msgpack"
10
11
 
11
12
  require_relative "dddr/sdbm"
12
13
  require_relative "dddr/sequel"
@@ -19,6 +20,15 @@ module Dddr
19
20
  @configuration ||= OpenStruct.new
20
21
  end
21
22
 
23
+ class MessagePack
24
+ def self.load(obj)
25
+ ::MessagePack.unpack(obj)
26
+ end
27
+ def self.dump(obj)
28
+ obj.to_msgpack
29
+ end
30
+ end
31
+
22
32
  class Serializer
23
33
  def initialize(engine)
24
34
  @engine = engine
@@ -37,7 +47,7 @@ module Dddr
37
47
  configuration.env = ENV["DDDR_ENV"] || "development"
38
48
  configuration.data_dir = "/var/dddr"
39
49
  configuration.engine = :sdbm
40
- configuration.serializer = Oj
50
+ configuration.serializer = MessagePack
41
51
  configuration.container = nil
42
52
  yield(configuration) if block_given?
43
53
  end
@@ -45,6 +55,13 @@ module Dddr
45
55
  class Error < StandardError; end
46
56
 
47
57
  module Entity
58
+
59
+ def self.create_properties(*names)
60
+ names.each do |name|
61
+ attr_accessor name
62
+ end
63
+ end
64
+
48
65
  def self.included(base)
49
66
  if Dddr.configuration.engine == :sdbm
50
67
  Dddr::ENTITIES << base
@@ -53,7 +70,7 @@ module Dddr
53
70
  end
54
71
  if Dddr.configuration.engine == :sequel
55
72
  include Dddr::Sequel
56
- base.extend(Dddr::Sdbm::ClassMethods)
73
+ base.extend(Dddr::Sequel::ClassMethods)
57
74
  end
58
75
  end
59
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.0.3
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-11 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
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: msgpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: |
56
70
  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.
57
71
  It offers a clean interface for abstracting data access, allowing you to focus on your domain logic rather than database operations.