local_model 0.1.4 → 0.1.9

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: 32aaa0557b37154955a584e86ddafab8b7dc5dac3731f77edb0817beeb511739
4
- data.tar.gz: 731283ec3e8f5577551247b006d29f919cc22dc5bbef5726ee60e008f19fe35b
3
+ metadata.gz: acc6c4975bec354dd97d2a2ef5c320a9525437153b405e484970aa4542e024a7
4
+ data.tar.gz: dc6a281ff86af1acf3ec1a414cc75342d13466393773efe90a705f30b175ae63
5
5
  SHA512:
6
- metadata.gz: af60390ff697de0be4c0b873af689ce0ad9f8169a883fab25a5b2ff933ce56e3603fa602d76fae1c36f966c71392eb3844c117f6614d228bde7a0c9a5aebc073
7
- data.tar.gz: d1e7e26d09cbd221f29ac75ba885577fa78ed01170aeaf1a9c09501b1046f2824a4a1ac807a2ff22373a8c39a584aba4f749795bdc5e7d5d8ba6f29bda1bbb4d
6
+ metadata.gz: 20d6be57b31cba2fa7f4c87a7f10235ae923e676d4a65ccc73fdf239c0c01fa757f219f3df138ffc68c41ecc6969ab266d5b8e92cd61e3b49ede6e20654c1881
7
+ data.tar.gz: 0ed5744beff0da65ad2aecff072d06ddf609d75203409d79c7ba934056c7d2508ae68628721bb26a5eda6ac43f76587c66fac1870ec5e10bb1b06e88947e010d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- local_model (0.1.2)
4
+ local_model (0.1.8)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -11,7 +11,8 @@ class LocalModel::Functions
11
11
  end
12
12
 
13
13
  def self.singularize(word)
14
- return PluralizedWords::IRREGULAR_SINGULARIZED_WORDS[word] if PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word]
14
+ word = word.to_s
15
+ return LocalModel::PluralizedWords::IRREGULAR_SINGULARIZED_WORDS[word] if LocalModel::PluralizedWords::IRREGULAR_SINGULARIZED_WORDS[word]
15
16
  if word[-1] == "i"
16
17
  "#{word[0...-1]}us"
17
18
  elsif word[-1] == "a"
@@ -33,7 +34,8 @@ class LocalModel::Functions
33
34
  end
34
35
 
35
36
  def self.pluralize(word)
36
- return PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word] if PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word]
37
+ word = word.to_s
38
+ return LocalModel::PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word] if LocalModel::PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word]
37
39
  if word[-2..-1] == "us"
38
40
  "#{word[0...-2]}i"
39
41
  elsif word[-2..-1] == "on"
@@ -1,4 +1,4 @@
1
- class PluralizedWords
1
+ class LocalModel::PluralizedWords
2
2
 
3
3
  IRREGULAR_SINGULARIZED_WORDS = {
4
4
  "children" => "child",
@@ -20,7 +20,11 @@ class PluralizedWords
20
20
  "beliefs" => "belief",
21
21
  "chefs" => "chef",
22
22
  "chiefs" => "chief",
23
- "gasses" => "gas"
23
+ "gasses" => "gas",
24
+ "moves" => "move",
25
+ "groves" => "grove",
26
+ "stoves" => "stove",
27
+ "beliefs" => "belief"
24
28
  }
25
29
 
26
30
  IRREGULAR_PLURALIZED_WORDS = {
@@ -43,7 +47,7 @@ class PluralizedWords
43
47
  "belief" => "beliefs",
44
48
  "chef" => "chefs",
45
49
  "chief" => "chiefs",
46
- "gas" => "gasses"
50
+ "gas" => "gasses",
47
51
  }
48
52
 
49
53
  end
@@ -5,19 +5,29 @@ class LocalModel::Model
5
5
  # yield(SchemaBuilder.new(self))
6
6
  end
7
7
 
