local_model 0.1.0 → 0.1.2
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 +0 -1
- data/Gemfile.lock +1 -3
- data/lib/local_model.rb +0 -1
- data/lib/local_model/model.rb +47 -12
- data/lib/local_model/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9a39edf5e4b5b1dc71f82ff08b04f01c57aa284a0bbb92a93194d70268c803c2
|
|
4
|
+
data.tar.gz: 237feb8dc6a943b6aacecdc0ca6774c8b01e1edd7d6e055401c7a24de784ada3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 962ee92001bc3d6a0a0fbd1b9353f60cc957fe44456f3a62d2b5dcde9e0c88e413b585bb224df64753720ef4956d666c09f26a99cdd2f00fa53c24fc446d9d35
|
|
7
|
+
data.tar.gz: 8eac9cb208772976ccabe5efae4345a924662584d2c027e446c303e0a4a7e687546cd73b9507f1c7cdad123ac9b81e5e1a0760f0ecbf19e40990d8c9c267ac31
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
local_model (0.1.
|
|
4
|
+
local_model (0.1.1)
|
|
5
5
|
|
|
6
6
|
GEM
|
|
7
7
|
remote: https://rubygems.org/
|
|
@@ -18,7 +18,6 @@ GEM
|
|
|
18
18
|
rake (13.0.3)
|
|
19
19
|
reline (0.2.5)
|
|
20
20
|
io-console (~> 0.5)
|
|
21
|
-
require_all (3.0.0)
|
|
22
21
|
rspec (3.10.0)
|
|
23
22
|
rspec-core (~> 3.10.0)
|
|
24
23
|
rspec-expectations (~> 3.10.0)
|
|
@@ -42,7 +41,6 @@ DEPENDENCIES
|
|
|
42
41
|
local_model!
|
|
43
42
|
pry (~> 0.14.1)
|
|
44
43
|
rake (~> 13.0)
|
|
45
|
-
require_all (~> 3.0)
|
|
46
44
|
rspec (~> 3.10)
|
|
47
45
|
|
|
48
46
|
BUNDLED WITH
|
data/lib/local_model.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
require 'pry'
|
|
2
2
|
require_relative "./local_model/version"
|
|
3
3
|
require 'csv'
|
|
4
|
-
require 'require_all'
|
|
5
4
|
require_relative './local_model/adapters/boolean_adapter'
|
|
6
5
|
require_relative './local_model/adapters/datetime_adapter'
|
|
7
6
|
require_relative './local_model/adapters/float_adapter'
|
data/lib/local_model/model.rb
CHANGED
|
@@ -8,7 +8,8 @@ class LocalModel::Model
|
|
|
8
8
|
def self.belongs_to(association)
|
|
9
9
|
define_method association do
|
|
10
10
|
id = self.send("#{association}_id")
|
|
11
|
-
association_class_name = LocalModel::Functions.snake_to_camel(association)
|
|
11
|
+
association_class_name = LocalModel::Functions.snake_to_camel(association)
|
|
12
|
+
association_class_name[0] = association_class_name[0].upcase
|
|
12
13
|
association_class = Object.const_get(association_class_name)
|
|
13
14
|
association_class.find(id)
|
|
14
15
|
end
|
|
@@ -18,23 +19,57 @@ class LocalModel::Model
|
|
|
18
19
|
end
|
|
19
20
|
end
|
|
20
21
|
|
|
21
|
-
def self.has_many(association)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
current_class_id_methodname = "#{LocalModel::Functions.camel_to_snake(self.to_s)}_id"
|
|
22
|
+
def self.has_many(association, through: nil, class_name: nil, foreign_key: nil)
|
|
23
|
+
if class_name.nil?
|
|
24
|
+
association_classname = get_classname_from_association(association)
|
|
25
|
+
else
|
|
26
|
+
association_classname = class_name
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(self.to_s)}_id"
|
|
29
30
|
belongs_to_id_sym = current_class_id_methodname.to_sym
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
if through.nil?
|
|
33
|
+
define_method association do
|
|
34
|
+
association_class = Object.const_get(association_classname)
|
|
35
|
+
association_class.where(belongs_to_id_sym => self.id)
|
|
36
|
+
end
|
|
37
|
+
else
|
|
38
|
+
through_classname = get_classname_from_association(through)
|
|
39
|
+
define_method association do
|
|
40
|
+
through_class = Object.const_get(through_classname)
|
|
41
|
+
through_class.where(belongs_to_id_sym => self.id).map{|obj| obj.send(LocalModel::Functions.singularize(association))}
|
|
42
|
+
end
|
|
34
43
|
end
|
|
44
|
+
end
|
|
35
45
|
|
|
46
|
+
def self.has_one(association, class_name: nil, foreign_key: nil)
|
|
47
|
+
if class_name.nil?
|
|
48
|
+
association_classname = LocalModel::Functions.snake_to_camel(association)
|
|
49
|
+
association_classname[0] = association_classname[0].upcase
|
|
50
|
+
else
|
|
51
|
+
association_classname = class_name
|
|
52
|
+
end
|
|
53
|
+
current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(self.to_s)}_id"
|
|
54
|
+
belongs_to_id_sym = current_class_id_methodname.to_sym
|
|
55
|
+
|
|
56
|
+
define_method association do
|
|
57
|
+
association_class = Object.const_get(association_classname)
|
|
58
|
+
association_class.where(belongs_to_id_sym => self.id).first
|
|
59
|
+
end
|
|
60
|
+
end
|
|
36
61
|
|
|
62
|
+
def self.get_classname_from_association(association)
|
|
63
|
+
association_portions = association.to_s.split('_')
|
|
64
|
+
association_portions_last = association_portions.last
|
|
65
|
+
singular_association_last = LocalModel::Functions.singularize(association_portions_last)
|
|
66
|
+
singularized_association = association_portions[0...-1] + [singular_association_last]
|
|
67
|
+
singularized_snakecase = singularized_association.join('_')
|
|
68
|
+
classname = LocalModel::Functions.snake_to_camel(singularized_snakecase)
|
|
69
|
+
classname[0] = classname[0].upcase
|
|
70
|
+
classname
|
|
37
71
|
end
|
|
72
|
+
private_class_method :get_classname_from_association
|
|
38
73
|
|
|
39
74
|
|
|
40
75
|
def self.storage_path
|
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.2
|
|
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-05-
|
|
11
|
+
date: 2021-05-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|