sti_fallback 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2b536e45474f6f0eb9c8af256cbda3168c40cab260bc6be2b65ccba67535cacd
4
+ data.tar.gz: d64ac7a8c40988d07dea79747eaae1d307fe1404e02814551292da53d3e23025
5
+ SHA512:
6
+ metadata.gz: ebf56536b24ddf649c51f98e60571dce825089dae348b7748ed40386ef52e5e2b7c2d5b20e9d539826441d95f0b9f12198129d4ede9264eab3ebd3e046585f7c
7
+ data.tar.gz: aae5099701a63a86c10ebf6103c44f6c5bba0917c975d2deab7a8df5dfbf4d7fe54b8dd5edc0e9c56004b94682ea375d59363bfe2b0680062b48eebdb566ef94
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Ostap Brehin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # StiFallback
2
+
3
+ Handle validation or additonal behavior on missing subclasses in STI by falling back to the base class.
4
+
5
+ Example:
6
+
7
+ ```ruby
8
+ class Action < ApplicationRecord
9
+ include StiFallback
10
+
11
+ validates :type, inclusion: { in: %w[Value1 Value2 Value3] }
12
+
13
+ # optional: specify which types should still raise an error when not found
14
+ sti_fallback raise_error_for: %w[Value1 Value2]
15
+ end
16
+ ```
17
+
18
+ ## Installation
19
+
20
+ Install the gem and add to the application's Gemfile by executing:
21
+
22
+ $ bundle add sti_fallback
23
+
24
+ If bundler is not being used to manage dependencies, install the gem by executing:
25
+
26
+ $ gem install sti_fallback
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/osbre/sti_fallback.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StiFallback
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "sti_fallback/version"
4
+
5
+ module StiFallback
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class_attribute :_raise_error_for_types, instance_writer: false
10
+ self._raise_error_for_types = []
11
+ end
12
+
13
+ class_methods do
14
+ # Method to specify which types should raise an error when not found
15
+ def sti_fallback(raise_error_for: [])
16
+ self._raise_error_for_types = raise_error_for.map(&:to_s)
17
+ end
18
+
19
+ def sti_class_for(type_name)
20
+ type_name.constantize
21
+ rescue NameError
22
+ # Raises error if type is in the list for which errors should be raised
23
+ raise if _raise_error_for_types.include?(type_name)
24
+ # Falls back to the base class if the type is not in the error list
25
+ self
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module StiFallback
2
+ VERSION: String
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ # Specifies which types should raise an error when not found
8
+ def sti_fallback: (raise_error_for: Array[String]?) -> void
9
+
10
+ # Returns the class for a given type name, falling back to the base class or raising an error as configured
11
+ def sti_class_for: (String) -> untyped
12
+ end
13
+
14
+ # This attribute holds the types that should raise an error
15
+ _raise_error_for_types: Array[String]
16
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/sti_fallback/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sti_fallback"
7
+ spec.version = StiFallback::VERSION
8
+ spec.authors = ["Ostap Brehin"]
9
+ spec.email = ["osbre@protonmail.com"]
10
+
11
+ spec.summary = "Fall back to base model on missing STI subclasses in ActiveRecord"
12
+ spec.homepage = "https://github.com/osbre/sti_fallback"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/osbre/sti_fallback.git"
20
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) ||
27
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "activerecord", "~> 6.0"
35
+ spec.add_development_dependency "rake", "~> 13.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sti_fallback
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ostap Brehin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ description:
42
+ email:
43
+ - osbre@protonmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE.txt
49
+ - README.md
50
+ - Rakefile
51
+ - lib/sti_fallback.rb
52
+ - lib/sti_fallback/version.rb
53
+ - sig/sti_fallback.rbs
54
+ - sti_fallback.gemspec
55
+ homepage: https://github.com/osbre/sti_fallback
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ allowed_push_host: https://rubygems.org
60
+ homepage_uri: https://github.com/osbre/sti_fallback
61
+ source_code_uri: https://github.com/osbre/sti_fallback.git
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.6.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.5.3
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Fall back to base model on missing STI subclasses in ActiveRecord
81
+ test_files: []