8
- def self.belongs_to(association)
9
- association_class_name = LocalModel::Functions.snake_to_camel(association)
10
- association_class_name[0] = association_class_name[0].upcase
11
- association_class_name = namespace_classname(association_class_name)
12
- association_class = Object.const_get(association_class_name)
8
+ def self.belongs_to(association, class_name: nil, foreign_key: nil)
9
+ if class_name.nil?
10
+ association_class_name = LocalModel::Functions.snake_to_camel(association)
11
+ association_class_name[0] = association_class_name[0].upcase
12
+ association_class_name = namespace_classname(association_class_name)
13
+ else
14
+ association_class_name = namespace_classname(class_name)
15
+ end
16
+
17
+ if foreign_key.nil?
18
+ keyname = "#{association}_id"
19
+ else
20
+ keyname = foreign_key
21
+ end
13
22
 
14
23
  define_method association do
15
- id = self.send("#{association}_id")
24
+ association_class = Object.const_get(association_class_name)
25
+ id = self.send(keyname)
16
26
  association_class.find(id)
17
27
  end
18
28
 
19
29
  define_method "#{association}=" do |association_obj|
20
- self.send("#{association}_id=", association_obj.id)
30
+ self.send("#{keyname}=", association_obj.id)
21
31
  end
22
32
  end
23
33
 
@@ -28,37 +38,47 @@ class LocalModel::Model
28
38
  association_classname = namespace_classname(class_name)
29
39
  end
30
40
 
31
- current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
32
- belongs_to_id_sym = current_class_id_methodname.to_sym
33
41
 
34
42
  if through.nil?
43
+ current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
44
+ belongs_to_id_sym = current_class_id_methodname.to_sym
35
45
  define_method association do
36
46
  association_class = Object.const_get(association_classname)
37
47
  association_class.where(belongs_to_id_sym => self.id)
38
48
  end
39
49
  else
40
- through_classname = namespace_classname(get_classname_from_association(through))
50
+ current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(LocalModel::Functions.singularize(association))}_id"
51
+ belongs_to_id_sym = current_class_id_methodname.to_sym
41
52
  define_method association do
42
- through_class = Object.const_get(through_classname)
43
- through_class.where(belongs_to_id_sym => self.id).map{|obj| obj.send(LocalModel::Functions.singularize(association))}
53
+ association_class = Object.const_get(association_classname)
54
+ self.send(through).map{|through_obj| association_class.find(through_obj.send(belongs_to_id_sym))}
44
55
  end
45
56
  end
46
57
  end
47
58
 
48
- def self.has_one(association, class_name: nil, foreign_key: nil)
59
+ def self.has_one(association, through: nil, class_name: nil, foreign_key: nil)
49
60
  if class_name.nil?
50
61
  association_classname = LocalModel::Functions.snake_to_camel(association)
51
62
  association_classname[0] = association_classname[0].upcase
52
63
  association_classname = namespace_classname(association_classname)
53
64
  else
54
- association_classname = class_name
65
+ association_classname = namespace_classname(class_name)
55
66
  end
56
- current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
57
- belongs_to_id_sym = current_class_id_methodname.to_sym
58
67
 
59
- define_method association do
60
- association_class = Object.const_get(association_classname)
61
- association_class.where(belongs_to_id_sym => self.id).first
68
+ if through.nil?
69
+ current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
70
+ belongs_to_id_sym = current_class_id_methodname.to_sym
71
+ define_method association do
72
+ association_class = Object.const_get(association_classname)
73
+ association_class.where(belongs_to_id_sym => self.id).first
74
+ end
75
+ else
76
+ current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(association)}_id"
77
+ belongs_to_id_sym = current_class_id_methodname.to_sym
78
+ define_method association do
79
+ association_class = Object.const_get(association_classname)
80
+ association_class.where(id: self.send(through)&.send(belongs_to_id_sym)).first
81
+ end
62
82
  end
63
83
  end
64
84
 
@@ -1,3 +1,3 @@
1
1
  module LocalModel
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.9"
3
3
  end
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
4
+ version: 0.1.9
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-12 00:00:00.000000000 Z
11
+ date: 2021-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler