anchormodel 0.1.1 → 0.1.3

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: c228e4a1d684b6404a0f1376566680fecb2b458b5e0d5f9df0b41ab11b3facba
4
- data.tar.gz: db3a4648ac8d41ca4bed6d9d540777e4c9a42b9e52e3500df2d5826429ef5b59
3
+ metadata.gz: '01259cd9abb8c08dd46ee9ebc39697fbd8eb17128bd09d8e2ffd20aca88b3c68'
4
+ data.tar.gz: 93eb6c57fae4d705cee6d92e9a5e5a3c5cfd56e3cc60306e906d5654521ae586
5
5
  SHA512:
6
- metadata.gz: d5a8c1d45cba54da1a1061f9c680111cbdfbe00daa33e95883540777f96bfa3f070cadfc07ffaff6898d65d8111004851e4054475e6d611e5a1d48804e635e48
7
- data.tar.gz: bf2fcc0048940551e969112f4ad522743bddc8d4c1fd84c3a45d6fc0161e99dc006db06c0480ebbd19afef3cbaa403d6b9dcbaf4dec294b14271c4de6bdbf697
6
+ metadata.gz: 564faa96c2eea1cc8bcbb3c05dbe9b7c86dd0a62618cff2d3ed56611a3c4a1431e1a2e1bfdaa30e7ccbd1a2a4723648aeb269ed6ef656c44daa01d21488516a3
7
+ data.tar.gz: 18d9556c138412dfa107e9958a3f4bfd8e992b9a26c85bd3516d3bc7afc3bee700c72b7d915fde69787087334be8692bf6afbb77d3dab1bc233c4be7b27ee6d2
data/CHANGELOG.md CHANGED
@@ -1,4 +1,12 @@
1
- # Unreleased
1
+ # 0.1.3 (19.05.2023)
2
+
3
+ - Add Rails generator
4
+
5
+ # 0.1.2 (25.01.2023)
6
+
7
+ - Consider empty string as nil (this is necessary to make Rails assignments work)
8
+
9
+ # 0.1.1 (24.01.2023)
2
10
 
3
11
  - Add boolean accessor directly to all anchormodels
4
12
  - Add ActiveRecord::Enum style readers, writers and scopes to the model
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- anchormodel (0.1.1.edge)
4
+ anchormodel (0.1.2)
5
5
  rails (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -50,6 +50,14 @@ entries of an Anchormodel.
50
50
  1. Add gem to Gemfile: `gem 'anchormodel'`
51
51
  2. In `application_record.rb`, add in the class body: `include Anchormodel::ModelMixin`
52
52
 
53
+ # Generator
54
+
55
+ For convenience, Anchormodel provides a Rails generator:
56
+
57
+ `rails generate anchormodel Role`
58
+
59
+ This will create `app/anchormodels/role.rb`.
60
+
53
61
  # Basic example
54
62
 
55
63
  `app/anchormodels/role.rb`:
data/anchormodel.gemspec CHANGED
@@ -2,48 +2,33 @@
2
2
  # This file is auto-generated via: 'rake gemspec'.
3
3
 
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: anchormodel 0.1.1 ruby lib
5
+ # stub: anchormodel 0.1.3 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "anchormodel".freeze
9
- s.version = "0.1.1"
9
+ s.version = "0.1.3"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Sandro Kalbermatter".freeze]
14
- s.date = "2023-01-24"
15
- s.files = [".gitignore".freeze, ".ruby-version".freeze, ".yardopts".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "Gemfile.lock".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "anchormodel.gemspec".freeze, "bin/rails".freeze, "doc/Anchormodel.html".freeze, "doc/Anchormodel/ActiveModelTypeValue.html".freeze, "doc/Anchormodel/Attribute.html".freeze, "doc/Anchormodel/ModelMixin.html".freeze, "doc/Anchormodel/Version.html".freeze, "doc/_index.html".freeze, "doc/class_list.html".freeze, "doc/css/common.css".freeze, "doc/css/full_list.css".freeze, "doc/css/style.css".freeze, "doc/file.README.html".freeze, "doc/file_list.html".freeze, "doc/frames.html".freeze, "doc/index.html".freeze, "doc/js/app.js".freeze, "doc/js/full_list.js".freeze, "doc/js/jquery.js".freeze, "doc/method_list.html".freeze, "doc/top-level-namespace.html".freeze, "lib/anchormodel.rb".freeze, "lib/anchormodel/active_model_type_value.rb".freeze, "lib/anchormodel/attribute.rb".freeze, "lib/anchormodel/model_mixin.rb".freeze, "lib/anchormodel/version.rb".freeze, "logo.svg".freeze, "test/active_record_model/user_test.rb".freeze, "test/dummy/.gitignore".freeze, "test/dummy/Rakefile".freeze, "test/dummy/app/anchormodels/locale.rb".freeze, "test/dummy/app/anchormodels/role.rb".freeze, "test/dummy/app/helpers/application_helper.rb".freeze, "test/dummy/app/models/application_record.rb".freeze, "test/dummy/app/models/concerns/.keep".freeze, "test/dummy/app/models/user.rb".freeze, "test/dummy/bin/rails".freeze, "test/dummy/bin/rake".freeze, "test/dummy/bin/setup".freeze, "test/dummy/config.ru".freeze, "test/dummy/config/application.rb".freeze, "test/dummy/config/boot.rb".freeze, "test/dummy/config/credentials.yml.enc".freeze, "test/dummy/config/database.yml".freeze, "test/dummy/config/environment.rb".freeze, "test/dummy/config/environments/test.rb".freeze, "test/dummy/config/initializers/content_security_policy.rb".freeze, "test/dummy/config/initializers/filter_parameter_logging.rb".freeze, "test/dummy/config/initializers/inflections.rb".freeze, "test/dummy/config/initializers/permissions_policy.rb".freeze, "test/dummy/config/locales/en.yml".freeze, "test/dummy/config/puma.rb".freeze, "test/dummy/config/routes.rb".freeze, "test/dummy/db/migrate/20230107173151_create_users.rb".freeze, "test/dummy/db/schema.rb".freeze, "test/dummy/db/seeds.rb".freeze, "test/dummy/lib/tasks/.keep".freeze, "test/dummy/log/.keep".freeze, "test/dummy/tmp/.keep".freeze, "test/dummy/tmp/pids/.keep".freeze, "test/test_helper.rb".freeze]
14
+ s.date = "2023-05-19"
15
+ s.files = [".gitignore".freeze, ".ruby-version".freeze, ".yardopts".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "Gemfile.lock".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "anchormodel.gemspec".freeze, "bin/rails".freeze, "doc/Anchormodel.html".freeze, "doc/Anchormodel/ActiveModelTypeValue.html".freeze, "doc/Anchormodel/Attribute.html".freeze, "doc/Anchormodel/ModelMixin.html".freeze, "doc/Anchormodel/Version.html".freeze, "doc/_index.html".freeze, "doc/class_list.html".freeze, "doc/css/common.css".freeze, "doc/css/full_list.css".freeze, "doc/css/style.css".freeze, "doc/file.README.html".freeze, "doc/file_list.html".freeze, "doc/frames.html".freeze, "doc/index.html".freeze, "doc/js/app.js".freeze, "doc/js/full_list.js".freeze, "doc/js/jquery.js".freeze, "doc/method_list.html".freeze, "doc/top-level-namespace.html".freeze, "lib/anchormodel.rb".freeze, "lib/anchormodel/active_model_type_value.rb".freeze, "lib/anchormodel/attribute.rb".freeze, "lib/anchormodel/model_mixin.rb".freeze, "lib/anchormodel/version.rb".freeze, "lib/generators/anchormodel/USAGE".freeze, "lib/generators/anchormodel/anchormodel_generator.rb".freeze, "lib/generators/anchormodel/templates/anchormodel.rb.erb".freeze, "logo.svg".freeze, "test/active_record_model/user_test.rb".freeze, "test/dummy/.gitignore".freeze, "test/dummy/Rakefile".freeze, "test/dummy/app/anchormodels/locale.rb".freeze, "test/dummy/app/anchormodels/role.rb".freeze, "test/dummy/app/helpers/application_helper.rb".freeze, "test/dummy/app/models/application_record.rb".freeze, "test/dummy/app/models/concerns/.keep".freeze, "test/dummy/app/models/user.rb".freeze, "test/dummy/bin/rails".freeze, "test/dummy/bin/rake".freeze, "test/dummy/bin/setup".freeze, "test/dummy/config.ru".freeze, "test/dummy/config/application.rb".freeze, "test/dummy/config/boot.rb".freeze, "test/dummy/config/credentials.yml.enc".freeze, "test/dummy/config/database.yml".freeze, "test/dummy/config/environment.rb".freeze, "test/dummy/config/environments/test.rb".freeze, "test/dummy/config/initializers/content_security_policy.rb".freeze, "test/dummy/config/initializers/filter_parameter_logging.rb".freeze, "test/dummy/config/initializers/inflections.rb".freeze, "test/dummy/config/initializers/permissions_policy.rb".freeze, "test/dummy/config/locales/en.yml".freeze, "test/dummy/config/puma.rb".freeze, "test/dummy/config/routes.rb".freeze, "test/dummy/db/migrate/20230107173151_create_users.rb".freeze, "test/dummy/db/schema.rb".freeze, "test/dummy/db/seeds.rb".freeze, "test/dummy/lib/tasks/.keep".freeze, "test/dummy/log/.keep".freeze, "test/dummy/tmp/.keep".freeze, "test/dummy/tmp/pids/.keep".freeze, "test/test_helper.rb".freeze]
16
16
  s.homepage = "https://github.com/kalsan/anchormodel".freeze
17
17
  s.licenses = ["LGPL-3.0-or-later".freeze]
18
18
  s.required_ruby_version = Gem::Requirement.new(">= 3.0.0".freeze)
19
- s.rubygems_version = "3.3.26".freeze
19
+ s.rubygems_version = "3.4.13".freeze
20
20
  s.summary = "Bringing object-oriented programming to Rails enums".freeze
21
21
 
22
- if s.respond_to? :specification_version then
23
- s.specification_version = 4
24
- end
22
+ s.specification_version = 4
25
23
 
26
- if s.respond_to? :add_runtime_dependency then
27
- s.add_runtime_dependency(%q<rails>.freeze, ["~> 7.0"])
28
- s.add_development_dependency(%q<rake>.freeze, ["~> 13.0.6"])
29
- s.add_development_dependency(%q<pry>.freeze, ["~> 0.14.1"])
30
- s.add_development_dependency(%q<rubocop>.freeze, ["~> 1.43.0"])
31
- s.add_development_dependency(%q<rubocop-rails>.freeze, ["~> 2.17.4"])
32
- s.add_development_dependency(%q<yard>.freeze, ["~> 0.9.28"])
33
- s.add_development_dependency(%q<yard-activesupport-concern>.freeze, ["~> 0.0.1"])
34
- s.add_development_dependency(%q<minitest>.freeze, ["~> 5.17.0"])
35
- s.add_development_dependency(%q<minitest-reporters>.freeze, ["~> 1.5.0"])
36
- s.add_development_dependency(%q<sqlite3>.freeze, ["~> 1.6.0"])
37
- else
38
- s.add_dependency(%q<rails>.freeze, ["~> 7.0"])
39
- s.add_dependency(%q<rake>.freeze, ["~> 13.0.6"])
40
- s.add_dependency(%q<pry>.freeze, ["~> 0.14.1"])
41
- s.add_dependency(%q<rubocop>.freeze, ["~> 1.43.0"])
42
- s.add_dependency(%q<rubocop-rails>.freeze, ["~> 2.17.4"])
43
- s.add_dependency(%q<yard>.freeze, ["~> 0.9.28"])
44
- s.add_dependency(%q<yard-activesupport-concern>.freeze, ["~> 0.0.1"])
45
- s.add_dependency(%q<minitest>.freeze, ["~> 5.17.0"])
46
- s.add_dependency(%q<minitest-reporters>.freeze, ["~> 1.5.0"])
47
- s.add_dependency(%q<sqlite3>.freeze, ["~> 1.6.0"])
48
- end
24
+ s.add_runtime_dependency(%q<rails>.freeze, ["~> 7.0"])
25
+ s.add_development_dependency(%q<rake>.freeze, ["~> 13.0.6"])
26
+ s.add_development_dependency(%q<pry>.freeze, ["~> 0.14.1"])
27
+ s.add_development_dependency(%q<rubocop>.freeze, ["~> 1.43.0"])
28
+ s.add_development_dependency(%q<rubocop-rails>.freeze, ["~> 2.17.4"])
29
+ s.add_development_dependency(%q<yard>.freeze, ["~> 0.9.28"])
30
+ s.add_development_dependency(%q<yard-activesupport-concern>.freeze, ["~> 0.0.1"])
31
+ s.add_development_dependency(%q<minitest>.freeze, ["~> 5.17.0"])
32
+ s.add_development_dependency(%q<minitest-reporters>.freeze, ["~> 1.5.0"])
33
+ s.add_development_dependency(%q<sqlite3>.freeze, ["~> 1.6.0"])
49
34
  end
@@ -381,12 +381,12 @@
381
381
  <pre class="lines">
382
382
 
383
383
 
384
- 34
385
- 35
386
- 36</pre>
384
+ 36
385
+ 37
386
+ 38</pre>
387
387
  </td>
388
388
  <td>
389
- <pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line 34</span>
389
+ <pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line 36</span>
390
390
 
391
391
  <span class='kw'>def</span> <span class='id identifier rubyid_changed?'>changed?</span><span class='lparen'>(</span><span class='id identifier rubyid_old_value'>old_value</span><span class='comma'>,</span> <span class='id identifier rubyid_new_value'>new_value</span><span class='comma'>,</span> <span class='id identifier rubyid__new_value_before_type_cast'>_new_value_before_type_cast</span><span class='rparen'>)</span>
392
392
  <span class='kw'>return</span> <span class='id identifier rubyid_deserialize'>deserialize</span><span class='lparen'>(</span><span class='id identifier rubyid_old_value'>old_value</span><span class='rparen'>)</span> <span class='op'>!=</span> <span class='id identifier rubyid_deserialize'>deserialize</span><span class='lparen'>(</span><span class='id identifier rubyid_new_value'>new_value</span><span class='rparen'>)</span>
@@ -420,15 +420,17 @@
420
420
  <pre class="lines">
421
421
 
422
422
 
423
- 29
424
423
  30
425
424
  31
426
- 32</pre>
425
+ 32
426
+ 33
427
+ 34</pre>
427
428
  </td>
428
429
  <td>
429
- <pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line 29</span>
430
+ <pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line 30</span>
430
431
 
431
432
  <span class='kw'>def</span> <span class='id identifier rubyid_deserialize'>deserialize</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
433
+ <span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_presence'>presence</span>
432
434
  <span class='kw'>return</span> <span class='id identifier rubyid_value'>value</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='ivar'>@attribute</span><span class='period'>.</span><span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='rparen'>)</span>
433
435
  <span class='kw'>return</span> <span class='ivar'>@attribute</span><span class='period'>.</span><span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
434
436
  <span class='kw'>end</span></pre>
@@ -477,12 +479,14 @@
477
479
  24
478
480
  25
479
481
  26
480
- 27</pre>
482
+ 27
483
+ 28</pre>
481
484
  </td>
482
485
  <td>
483
486
  <pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line 13</span>
484
487
 
485
488
  <span class='kw'>def</span> <span class='id identifier rubyid_serialize'>serialize</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
489
+ <span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_presence'>presence</span>
486
490
  <span class='kw'>return</span> <span class='kw'>case</span> <span class='id identifier rubyid_value'>value</span>
487
491
  <span class='kw'>when</span> <span class='const'>Symbol</span><span class='comma'>,</span> <span class='const'>String</span>
488
492
  <span class='kw'>unless</span> <span class='ivar'>@attribute</span><span class='period'>.</span><span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='period'>.</span><span class='id identifier rubyid_valid_keys'>valid_keys</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rparen'>)</span>
@@ -507,7 +511,7 @@
507
511
  </div>
508
512
 
509
513
  <div id="footer">
510
- Generated on Tue Jan 24 20:27:59 2023 by
514
+ Generated on Wed Jan 25 12:36:40 2023 by
511
515
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
512
516
  0.9.28 (ruby-3.1.3).
513
517
  </div>
@@ -496,7 +496,7 @@
496
496
  </div>
497
497
 
498
498
  <div id="footer">
499
- Generated on Tue Jan 24 20:27:59 2023 by
499
+ Generated on Wed Jan 25 12:36:40 2023 by
500
500
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
501
501
  0.9.28 (ruby-3.1.3).
502
502
  </div>
@@ -382,7 +382,7 @@
382
382
  <pre class="code"><span class="info file"># File 'lib/anchormodel/model_mixin.rb', line 20</span>
383
383
 
384
384
  <span class='kw'>def</span> <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span><span class='lparen'>(</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='comma'>,</span> <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='label'>optional:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>model_readers:</span> <span class='kw'>true</span><span class='comma'>,</span>
385
- <span class='label'>model_writers:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>model_scopes:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>model_methods:</span> <span class='kw'>nil</span><span class='rparen'>)</span>
385
+ <span class='label'>model_writers:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>model_scopes:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>model_methods:</span> <span class='kw'>nil</span><span class='rparen'>)</span>
386
386
  <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span> <span class='op'>||=</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='period'>.</span><span class='id identifier rubyid_classify'>classify</span><span class='period'>.</span><span class='id identifier rubyid_constantize'>constantize</span>
387
387
  <span class='id identifier rubyid_attribute_name'>attribute_name</span> <span class='op'>=</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
388
388
  <span class='id identifier rubyid_attribute'>attribute</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="../Anchormodel.html" title="Anchormodel (class)">Anchormodel</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Attribute.html" title="Anchormodel::Attribute (class)">Attribute</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'><span class='object_link'><a href="Attribute.html#initialize-instance_method" title="Anchormodel::Attribute#initialize (method)">new</a></span></span><span class='lparen'>(</span><span class='kw'>self</span><span class='comma'>,</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='comma'>,</span> <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='comma'>,</span> <span class='id identifier rubyid_optional'>optional</span><span class='rparen'>)</span>
@@ -440,7 +440,7 @@
440
440
  <span class='id identifier rubyid_fail'>fail</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Anchormodel writer </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'>! already defined for </span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='embexpr_end'>}</span><span class='tstring_content'>, add `model_writers: false` to `belongs_to_anchormodel :</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>`.</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
441
441
  <span class='kw'>end</span>
442
442
  <span class='id identifier rubyid_define_method'>define_method</span><span class='lparen'>(</span><span class='symbol'>:&quot;#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'>!</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span> <span class='kw'>do</span>
443
- <span class='id identifier rubyid_public_send'>public_send</span><span class='lparen'>(</span><span class='symbol'>:&quot;#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='embexpr_end'>}</span><span class='tstring_content'>=</span><span class='tstring_end'>&quot;</span></span><span class='comma'>,</span> <span class='id identifier rubyid_entry'>entry</span><span class='rparen'>)</span>
443
+ <span class='id identifier rubyid_public_send'>public_send</span><span class='lparen'>(</span><span class='symbol'>:&quot;#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>=</span><span class='tstring_end'>&quot;</span></span><span class='comma'>,</span> <span class='id identifier rubyid_entry'>entry</span><span class='rparen'>)</span>
444
444
  <span class='kw'>end</span>
445
445
  <span class='kw'>end</span>
446
446
  <span class='kw'>end</span>
@@ -452,7 +452,7 @@
452
452
  <span class='kw'>if</span> <span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
453
453
  <span class='id identifier rubyid_fail'>fail</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Anchormodel scope </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'> already defined for </span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='embexpr_end'>}</span><span class='tstring_content'>, add `model_scopes: false` to `belongs_to_anchormodel :</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>`.</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
454
454
  <span class='kw'>end</span>
455
- <span class='id identifier rubyid_scope'>scope</span><span class='lparen'>(</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='tlambda'>-&gt;</span><span class='tlambeg'>{</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='id identifier rubyid_attribute_name'>attribute_name</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span><span class='rbrace'>}</span><span class='rparen'>)</span>
455
+ <span class='id identifier rubyid_scope'>scope</span><span class='lparen'>(</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='tlambda'>-&gt;</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='id identifier rubyid_attribute_name'>attribute_name</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span> <span class='rbrace'>}</span><span class='rparen'>)</span>
456
456
  <span class='kw'>end</span>
457
457
  <span class='kw'>end</span>
458
458
  <span class='kw'>end</span></pre>
@@ -466,7 +466,7 @@
466
466
  </div>
467
467
 
468
468
  <div id="footer">
469
- Generated on Tue Jan 24 20:27:59 2023 by
469
+ Generated on Wed Jan 25 12:36:40 2023 by
470
470
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
471
471
  0.9.28 (ruby-3.1.3).
472
472
  </div>
@@ -141,7 +141,7 @@
141
141
 
142
142
  </div>
143
143
  </dt>
144
- <dd><pre class="code"><span class='int'>1</span></pre></dd>
144
+ <dd><pre class="code"><span class='int'>2</span></pre></dd>
145
145
 
146
146
  <dt id="EDGE-constant" class="">EDGE =
147
147
  <div class="docstring">
@@ -185,7 +185,7 @@
185
185
  </div>
186
186
 
187
187
  <div id="footer">
188
- Generated on Tue Jan 24 20:27:59 2023 by
188
+ Generated on Wed Jan 25 12:36:39 2023 by
189
189
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
190
190
  0.9.28 (ruby-3.1.3).
191
191
  </div>
data/doc/Anchormodel.html CHANGED
@@ -934,7 +934,7 @@
934
934
  </div>
935
935
 
936
936
  <div id="footer">
937
- Generated on Tue Jan 24 20:27:59 2023 by
937
+ Generated on Wed Jan 25 12:36:40 2023 by
938
938
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
939
939
  0.9.28 (ruby-3.1.3).
940
940
  </div>
data/doc/_index.html CHANGED
@@ -141,7 +141,7 @@
141
141
  </div>
142
142
 
143
143
  <div id="footer">
144
- Generated on Tue Jan 24 20:27:58 2023 by
144
+ Generated on Wed Jan 25 12:36:39 2023 by
145
145
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
146
146
  0.9.28 (ruby-3.1.3).
147
147
  </div>
data/doc/file.README.html CHANGED
@@ -228,7 +228,7 @@
228
228
  </div></div>
229
229
 
230
230
  <div id="footer">
231
- Generated on Tue Jan 24 20:27:59 2023 by
231
+ Generated on Wed Jan 25 12:36:39 2023 by
232
232
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
233
233
  0.9.28 (ruby-3.1.3).
234
234
  </div>
data/doc/index.html CHANGED
@@ -228,7 +228,7 @@
228
228
  </div></div>
229
229
 
230
230
  <div id="footer">
231
- Generated on Tue Jan 24 20:27:58 2023 by
231
+ Generated on Wed Jan 25 12:36:39 2023 by
232
232
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
233
233
  0.9.28 (ruby-3.1.3).
234
234
  </div>
@@ -100,7 +100,7 @@
100
100
  </div>
101
101
 
102
102
  <div id="footer">
103
- Generated on Tue Jan 24 20:27:59 2023 by
103
+ Generated on Wed Jan 25 12:36:39 2023 by
104
104
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
105
105
  0.9.28 (ruby-3.1.3).
106
106
  </div>
@@ -11,6 +11,7 @@ class Anchormodel::ActiveModelTypeValue < ActiveModel::Type::Value
11
11
 
12
12
  # Implementing this instead of cast to force key validation in any case
13
13
  def serialize(value)
14
+ value = value.presence
14
15
  return case value
15
16
  when Symbol, String
16
17
  unless @attribute.anchormodel_class.valid_keys.include?(value.to_sym)
@@ -27,6 +28,7 @@ class Anchormodel::ActiveModelTypeValue < ActiveModel::Type::Value
27
28
  end
28
29
 
29
30
  def deserialize(value)
31
+ value = value.presence
30
32
  return value if value.is_a?(@attribute.anchormodel_class)
31
33
  return @attribute.anchormodel_class.find(value)
32
34
  end
@@ -18,7 +18,7 @@ module Anchormodel::ModelMixin
18
18
  # @param model_scopes [Boolean] If true, the model is given an ActiveRecord::Enum style scope `MyModel.mykey` for each key in the anchormodel
19
19
  # @param model_methods [Boolean, NilClass] If non-nil, this mass-assigns and overrides `model_readers`, `model_writers` and `model_scopes`
20
20
  def belongs_to_anchormodel(attribute_name, anchormodel_class = nil, optional: false, model_readers: true,
21
- model_writers: true, model_scopes: true, model_methods: nil)
21
+ model_writers: true, model_scopes: true, model_methods: nil)
22
22
  anchormodel_class ||= attribute_name.to_s.classify.constantize
23
23
  attribute_name = attribute_name.to_sym
24
24
  attribute = Anchormodel::Attribute.new(self, attribute_name, anchormodel_class, optional)
@@ -76,7 +76,7 @@ module Anchormodel::ModelMixin
76
76
  fail("Anchormodel writer #{entry.key}! already defined for #{self}, add `model_writers: false` to `belongs_to_anchormodel :#{attribute_name}`.")
77
77
  end
78
78
  define_method(:"#{entry.key}!") do
79
- public_send(:"#{attribute_name.to_s}=", entry)
79
+ public_send(:"#{attribute_name}=", entry)
80
80
  end
81
81
  end
82
82
  end
@@ -88,7 +88,7 @@ module Anchormodel::ModelMixin
88
88
  if respond_to?(entry.key)
89
89
  fail("Anchormodel scope #{entry.key} already defined for #{self}, add `model_scopes: false` to `belongs_to_anchormodel :#{attribute_name}`.")
90
90
  end
91
- scope(entry.key, ->{where(attribute_name => entry.key)})
91
+ scope(entry.key, -> { where(attribute_name => entry.key) })
92
92
  end
93
93
  end
94
94
  end
@@ -2,7 +2,7 @@ class Anchormodel
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 1
5
+ PATCH = 3
6
6
 
7
7
  EDGE = false
8
8
 
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generate a single Anchormodel
3
+
4
+ Example:
5
+ bin/rails generate anchormodel Color
6
+
7
+ This will create:
8
+ app/anchormodels/color.rb
@@ -0,0 +1,11 @@
1
+ class AnchormodelGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ def add_anchormodel
5
+ fail('NAME must be present.') if name.blank?
6
+ @klass = @name.camelize
7
+ @filename = @name.underscore
8
+
9
+ template 'anchormodel.rb.erb', "app/anchormodels/#{@filename}.rb"
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ class <%= @klass %> < Anchormodel
2
+ new :foo
3
+ end
@@ -53,7 +53,7 @@ class UserTest < Minitest::Test
53
53
  def test_custom_comparison
54
54
  assert_equal(-1, Role.find(:moderator) <=> Role.find(:admin))
55
55
  assert_equal(1, Role.find(:moderator) <=> Role.find(:guest))
56
- assert_equal(0, Role.find(:moderator) <=> Role.find(:moderator))
56
+ assert_equal(0, Role.find(:moderator) <=> Role.find('moderator'))
57
57
  assert Role.find(:moderator) < Role.find(:admin)
58
58
  end
59
59
 
@@ -64,9 +64,9 @@ class UserTest < Minitest::Test
64
64
 
65
65
  def test_alternative_column_name
66
66
  ben = User.create!(
67
- role: Role.find(:moderator),
67
+ role: Role.find(:moderator),
68
68
  secondary_role: Role.find(:admin),
69
- locale: Locale.find(:de)
69
+ locale: Locale.find(:de)
70
70
  )
71
71
  assert_equal(Role.find(:moderator), ben.role)
72
72
  assert_equal(Role.find(:admin), ben.secondary_role)
@@ -112,4 +112,10 @@ class UserTest < Minitest::Test
112
112
  assert_equal 1, User.fr.count
113
113
  assert_equal 0, User.en.count
114
114
  end
115
+
116
+ def test_rails_blank_assignment
117
+ u = User.new(role: :admin, secondary_role: :admin, locale: :en, preferred_locale: :en)
118
+ u.secondary_role = ''
119
+ assert_nil u.secondary_role
120
+ end
115
121
  end
@@ -10,14 +10,13 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema[7.0].define(version: 2023_01_24_084241) do
14
- create_table "users", force: :cascade do |t|
15
- t.string "role"
16
- t.string "locale"
17
- t.datetime "created_at", null: false
18
- t.datetime "updated_at", null: false
19
- t.string "secondary_role"
20
- t.string "preferred_locale", default: "en", null: false
13
+ ActiveRecord::Schema[7.0].define(version: 20_230_124_084_241) do
14
+ create_table 'users', force: :cascade do |t|
15
+ t.string 'role'
16
+ t.string 'locale'
17
+ t.datetime 'created_at', null: false
18
+ t.datetime 'updated_at', null: false
19
+ t.string 'secondary_role'
20
+ t.string 'preferred_locale', default: 'en', null: false
21
21
  end
22
-
23
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anchormodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Kalbermatter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-24 00:00:00.000000000 Z
11
+ date: 2023-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -191,6 +191,9 @@ files:
191
191
  - lib/anchormodel/attribute.rb
192
192
  - lib/anchormodel/model_mixin.rb
193
193
  - lib/anchormodel/version.rb
194
+ - lib/generators/anchormodel/USAGE
195
+ - lib/generators/anchormodel/anchormodel_generator.rb
196
+ - lib/generators/anchormodel/templates/anchormodel.rb.erb
194
197
  - logo.svg
195
198
  - test/active_record_model/user_test.rb
196
199
  - test/dummy/.gitignore
@@ -245,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
245
248
  - !ruby/object:Gem::Version
246
249
  version: '0'
247
250
  requirements: []
248
- rubygems_version: 3.3.26
251
+ rubygems_version: 3.4.13
249
252
  signing_key:
250
253
  specification_version: 4
251
254
  summary: Bringing object-oriented programming to Rails enums