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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1f1bc15ddc521bde62f5f54c0a2d1afd83fce6cb48f6d219a2f51ab10bcca5a
4
- data.tar.gz: 68bba359571444d2642144a08f8048eaa6dc0576968a92a896dc8e52b1258f49
3
+ metadata.gz: 9a39edf5e4b5b1dc71f82ff08b04f01c57aa284a0bbb92a93194d70268c803c2
4
+ data.tar.gz: 237feb8dc6a943b6aacecdc0ca6774c8b01e1edd7d6e055401c7a24de784ada3
5
5
  SHA512:
6
- metadata.gz: 9ad5e42e5270c50e4e05d8ef678fb59885efd9fbd15acbd5e2ce76c4f47ad46ab63e8aef0259257db6fa36b510853bf78e72c097d2319a5a6ab33ecd2dfdbe81
7
- data.tar.gz: d43a6e529cd6670cbbac2711db14af646d68f0fb40616d86c0347d9491f4acf7d3063ab0fca191f42cf1848cca1801d87d87d7b0347695abcaace0a143dad347
6
+ metadata.gz: 962ee92001bc3d6a0a0fbd1b9353f60cc957fe44456f3a62d2b5dcde9e0c88e413b585bb224df64753720ef4956d666c09f26a99cdd2f00fa53c24fc446d9d35
7
+ data.tar.gz: 8eac9cb208772976ccabe5efae4345a924662584d2c027e446c303e0a4a7e687546cd73b9507f1c7cdad123ac9b81e5e1a0760f0ecbf19e40990d8c9c267ac31
data/Gemfile CHANGED
@@ -5,7 +5,6 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5
5
  # Specify your gem's dependencies in local_model.gemspec
6
6
  gemspec
7
7
 
8
- gem "require_all", "~> 3.0"
9
8
 
10
9
  gem "pry", "~> 0.14.1", group: :development
11
10
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- local_model (0.1.0)
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'
@@ -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).capitalize
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
- association_portions = association.to_s.split('_')
23
- association_portions_last = association_portions.last
24
- singular_association_last = LocalModel::Functions.singularize(association_portions_last)
25
- singularized_association = association_portions[0...-1] + [singular_association_last]
26
- singularized_snakecase = singularized_association.join('_')
27
- association_classname = LocalModel::Functions.snake_to_camel(singularized_snakecase).capitalize
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
- define_method association do
32
- association_class = Object.const_get(association_classname)
33
- association_class.where(belongs_to_id_sym => self.id)
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
@@ -1,3 +1,3 @@
1
1
  module LocalModel
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
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.0
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-10 00:00:00.000000000 Z
11
+ date: 2021-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler