local_model 0.1.13 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e778e1ced2f3d028e36ac5363e110343ee1f290fe97f23fcf2005ee3a38cfdb
4
- data.tar.gz: b5d80e4c968830806dfc33e23f7bd72000c1382c59bb22dc96b952cf9a949766
3
+ metadata.gz: 52fd36165c3ec49f275a899b9c687e99e91942c64d603542f5295dfbd6b4070a
4
+ data.tar.gz: 26992202b57f21466187744566b20f1be8a2701d15a3cb6975dcd4f5f389d902
5
5
  SHA512:
6
- metadata.gz: cc73cfe46da4e2fd03918604b0a80dba92d678a7ab8101671b777855b31a16fc07c20469fa7a5d01b9acc179f07c99448467d58193193ee7088d92c105999d70
7
- data.tar.gz: 470ed0d894f193ea64e63cb2bea0a06949769080712a91454470315cceae74d9172f5fe3944daf4ac4186de563b05ac4acdc78f556d1f74baba7ec41314ee38e
6
+ metadata.gz: 34dfb6506d823c13ecbe82869a7a60d3047e3842df78af576e7cf8b463422736bd95e103ef38b5ce57373e03f3a9d957ba83235984caec258aa593b348206d39
7
+ data.tar.gz: 989578a3bab8d8bb7d60fc1556b2f28c0c0e718f9c2d0054b178c1e2a460857fba0108198ce9ed13c5c0b7adec08a9f3ce21eaf8e3cb46bca9d0f871f754a83f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- local_model (0.1.12)
4
+ local_model (0.1.13)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,9 +1,5 @@
1
1
  # LocalModel
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/local_model`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
3
  ## Installation
8
4
 
9
5
  Add this line to your application's Gemfile:
@@ -22,6 +18,36 @@ Or install it yourself as:
22
18
 
23
19
  ## Usage
24
20
 
21
+ Use the generator:
22
+
23
+ ```bash
24
+ bundle add local_model
25
+ ```
26
+ ```
27
+ bundle exec local_model --namespace DesiredNamespace
28
+ ```
29
+ Note: `--namespace` is optional and will default to `InMemory`
30
+ This creates a rails initializer which sets some config options,
31
+ and a `DataAccessor` class which you can use to switch between your `LocalModel` objects and your `ActiveRecord` objects.
32
+
33
+ Recommend adding:
34
+
35
+ `config/application.rb`
36
+ ```ruby
37
+ config.autoload_paths << config.root.join('lib')
38
+ ```
39
+
40
+ so you get all of these files.
41
+
42
+ You can set an environment variable `USE_LOCAL_MODELS` to `true` or `false` to globally decide what to use.
43
+
44
+
45
+ You also now can use a rake task to generate your `LocalModel` models:
46
+ `rake local_model:create_model\[User\]`
47
+ (The `\`s should only be necessary in zsh, not bash)
48
+
49
+ OR, you can manually:
50
+
25
51
  Create your schema:
26
52
 
27
53
  ```rb
@@ -5,7 +5,8 @@ class LocalModel::FloatAdapter
5
5
  end
6
6
 
7
7
  def self.read(flstr)
8
- flstr =~ /[^0-9\.]/ ? nil : flstr.to_f
8
+ str = flstr.strip
9
+ str =~ /^[+-]?([1-9]\d*|0)(\.\d+)?$/ ? str.to_f : nil
9
10
  end
10
11
 
11
12
  end
@@ -26,7 +26,7 @@ class LocalModel::Collection < Array
26
26
  end
27
27
 
28
28
  def where(**args)
29
- self.filter do |el|
29
+ arr = self.filter do |el|
30
30
  found = true
31
31
  args.each do |k,v|
32
32
  if el.send(k.to_s) != v
@@ -36,6 +36,24 @@ class LocalModel::Collection < Array
36
36
  end
37
37
  found
38
38
  end
39
+ self.class.create_from(
40
+ array: arr,
41
+ for_model: model,
42
+ for_collection_class: nil,
43
+ add_to_collection_proc: ->(a,b) { raise NoMethodError.new("Cannot add to this collection") }
44
+ )
45
+ end
46
+
47
+ def find_by(**args)
48
+ where(**args).first
49
+ end
50
+
51
+ def first
52
+ self[0]
53
+ end
54
+
55
+ def last
56
+ self[-1]
39
57
  end
40
58
 
41
59
  end
@@ -37,6 +37,10 @@ class LocalModel::CSV < LocalModel::Model
37
37
  schema_data
38
38
  end
39
39
 
40
+ self.define_method :schema_defaults do
41
+ builder.defaults
42
+ end
43
+
40
44
  if !File.file?(self.storage_path)
41
45
  CSV.open(self.storage_path, 'wb') do |csv|
42
46
  csv << cols.map(&:to_s)
@@ -63,7 +67,7 @@ class LocalModel::CSV < LocalModel::Model
63
67
  end
64
68
 
65
69
  def self.where(**args)
66
- all_records do |row|
70
+ arr = all_records do |row|
67
71
  found = true
68
72
  args.each do |k,v|
69
73
  if row[k] != v
@@ -73,6 +77,12 @@ class LocalModel::CSV < LocalModel::Model
73
77
  end
74
78
  found
75
79
  end.map{|r| new_from_record(r) }
80
+ LocalModel::Collection.create_from(
81
+ array: arr,
82
+ for_model: self,
83
+ for_collection_class: nil,
84
+ add_to_collection_proc: ->(a,b) { raise NoMethodError.new("Cannot add to this collection") }
85
+ )
76
86
  end
77
87
 
78
88
  def self.find_by(**args)
@@ -108,11 +118,7 @@ class LocalModel::CSV < LocalModel::Model
108
118
  end
109
119
 
110
120
  def self.find(id)
111
- return self.find_by(id: id)
112
- end
113
-
114
- def self.find!(id)
115
- found_record = find(id)
121
+ found_record = self.find_by(id: id)
116
122
  if !found_record
117
123
  raise LocalModel::RecordNotFound.new
118
124
  else
@@ -120,6 +126,14 @@ class LocalModel::CSV < LocalModel::Model
120
126
  end
121
127
  end
122
128
 
129
+ def self.find_or_create_by(**args)
130
+ found_obj = find_by(**args)
131
+ return found_obj if found_obj
132
+ create(**args)
133
+ end
134
+
135
+ # TODO: Move necessary methods to model.rb
136
+
123
137
  def self.create(**args)
124
138
  inst = new(**args)
125
139
  inst.save
@@ -133,6 +147,9 @@ class LocalModel::CSV < LocalModel::Model
133
147
  end
134
148
 
135
149
  def initialize(**args)
150
+ self.schema_defaults.each do |property, value|
151
+ self.send("#{property}=", value)
152
+ end
136
153
  args.each do |k,v|
137
154
  self.send("#{k}=", v)
138
155
  end
@@ -145,7 +162,7 @@ class LocalModel::CSV < LocalModel::Model
145
162
 
146
163
  def reload
147
164
  raise LocalModel::RecordNotFound if !self.id
148
- self.class.find!(self.id)
165
+ self.class.find(self.id)
149
166
  end
150
167
 
151
168
  def destroy
@@ -33,7 +33,7 @@ class LocalModel::Model
33
33
  association_class = Object.const_get(association_class_name)
34
34
  end
35
35
  id = self.send(keyname)
36
- association_class.find(id)
36
+ association_class.find_by(id: id)
37
37
  end
38
38
 
39
39
  define_method "#{association}=" do |association_obj|
@@ -91,7 +91,7 @@ class LocalModel::Model
91
91
  define_method association do
92
92
  association_class = Object.const_get(association_classname)
93
93
  LocalModel::Collection.create_from(
94
- array: self.send(through).map{|through_obj| association_class.find(through_obj.send(belongs_to_id_sym))},
94
+ array: self.send(through).map{|through_obj| association_class.find_by(id: through_obj.send(belongs_to_id_sym))},
95
95
  for_model: self,
96
96
  for_collection_class: association_class,
97
97
  add_to_collection_proc: add_to_collection
@@ -192,37 +192,47 @@ class LocalModel::Model
192
192
  class SchemaBuilder
193
193
 
194
194
  attr_reader :schema
195
+ attr_reader :defaults
195
196
 
196
197
  def initialize(model)
197
198
  @model = model
198
199
  @schema = { id: :integer }
199
200
  @model.attr_accessor :id
201
+ @defaults = {}
200
202
  end
201
203
 
202
- def string(name)
204
+ def set_default(name, default)
205
+ @defaults[name] = default if !default.nil?
206
+ end
207
+
208
+ def string(name, default: nil)
203
209
  @model.attr_accessor name
204
210
  @schema[name] = :string
211
+ set_default(name, default)
205
212
  end
206
213
 
207
- def integer(name)
214
+ def integer(name, default: nil)
208
215
  @model.attr_accessor name
209
216
  @schema[name] = :integer
217
+ set_default(name, default)
210
218
  end
211
219
 
212
- def boolean(name)
220
+ def boolean(name, default: nil)
213
221
  @model.attr_accessor name
214
222
  @schema[name] = :boolean
223
+ set_default(name, default)
215
224
  end
216
225
 
217
- def float(name)
226
+ def float(name, default: nil)
218
227
  @model.attr_accessor name
219
228
  @schema[name] = :float
229
+ set_default(name, default)
220
230
  end
221
231
 
222
- def datetime(name)
232
+ def datetime(name, default: nil)
223
233
  @model.attr_accessor name
224
234
  @schema[name] = :datetime
235
+ set_default(name, default)
225
236
  end
226
-
227
237
  end
228
- end
238
+ end
@@ -1,3 +1,3 @@
1
1
  module LocalModel
2
- VERSION = "0.1.13"
2
+ VERSION = "0.1.14"
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.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Shute
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-17 00:00:00.000000000 Z
11
+ date: 2022-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler