local_model 0.1.9 → 0.1.10
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/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/local_model.rb +1 -0
- data/lib/local_model/collection.rb +28 -0
- data/lib/local_model/model.rb +23 -4
- data/lib/local_model/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8851a543d6b05aff962d00d160c7e38c5d770e880b38131bb7f417e51c7e1a35
|
4
|
+
data.tar.gz: 20e8e5b027a36a7e4a99d142a21a07797563ca024ec3e24be1c55e8b14b663e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c75fb02836ac60ec9f916fe545a8d1af7ba95895f94f217379ef9086e2140f7489b4ad879b5efcf7a183be666e2a0ec4beb38854556fff96e87eeb2423d79101
|
7
|
+
data.tar.gz: c9c72999f0f71fc98ba75563cbacf4114678dc4e9343cdbe49327db35564c2324633f1fc08edd348790e2df795fb7c3dbaaed41c876d49dcd2b9ccb73a1be9ba
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -81,7 +81,7 @@ As of now, supports:
|
|
81
81
|
- #destroy, .find, .create, .new, .where, .first, .second, .last, relationships, updating,
|
82
82
|
|
83
83
|
Does not support yet (notably):
|
84
|
-
- .build, .update, validations
|
84
|
+
- .build, .update, validations
|
85
85
|
- object equivalence if gotten from source twice
|
86
86
|
|
87
87
|
|
data/lib/local_model.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require_relative "./local_model/version"
|
2
2
|
require 'csv'
|
3
3
|
require_relative './local_model/sandbox'
|
4
|
+
require_relative './local_model/collection'
|
4
5
|
require_relative './local_model/adapters/boolean_adapter'
|
5
6
|
require_relative './local_model/adapters/datetime_adapter'
|
6
7
|
require_relative './local_model/adapters/float_adapter'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class LocalModel::Collection < Array
|
2
|
+
|
3
|
+
def self.create_from(array: , for_model: , for_collection_class:, add_to_collection_proc:)
|
4
|
+
new_obj = new(array)
|
5
|
+
new_obj.model = for_model
|
6
|
+
new_obj.collection_class = for_collection_class
|
7
|
+
new_obj.add_to_collection = add_to_collection_proc
|
8
|
+
new_obj
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_accessor :model, :collection_class, :add_to_collection
|
12
|
+
|
13
|
+
def <<(arg)
|
14
|
+
self.push(arg)
|
15
|
+
end
|
16
|
+
|
17
|
+
def push(arg)
|
18
|
+
self.[]=(self.length, arg)
|
19
|
+
raise ArgumentError.new("#{arg.class} inputted, expecting #{self.collection_class}") if !arg.is_a?(self.collection_class)
|
20
|
+
add_to_collection[arg, self.model]
|
21
|
+
arg.save && self.model.save
|
22
|
+
end
|
23
|
+
|
24
|
+
def build(arg)
|
25
|
+
self.push(arg)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/lib/local_model/model.rb
CHANGED
@@ -27,7 +27,7 @@ class LocalModel::Model
|
|
27
27
|
end
|
28
28
|
|
29
29
|
define_method "#{association}=" do |association_obj|
|
30
|
-
self.send("#{keyname}=", association_obj
|
30
|
+
self.send("#{keyname}=", association_obj&.id)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -38,20 +38,39 @@ class LocalModel::Model
|
|
38
38
|
association_classname = namespace_classname(class_name)
|
39
39
|
end
|
40
40
|
|
41
|
-
|
42
41
|
if through.nil?
|
43
42
|
current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
|
44
43
|
belongs_to_id_sym = current_class_id_methodname.to_sym
|
44
|
+
add_to_collection = Proc.new do |arg, model|
|
45
|
+
arg.send("#{belongs_to_id_sym}=", model.id)
|
46
|
+
end
|
45
47
|
define_method association do
|
46
48
|
association_class = Object.const_get(association_classname)
|
47
|
-
|
49
|
+
LocalModel::Collection.create_from(
|
50
|
+
array: association_class.where(belongs_to_id_sym => self.id),
|
51
|
+
for_model: self,
|
52
|
+
for_collection_class: association_class,
|
53
|
+
add_to_collection_proc: add_to_collection
|
54
|
+
)
|
48
55
|
end
|
49
56
|
else
|
50
57
|
current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(LocalModel::Functions.singularize(association))}_id"
|
51
58
|
belongs_to_id_sym = current_class_id_methodname.to_sym
|
59
|
+
add_to_collection = Proc.new do |arg, model|
|
60
|
+
through_collection = model.send(through)
|
61
|
+
through_classname = through_collection.collection_class
|
62
|
+
new_join = through_classname.new
|
63
|
+
new_join.send("#{belongs_to_id_sym}=", arg.id)
|
64
|
+
through_collection << new_join
|
65
|
+
end
|
52
66
|
define_method association do
|
53
67
|
association_class = Object.const_get(association_classname)
|
54
|
-
|
68
|
+
LocalModel::Collection.create_from(
|
69
|
+
array: self.send(through).map{|through_obj| association_class.find(through_obj.send(belongs_to_id_sym))},
|
70
|
+
for_model: self,
|
71
|
+
for_collection_class: association_class,
|
72
|
+
add_to_collection_proc: add_to_collection
|
73
|
+
)
|
55
74
|
end
|
56
75
|
end
|
57
76
|
end
|
data/lib/local_model/version.rb
CHANGED
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.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Shute
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- lib/local_model/adapters/float_adapter.rb
|
78
78
|
- lib/local_model/adapters/integer_adapter.rb
|
79
79
|
- lib/local_model/adapters/string_adapter.rb
|
80
|
+
- lib/local_model/collection.rb
|
80
81
|
- lib/local_model/concerns/csv_interactable.rb
|
81
82
|
- lib/local_model/csv.rb
|
82
83
|
- lib/local_model/helpers/functions.rb
|