local_model 0.1.5 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f853000f33467ac6f85e87035eecfcd676640fc6bbaf05457a61637604c9f005
4
- data.tar.gz: f33774550c04178eb2083f9a1c008b1d6e548454a9134077a345a822c274fb27
3
+ metadata.gz: 8851a543d6b05aff962d00d160c7e38c5d770e880b38131bb7f417e51c7e1a35
4
+ data.tar.gz: 20e8e5b027a36a7e4a99d142a21a07797563ca024ec3e24be1c55e8b14b663e2
5
5
  SHA512:
6
- metadata.gz: 7a66632adc5f6c61c0f4d2f9153df4af34427e03cb3dccdcef59c6f4f6fabcdada61e7071ec0b3f4a85679830f57f48f120863677d0077826cf1961ddd55d847
7
- data.tar.gz: 0e38bf60d7b826a97b4b7f7db53740ad3b532cb054dc843eb58f1e4f770cd57b73c915f4ab252d7aeac5b2784192f3d3d32aabd34bce2e6b570ccd09ced43714
6
+ metadata.gz: c75fb02836ac60ec9f916fe545a8d1af7ba95895f94f217379ef9086e2140f7489b4ad879b5efcf7a183be666e2a0ec4beb38854556fff96e87eeb2423d79101
7
+ data.tar.gz: c9c72999f0f71fc98ba75563cbacf4114678dc4e9343cdbe49327db35564c2324633f1fc08edd348790e2df795fb7c3dbaaed41c876d49dcd2b9ccb73a1be9ba
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- local_model (0.1.4)
4
+ local_model (0.1.9)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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, #<< to add many relationships
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
@@ -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
@@ -13,7 +13,6 @@ class LocalModel::Model
13
13
  else
14
14
  association_class_name = namespace_classname(class_name)
15
15
  end
16
- association_class = Object.const_get(association_class_name)
17
16
 
18
17
  if foreign_key.nil?
19
18
  keyname = "#{association}_id"
@@ -22,12 +21,13 @@ class LocalModel::Model
22
21
  end
23
22
 
24
23
  define_method association do
24
+ association_class = Object.const_get(association_class_name)
25
25
  id = self.send(keyname)
26
26
  association_class.find(id)
27
27
  end
28
28
 
29
29
  define_method "#{association}=" do |association_obj|
30
- self.send("#{keyname}=", association_obj.id)
30
+ self.send("#{keyname}=", association_obj&.id)
31
31
  end
32
32
  end
33
33
 
@@ -38,37 +38,66 @@ class LocalModel::Model
38
38
  association_classname = namespace_classname(class_name)
39
39
  end
40
40
 
41
- current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
42
- belongs_to_id_sym = current_class_id_methodname.to_sym
43
-
44
41
  if through.nil?
42
+ current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
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
- association_class.where(belongs_to_id_sym => self.id)
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
- through_classname = namespace_classname(get_classname_from_association(through))
57
+ current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(LocalModel::Functions.singularize(association))}_id"
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
51
66
  define_method association do
52
- through_class = Object.const_get(through_classname)
53
- through_class.where(belongs_to_id_sym => self.id).map{|obj| obj.send(LocalModel::Functions.singularize(association))}
67
+ association_class = Object.const_get(association_classname)
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
+ )
54
74
  end
55
75
  end
56
76
  end
57
77
 
58
- def self.has_one(association, class_name: nil, foreign_key: nil)
78
+ def self.has_one(association, through: nil, class_name: nil, foreign_key: nil)
59
79
  if class_name.nil?
60
80
  association_classname = LocalModel::Functions.snake_to_camel(association)
61
81
  association_classname[0] = association_classname[0].upcase
62
82
  association_classname = namespace_classname(association_classname)
63
83
  else
64
- association_classname = class_name
84
+ association_classname = namespace_classname(class_name)
65
85
  end
66
- current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
67
- belongs_to_id_sym = current_class_id_methodname.to_sym
68
86
 
69
- define_method association do
70
- association_class = Object.const_get(association_classname)
71
- association_class.where(belongs_to_id_sym => self.id).first
87
+ if through.nil?
88
+ current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id"
89
+ belongs_to_id_sym = current_class_id_methodname.to_sym
90
+ define_method association do
91
+ association_class = Object.const_get(association_classname)
92
+ association_class.where(belongs_to_id_sym => self.id).first
93
+ end
94
+ else
95
+ current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(association)}_id"
96
+ belongs_to_id_sym = current_class_id_methodname.to_sym
97
+ define_method association do
98
+ association_class = Object.const_get(association_classname)
99
+ association_class.where(id: self.send(through)&.send(belongs_to_id_sym)).first
100
+ end
72
101
  end
73
102
  end
74
103
 
@@ -1,3 +1,3 @@
1
1
  module LocalModel
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.10"
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.5
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-05-26 00:00:00.000000000 Z
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