dddr 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3967837aa56e6910bdf8ffc00d575c521fb1454814c3c9895a7335b17cbc877f
4
- data.tar.gz: 77de95c903cff093d1db2d625c0d94dfb58eba214014b731fb40e58abe3c500b
3
+ metadata.gz: f96ffac1e23446ac9a1c36f4510843e4dc1604def5358f57118ace997c0b7d82
4
+ data.tar.gz: f2596ac36f26e0f507b1e0daaf26de0fff2708a840eca88b5ea11b433dbb4f4a
5
5
  SHA512:
6
- metadata.gz: 668227ed62d727887f0866d1eb4ba4b4d5ff9dc293c6122e7b20a828f07d97966dc43572006f1b807d16750e635d6fd91940baead953f1ba4aaa214119e6ffa7
7
- data.tar.gz: 65836fea37bc4123d59cd33779e11cb05600b4e0cbd20b08d25cdc9a63a65c33c1c5c122a23911c602fad7f2eefd3eccf77eb899cf97e117926aaeca7bccf75a
6
+ metadata.gz: ea1ff7abff2d883c5a0fc4ede0f79a8895a062e47832372cdbe4303ce111280c032bb2824a84f8d1bbd538d6b45f5ee9ecc2c3d49e193221211ca8bbca4d55c4
7
+ data.tar.gz: feca838ebd27c5a28169babab509a6278cc485f81b1de1e8f3b96ae327bdb428ad9887640cd786ce0d127d22f2e300b4b34df0aba6ad2eaff792a1a058d0933d
data/lib/dddr/sequel.rb CHANGED
@@ -1,15 +1,13 @@
1
1
  require "sequel"
2
+ require "securerandom"
2
3
 
3
- ::Sequel::Model.plugin :timestamps
4
-
4
+ # Assuming Dddr.configuration.db is already set up to return a Sequel database connection
5
5
  module Dddr
6
6
  module Sequel
7
7
  module ClassMethods
8
8
  def create_properties(*names)
9
- names.each do |name|
10
- name.each do |name|
11
- attr_accessor name
12
- end
9
+ names.flatten.each do |name|
10
+ attr_accessor name
13
11
  end
14
12
  end
15
13
 
@@ -20,8 +18,8 @@ module Dddr
20
18
  end
21
19
 
22
20
  def table(name = nil)
23
- @@table_name = name unless name.nil?
24
- @@table_name
21
+ @table_name = name unless name.nil?
22
+ @table_name
25
23
  end
26
24
 
27
25
  def queries(&block)
@@ -41,13 +39,12 @@ module Dddr
41
39
 
42
40
  def create_repository_for(entity_class)
43
41
  repository_class = Class.new(RepositoryBase) do
44
- define_singleton_method(:new) do |*args|
45
- super(entity_class)
42
+ define_singleton_method(:entity_class) do
43
+ entity_class
46
44
  end
47
45
  end
48
46
 
49
47
  repository_class.include(@queries_module) if instance_variable_defined?(:@queries_module)
50
-
51
48
  const_set(:Repository, repository_class)
52
49
  end
53
50
  end
@@ -55,79 +52,61 @@ module Dddr
55
52
  class RepositoryBase
56
53
  attr_reader :dataset
57
54
 
58
- def initialize(entity_class)
59
- @entity_class = entity_class
60
- @dataset = Dddr.configuration.db[@entity_class.table]
61
- @entity_class.create_properties(@dataset.columns)
55
+ def initialize
56
+ @dataset = Dddr.configuration.db[self.class.entity_class.table]
57
+ self.class.entity_class.create_properties(@dataset.columns)
62
58
  end
63
59
 
64
60
  def count
65
61
  @dataset.count
66
62
  end
67
63
 
68
- def add(entity)
69
- uid = SecureRandom.uuid
70
- entity.uid = uid
71
- @dataset.insert(entity.to_hash)
72
- uid
64
+ def get(uid)
65
+ row = @dataset.where(uid: uid).first
66
+ self.class.entity_class.from_row(row) if row
73
67
  end
74
- alias_method :create, :add
75
- alias_method :insert, :add
76
68
 
77
69
  def update(entity)
78
70
  @dataset.where(uid: entity.uid).update(entity.to_hash)
79
71
  end
80
72
 
81
- alias_method :modify, :update
82
- alias_method :change, :update
83
- alias_method :edit, :update
84
- alias_method :revise, :update
85
- alias_method :alter, :update
73
+ def all
74
+ @dataset.map { |row| self.class.entity_class.from_row(row) }
75
+ end
86
76
 
87
77
  def delete(entity)
88
78
  @dataset.where(uid: entity.uid).delete
89
79
  end
80
+
81
+ alias_method :find, :get
90
82
 
91
- alias_method :remove, :delete
92
- alias_method :erase, :delete
93
- alias_method :discard, :delete
94
- alias_method :destroy, :delete
95
- alias_method :wipe, :delete
96
-
97
- def get(uid)
98
- row = @dataset.where(uid: uid).first
99
- raise Dddr::Error, "UID is required" unless row
100
- item = Item.new
101
- item.from_hash(uid, row)
102
- item
83
+ def add(entity)
84
+ uid = SecureRandom.uuid
85
+ entity.uid = uid
86
+ @dataset.insert(entity.to_hash)
87
+ uid
103
88
  end
89
+ alias_method :create, :add
90
+ alias_method :insert, :add
104
91
 
105
- alias_method :find, :get
92
+ # Methods update, delete, get, etc., remain unchanged.
93
+ end
106
94
 
107
- def all
108
- @dataset.all.map do |row|
109
- item = Item.new
110
- item.from_hash(row[:uid], row)
111
- item
112
- end
95
+ module Entity
96
+ def self.included(base)
97
+ base.extend(ClassMethods)
113
98
  end
114
- end
115
99
 
116
- def to_hash
117
- result = {}
118
- instance_variables.each do |var_name|
119
- attribute_name = var_name.to_s[1..].to_sym
120
- result[attribute_name] = instance_variable_get(var_name)
100
+ def to_hash
101
+ instance_variables.each_with_object({}) do |var, hash|
102
+ hash[var.to_s.delete("@").to_sym] = instance_variable_get(var)
103
+ end
121
104
  end
122
- result
123
- end
124
105
 
125
- def from_hash(uid, data_hash)
126
- self.uid = uid
127
- data_hash.each do |attribute, value|
128
- attribute = attribute.to_sym
129
- if respond_to?(:"#{attribute}=")
130
- send(:"#{attribute}=", value)
106
+ def from_hash(uid, data_hash)
107
+ self.uid = uid
108
+ data_hash.each do |attribute, value|
109
+ send(:"#{attribute}=", value) if respond_to?(:"#{attribute}=")
131
110
  end
132
111
  end
133
112
  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.4.0"
4
+ VERSION = "2.5.0"
5
5
  end
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.4.0
4
+ version: 2.5.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-03-12 00:00:00.000000000 Z
11
+ date: 2024-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sdbm