local_model 0.1.11 → 0.1.12
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 +4 -4
- data/lib/local_model/collection.rb +15 -2
- data/lib/local_model/concerns/csv_interactable.rb +16 -5
- data/lib/local_model/csv.rb +1 -1
- data/lib/local_model/model.rb +69 -13
- data/lib/local_model/version.rb +1 -1
- data/lib/local_model.rb +8 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23ec22d2650b17792c2a03dff88d9dffbcedd78b08ebd05afd057fd124d0476a
|
4
|
+
data.tar.gz: 8c89b6eaf25891cb05c3692785d9e88979372be6078ea465d080e9fbd9a7f330
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b770b1003097729755a6a63df94b909c58eb2c39ae64f398db5ff64083b2846307b173d8da3bd3533efa30ba4dce9dce2170f69ac3e6cc61d886d2b4cf08d5c
|
7
|
+
data.tar.gz: 405d6229dbb39b63e8fc5b9dd01d0e3dadaedf06907519f85a411a0d0547c7a1c12d6b1e7528d11f50a6d2e1cfe79f13526f5dcd6e919e1e91125ec9c03290b5
|
@@ -21,8 +21,21 @@ class LocalModel::Collection < Array
|
|
21
21
|
arg.save && self.model.save
|
22
22
|
end
|
23
23
|
|
24
|
-
def build(
|
25
|
-
self.push(
|
24
|
+
def build(**args)
|
25
|
+
self.push(collection_class.create(**args))
|
26
|
+
end
|
27
|
+
|
28
|
+
def where(**args)
|
29
|
+
self.filter do |el|
|
30
|
+
found = true
|
31
|
+
args.each do |k,v|
|
32
|
+
if el.send(k.to_s) != v
|
33
|
+
found = false
|
34
|
+
break
|
35
|
+
end
|
36
|
+
end
|
37
|
+
found
|
38
|
+
end
|
26
39
|
end
|
27
40
|
|
28
41
|
end
|
@@ -18,15 +18,15 @@ module CSVInteractable
|
|
18
18
|
rand_val = @@rand.rand(1000000)
|
19
19
|
f = File.new("#{self.storage_path}-#{rand_val}.prep", 'w')
|
20
20
|
f.close
|
21
|
-
r = @@rand.rand / 10.0
|
22
|
-
sleep(r)
|
21
|
+
# r = @@rand.rand / 10.0
|
22
|
+
# sleep(r)
|
23
23
|
|
24
24
|
if File.exist?("#{self.storage_path}.bak.csv") || Dir["#{self.storage_path}-*.prep"].length > 1
|
25
25
|
File.delete("#{self.storage_path}-#{rand_val}.prep")
|
26
|
-
if @@rand.rand(
|
27
|
-
sleep(0
|
26
|
+
if @@rand.rand(5) != 1
|
27
|
+
sleep(@@rand.rand / 10.0)
|
28
28
|
end
|
29
|
-
mutate_csv
|
29
|
+
mutate_csv(option, block)
|
30
30
|
return
|
31
31
|
else
|
32
32
|
File.delete("#{self.storage_path}-#{rand_val}.prep")
|
@@ -200,6 +200,17 @@ module CSVInteractable
|
|
200
200
|
end
|
201
201
|
end
|
202
202
|
|
203
|
+
def delete_all_rows
|
204
|
+
begin
|
205
|
+
self.mutate_csv(:mutate) {}
|
206
|
+
true
|
207
|
+
rescue
|
208
|
+
File.delete("#{self.storage_path}.bak.csv")
|
209
|
+
false
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
203
214
|
end
|
204
215
|
|
205
216
|
module InstanceMethods
|
data/lib/local_model/csv.rb
CHANGED
data/lib/local_model/model.rb
CHANGED
@@ -5,7 +5,14 @@ class LocalModel::Model
|
|
5
5
|
# yield(SchemaBuilder.new(self))
|
6
6
|
end
|
7
7
|
|
8
|
-
def self.belongs_to(association, class_name: nil, foreign_key: nil)
|
8
|
+
def self.belongs_to(association, class_name: nil, foreign_key: nil, polymorphic: false)
|
9
|
+
if foreign_key.nil?
|
10
|
+
keyname = "#{association}_id"
|
11
|
+
typename = "#{association}_type"
|
12
|
+
else
|
13
|
+
keyname = foreign_key
|
14
|
+
end
|
15
|
+
|
9
16
|
if class_name.nil?
|
10
17
|
association_class_name = LocalModel::Functions.snake_to_camel(association)
|
11
18
|
association_class_name[0] = association_class_name[0].upcase
|
@@ -14,24 +21,32 @@ class LocalModel::Model
|
|
14
21
|
association_class_name = namespace_classname(class_name)
|
15
22
|
end
|
16
23
|
|
17
|
-
if foreign_key.nil?
|
18
|
-
keyname = "#{association}_id"
|
19
|
-
else
|
20
|
-
keyname = foreign_key
|
21
|
-
end
|
22
24
|
|
23
25
|
define_method association do
|
24
|
-
|
26
|
+
if polymorphic
|
27
|
+
association_type = self.send(typename)
|
28
|
+
return nil if association_type.nil? || association_type.empty?
|
29
|
+
polymorphic_class_name = LocalModel::Functions.snake_to_camel(association_type.gsub("_type", ""))
|
30
|
+
polymorphic_class_name[0] = polymorphic_class_name[0].upcase
|
31
|
+
association_class = Object.const_get(polymorphic_class_name)
|
32
|
+
else
|
33
|
+
association_class = Object.const_get(association_class_name)
|
34
|
+
end
|
25
35
|
id = self.send(keyname)
|
26
36
|
association_class.find(id)
|
27
37
|
end
|
28
38
|
|
29
39
|
define_method "#{association}=" do |association_obj|
|
30
40
|
self.send("#{keyname}=", association_obj&.id)
|
41
|
+
if polymorphic
|
42
|
+
if !association_obj.nil?
|
43
|
+
self.send("#{typename}=", association_obj.class.to_s)
|
44
|
+
end
|
45
|
+
end
|
31
46
|
end
|
32
47
|
end
|
33
48
|
|
34
|
-
def self.has_many(association, through: nil, class_name: nil, foreign_key: nil)
|
49
|
+
def self.has_many(association, through: nil, class_name: nil, foreign_key: nil, as: nil)
|
35
50
|
if class_name.nil?
|
36
51
|
association_classname = namespace_classname(get_classname_from_association(association))
|
37
52
|
else
|
@@ -39,15 +54,25 @@ class LocalModel::Model
|
|
39
54
|
end
|
40
55
|
|
41
56
|
if through.nil?
|
42
|
-
|
57
|
+
if as.nil?
|
58
|
+
current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
|
59
|
+
else
|
60
|
+
current_class_id_methodname = "#{as}_id"
|
61
|
+
end
|
43
62
|
belongs_to_id_sym = current_class_id_methodname.to_sym
|
44
63
|
add_to_collection = Proc.new do |arg, model|
|
45
64
|
arg.send("#{belongs_to_id_sym}=", model.id)
|
46
65
|
end
|
66
|
+
|
47
67
|
define_method association do
|
68
|
+
collection_args = {belongs_to_id_sym => self.id}
|
69
|
+
if !as.nil?
|
70
|
+
collection_args["#{as}_type".to_sym] = self.class.to_s
|
71
|
+
end
|
72
|
+
|
48
73
|
association_class = Object.const_get(association_classname)
|
49
74
|
LocalModel::Collection.create_from(
|
50
|
-
array: association_class.where(
|
75
|
+
array: association_class.where(**collection_args),
|
51
76
|
for_model: self,
|
52
77
|
for_collection_class: association_class,
|
53
78
|
add_to_collection_proc: add_to_collection
|
@@ -75,7 +100,7 @@ class LocalModel::Model
|
|
75
100
|
end
|
76
101
|
end
|
77
102
|
|
78
|
-
def self.has_one(association, through: nil, class_name: nil, foreign_key: nil)
|
103
|
+
def self.has_one(association, through: nil, class_name: nil, foreign_key: nil, as: nil)
|
79
104
|
if class_name.nil?
|
80
105
|
association_classname = LocalModel::Functions.snake_to_camel(association)
|
81
106
|
association_classname[0] = association_classname[0].upcase
|
@@ -85,11 +110,19 @@ class LocalModel::Model
|
|
85
110
|
end
|
86
111
|
|
87
112
|
if through.nil?
|
88
|
-
|
113
|
+
if as.nil?
|
114
|
+
current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
|
115
|
+
else
|
116
|
+
current_class_id_methodname = "#{as}_id"
|
117
|
+
end
|
89
118
|
belongs_to_id_sym = current_class_id_methodname.to_sym
|
90
119
|
define_method association do
|
120
|
+
collection_args = {belongs_to_id_sym => self.id}
|
121
|
+
if !as.nil?
|
122
|
+
collection_args["#{as}_type".to_sym] = self.class.to_s
|
123
|
+
end
|
91
124
|
association_class = Object.const_get(association_classname)
|
92
|
-
association_class.where(
|
125
|
+
association_class.where(**collection_args).first
|
93
126
|
end
|
94
127
|
else
|
95
128
|
current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(association)}_id"
|
@@ -132,6 +165,29 @@ class LocalModel::Model
|
|
132
165
|
"#{LocalModel.path}#{slash}#{self}.csv"
|
133
166
|
end
|
134
167
|
|
168
|
+
def update(**args)
|
169
|
+
args.each do |k,v|
|
170
|
+
self.send("#{k.to_s}=", v)
|
171
|
+
end
|
172
|
+
self.save
|
173
|
+
end
|
174
|
+
|
175
|
+
def update!(**args)
|
176
|
+
args.each do |k,v|
|
177
|
+
self.send("#{k.to_s}=", v)
|
178
|
+
end
|
179
|
+
self.save!
|
180
|
+
end
|
181
|
+
|
182
|
+
def reload
|
183
|
+
self.class.find(self.id)
|
184
|
+
end
|
185
|
+
|
186
|
+
def ==(other)
|
187
|
+
self.class == other.class &&
|
188
|
+
self.id == other.id
|
189
|
+
end
|
190
|
+
|
135
191
|
|
136
192
|
class SchemaBuilder
|
137
193
|
|
data/lib/local_model/version.rb
CHANGED
data/lib/local_model.rb
CHANGED
@@ -39,6 +39,13 @@ module LocalModel
|
|
39
39
|
@@path
|
40
40
|
end
|
41
41
|
|
42
|
+
def self.db_drop
|
43
|
+
Dir.foreach(@@path) do |f|
|
44
|
+
fn = File.join(@@path, f)
|
45
|
+
File.delete(fn) if f != '.' && f != '..'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
42
49
|
def self.config(&block)
|
43
50
|
configuration = Configuration.new
|
44
51
|
if block_given?
|
@@ -48,10 +55,7 @@ module LocalModel
|
|
48
55
|
@@namespace = configuration.namespace
|
49
56
|
Dir.mkdir(configuration.path) unless Dir.exist?(configuration.path)
|
50
57
|
if configuration.cleanup_on_start
|
51
|
-
|
52
|
-
fn = File.join(configuration.path, f)
|
53
|
-
File.delete(fn) if f != '.' && f != '..'
|
54
|
-
end
|
58
|
+
db_drop
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: local_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Shute
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
112
|
requirements: []
|
113
|
-
rubygems_version: 3.
|
113
|
+
rubygems_version: 3.3.7
|
114
114
|
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: Easily set up a schema persisted with CSV but interactable with AR-like methods
|