local_model 0.1.3 → 0.1.8

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: a86bbe76f369c6f1b0a3984edbb58dcbc070dee5211afd9e50f22b1deb72f8bf
4
- data.tar.gz: cdda97bc5fc77dd8b54a628b49f5a7724b01a1f12b37dcb3b570971689b99e10
3
+ metadata.gz: 46c92d2460b4c3db420fe0f4dcc289a07a7ba3320959c2bab48e9d086e96401e
4
+ data.tar.gz: b9bc068f6fc969c7d60424640a86934e25ea2f5d4ea1061f34e0d3bfa706216a
5
5
  SHA512:
6
- metadata.gz: fe2ca903606f84595f7decd590d994a4c48885b7c0bd7a4a86d67603cda157b3ebede305e30368abdaa5db694bd688a6ecf08aacd351818e2f7ebfe02bb9f8cf
7
- data.tar.gz: fa5d4d01e42968f74e0323c6173e95dc21e3a58d9d7e975740e7f082597c7d6728eacb78e84178298039dbe175448cd6cd4672c9a974a3f5124c762ba955ebfd
6
+ metadata.gz: 7965f12ad0e0a84acdb4c7e31c3f33b7f3f3635d9f1a296b34ba22383329837871967cada747fa149a4651e029ffda0136d559630c274079c838abb8a902dfc5
7
+ data.tar.gz: ac3c17e4fd413b4f3d45e441ad6888db5ab3a28c17ea645aecec21976f1742b4d0e5bf5d29583c14cafbba08578b9db100099323b700f7063c5ca89abc2d7e35
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.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -84,6 +84,36 @@ Does not support yet (notably):
84
84
  - .build, .update, validations, #<< to add many relationships
85
85
  - object equivalence if gotten from source twice
86
86
 
87
+
88
+ ### Namespace Methods
89
+
90
+ ```rb
91
+
92
+ LocalModel.config do |c|
93
+ c.path = "/home/me/data/"
94
+ c.cleanup_on_start = true # delete ALL files in path on startup
95
+ c.namespace = "InMemory" # Expect ALL LocalModels to be namespaced via this string
96
+ end
97
+
98
+ ```
99
+
100
+ ```rb
101
+ # in_memory/pokemon.rb
102
+
103
+ class InMemory::Pokemon < LocalModel::CSV
104
+
105
+ schema do |t|
106
+ t.string :name
107
+ t.string :nickname
108
+ t.integer :hp
109
+ t.string :species
110
+ end
111
+
112
+ end
113
+
114
+
115
+ ```
116
+
87
117
  ## Development
88
118
 
89
119
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/local_model.rb CHANGED
@@ -45,7 +45,7 @@ module LocalModel
45
45
  @@namespace = configuration.namespace
46
46
  Dir.mkdir(configuration.path) unless Dir.exist?(configuration.path)
47
47
  if configuration.cleanup_on_start
48
- Dir.foreach do |f|
48
+ Dir.foreach(configuration.path) do |f|
49
49
  fn = File.join(configuration.path, f)
50
50
  File.delete(fn) if f != '.' && f != '..'
51
51
  end
@@ -11,6 +11,7 @@ class LocalModel::Functions
11
11
  end
12
12
 
13
13
  def self.singularize(word)
14
+ word = word.to_s
14
15
  return PluralizedWords::IRREGULAR_SINGULARIZED_WORDS[word] if PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word]
15
16
  if word[-1] == "i"
16
17
  "#{word[0...-1]}us"
@@ -33,6 +34,7 @@ class LocalModel::Functions
33
34
  end
34
35
 
35
36
  def self.pluralize(word)
37
+ word = word.to_s
36
38
  return PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word] if PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word]
37
39
  if word[-2..-1] == "us"
38
40
  "#{word[0...-2]}i"
@@ -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.3"
2
+ VERSION = "0.1.8"
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.3
4
+ version: 0.1.8
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