activemodel-with_conditions 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b87e3feb3afd222ced22ffd2c5ffb9dc027bba52afe73404e2517fb7f037378f
4
+ data.tar.gz: d934a506e31218f30a2b702bc729ee81ffeb78130640360d6856136df6148fc3
5
+ SHA512:
6
+ metadata.gz: 637afd30d18d47428c7c29023ceea2d50f1102f2ff6c6a01243012f0122f81c7aa4a2c914f7f59d321b0e3bba4ca1125115bb3b5e2203d789b05d487d5879b79
7
+ data.tar.gz: 15a68276fd2e44b78a5a68495c77c028b220bdde08e29ba8635ededbba4e34d9398944105c38830ea7837ca6921f652ba656e8dea7d21063dafcba64e3cb4fe0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [1.0.0] - 2024-12-22
2
+
3
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rake"
8
+
9
+ group :test do
10
+ gem "activemodel"
11
+ gem "activesupport"
12
+ gem "rspec"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activemodel-with_conditions (1.0.0)
5
+ activemodel
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (8.0.1)
11
+ activesupport (= 8.0.1)
12
+ activesupport (8.0.1)
13
+ base64
14
+ benchmark (>= 0.3)
15
+ bigdecimal
16
+ concurrent-ruby (~> 1.0, >= 1.3.1)
17
+ connection_pool (>= 2.2.5)
18
+ drb
19
+ i18n (>= 1.6, < 2)
20
+ logger (>= 1.4.2)
21
+ minitest (>= 5.1)
22
+ securerandom (>= 0.3)
23
+ tzinfo (~> 2.0, >= 2.0.5)
24
+ uri (>= 0.13.1)
25
+ base64 (0.2.0)
26
+ benchmark (0.4.0)
27
+ bigdecimal (3.1.8)
28
+ concurrent-ruby (1.3.4)
29
+ connection_pool (2.4.1)
30
+ diff-lcs (1.5.1)
31
+ drb (2.2.1)
32
+ i18n (1.14.6)
33
+ concurrent-ruby (~> 1.0)
34
+ logger (1.6.4)
35
+ minitest (5.25.4)
36
+ rake (13.2.1)
37
+ rspec (3.13.0)
38
+ rspec-core (~> 3.13.0)
39
+ rspec-expectations (~> 3.13.0)
40
+ rspec-mocks (~> 3.13.0)
41
+ rspec-core (3.13.2)
42
+ rspec-support (~> 3.13.0)
43
+ rspec-expectations (3.13.3)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.13.0)
46
+ rspec-mocks (3.13.2)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.13.0)
49
+ rspec-support (3.13.2)
50
+ securerandom (0.4.1)
51
+ tzinfo (2.0.6)
52
+ concurrent-ruby (~> 1.0)
53
+ uri (1.0.2)
54
+
55
+ PLATFORMS
56
+ arm64-darwin-22
57
+
58
+ DEPENDENCIES
59
+ activemodel
60
+ activemodel-with_conditions!
61
+ activesupport
62
+ rake
63
+ rspec
64
+
65
+ BUNDLED WITH
66
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Henrik Nyh
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,59 @@
1
+ # ActiveModel::WithConditions
2
+
3
+ Adds `with_conditions` to Rails apps, or other projects using Active Model or Active Record.
4
+
5
+ `with_conditions` is like Active Support's [`with_options`](https://guides.rubyonrails.org/active_support_core_extensions.html#with-options), but only supports `:if` and `:unless`, and it merges these.
6
+
7
+ So you can do:
8
+
9
+ ``` ruby
10
+ with_conditions(if: :feature_x_is_on?) do
11
+ validate :free_plans_must_have_x, if: :free_plan?
12
+ end
13
+ ```
14
+
15
+ It supports all forms of `:if` and `:unless` – symbols, lambdas (with or without a block argument), and arrays of the same.
16
+
17
+ ## Compared to `with_options`
18
+
19
+ This improves on Active Support's `with_options`, where the inner `:if` would overwrite the outer one, and you'd need to work around it with something like:
20
+
21
+ ``` ruby
22
+ with_options(unless: -> { !feature_x_is_on? }) do
23
+ validate :free_plans_must_have_x, if: :free_plan?
24
+ end
25
+ ```
26
+
27
+ You can still use `with_options` for other things (though this gem's author ever only uses it for validations and callback conditions). You could even combine them:
28
+
29
+ ``` ruby
30
+ with_conditions(if: :feature_x_is_on?) do
31
+ with_options(presence: true) do
32
+ validates :x
33
+ end
34
+ end
35
+ ```
36
+
37
+ ## Installation
38
+
39
+ Install the gem and add to the application's Gemfile by executing:
40
+
41
+ $ bundle add activemodel-with_conditions
42
+
43
+ If bundler is not being used to manage dependencies, install the gem by executing:
44
+
45
+ $ gem install activemodel-with_conditions
46
+
47
+ ## Development
48
+
49
+ 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.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome at <https://github.com/henrik/activemodel-with_conditions>.
56
+
57
+ ## License
58
+
59
+ 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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1 @@
1
+ require_relative "with_conditions"
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WithConditions
4
+ class OptionMerger < BasicObject
5
+ def initialize(context, if:, unless:)
6
+ @context = context
7
+ @if_conds = Array(::Kernel.binding.local_variable_get(:if))
8
+ @unless_conds = Array(::Kernel.binding.local_variable_get(:unless))
9
+ end
10
+
11
+ private
12
+
13
+ def method_missing(method, *, **kwargs, &)
14
+ kwargs[:if] = @if_conds + Array(kwargs[:if])
15
+ kwargs[:unless] = @unless_conds + Array(kwargs[:unless])
16
+ @context.__send__(method, *, **kwargs, &)
17
+ end
18
+
19
+ def respond_to_missing?(...) = @context.respond_to?(...)
20
+ def Array(...) = ::Kernel.Array(...)
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WithConditions
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+ require_relative "with_conditions/options_merger"
5
+ require_relative "with_conditions/version"
6
+
7
+ module WithConditions
8
+ def with_conditions(if: nil, unless: nil, &block)
9
+ option_merger = WithConditions::OptionMerger.new(self, if:, unless:)
10
+
11
+ if block
12
+ block.arity.zero? ? option_merger.instance_eval(&block) : block.call(option_merger)
13
+ else
14
+ option_merger
15
+ end
16
+ end
17
+ end
18
+
19
+ module ActiveModel
20
+ module API
21
+ singleton_class.prepend(Module.new do
22
+ def included(klass)
23
+ super
24
+ klass.extend WithConditions
25
+ end
26
+ end)
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-with_conditions
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Henrik Nyh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Like with_options but merges :if and :unless conditions. Convenient for
28
+ Active Model or Active Record validations and callbacks.
29
+ email:
30
+ - henrik@nyh.se
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - CHANGELOG.md
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/activemodel-with_conditions.rb
43
+ - lib/with_conditions.rb
44
+ - lib/with_conditions/options_merger.rb
45
+ - lib/with_conditions/version.rb
46
+ homepage: https://github.com/henrik/activemodel-with_conditions
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/henrik/activemodel-with_conditions
51
+ source_code_uri: https://github.com/henrik/activemodel-with_conditions
52
+ changelog_uri: https://github.com/henrik/activemodel-with_conditions/blob/main/CHANGELOG.md
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.4.6
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Like with_options but merges :if and :unless conditions.
72
+ test_files: []