delegate_if_nil 0.2.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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +73 -0
- data/Rakefile +29 -0
- data/lib/delegate_if_nil.rb +34 -0
- data/lib/delegate_if_nil/railtie.rb +6 -0
- data/lib/delegate_if_nil/version.rb +5 -0
- data/lib/tasks/delegate_if_nil_tasks.rake +6 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 118a0a92d00e61a386046d6dad0be907cb13be59926b4d52947986f646db59e6
|
4
|
+
data.tar.gz: 7d7079f3786f35f538aa2ed9636ed9dd2942162d823ce017116fcb41133c5c58
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b5af1ff90239141a6618bd541718c4834340eb9414dd71d6ae50948251eced69e78ee2da93cf5208d9a19f448e62b76de3b52683e4ee56a35a663148921b611
|
7
|
+
data.tar.gz: 01ebe27e99234b2a26af27169639b6d62d43506f0d7ffafa8427ff2f66820efb260210eaff7849228174d304b646c82bda8e23d6121736b1e5529807b9fe1832
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 Steven Allen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# DelegateIfNil
|
2
|
+
|
3
|
+
A simple gem to add nil delegation to an associated model if an attribute is `nil`.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Add this to your model:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class SomeModel < ApplicationRecord
|
11
|
+
extend DelegateIfNil
|
12
|
+
belongs_to :owning_model
|
13
|
+
|
14
|
+
nil_delegate :animal, to: :owning_model
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
`animal` will now delegate to `owning_model` if it's `nil` on an instance of `SomeModel`.
|
19
|
+
|
20
|
+
You also get `_source` methods, which will tell you where the `animal` value comes from. If it's not set on either one, it will return `"unset"`
|
21
|
+
|
22
|
+
You'll get the following results:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
om = OwningModel.create(animal: "Cat")
|
26
|
+
|
27
|
+
some_model = SomeModel.create(animal: nil, owning_model = om)
|
28
|
+
some_model.animal # "Cat"
|
29
|
+
some_model.animal_source # "owning_model"
|
30
|
+
|
31
|
+
some_model = SomeModel.create(animal: "Dog", owning_model = om)
|
32
|
+
some_model.animal # Dog
|
33
|
+
some_model.animal_source # "self"
|
34
|
+
|
35
|
+
om = OwningModel.create(animal: nil)
|
36
|
+
some_model = SomeModel.create(animal: nil, owning_model = om)
|
37
|
+
some_model.animal # nil
|
38
|
+
some_model.animal_source # "unset"
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
It also works for multiple attributes. EG:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class SomeModel < ApplicationRecord
|
46
|
+
extend DelegateIfNil
|
47
|
+
belongs_to :owning_model
|
48
|
+
|
49
|
+
nil_delegate :animal, :phone_number, :name, to: :owning_model
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
It also resolves correctly for recursive delegations. IE: if `owning_model` delgates an attribute if `nil`, it will correctly report the source all the way down the chain, terminating either in `self`, `unset`, or the association name.
|
54
|
+
|
55
|
+
## Installation
|
56
|
+
Add this line to your application's Gemfile:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
gem 'delegate_if_nil', git: 'https://github.com/stevenallen05/delegate_if_nil.git'
|
60
|
+
```
|
61
|
+
|
62
|
+
And then execute:
|
63
|
+
```bash
|
64
|
+
$ bundle
|
65
|
+
```
|
66
|
+
|
67
|
+
Or install it yourself as:
|
68
|
+
```bash
|
69
|
+
$ gem install delegate_if_nil
|
70
|
+
```
|
71
|
+
|
72
|
+
## License
|
73
|
+
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,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "bundler/setup"
|
5
|
+
rescue LoadError
|
6
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
7
|
+
end
|
8
|
+
|
9
|
+
require "rdoc/task"
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = "rdoc"
|
13
|
+
rdoc.title = "DelegateIfNil"
|
14
|
+
rdoc.options << "--line-numbers"
|
15
|
+
rdoc.rdoc_files.include("README.md")
|
16
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
17
|
+
end
|
18
|
+
|
19
|
+
require "bundler/gem_tasks"
|
20
|
+
|
21
|
+
require "rake/testtask"
|
22
|
+
|
23
|
+
Rake::TestTask.new(:test) do |t|
|
24
|
+
t.libs << "test"
|
25
|
+
t.pattern = "test/**/*_test.rb"
|
26
|
+
t.verbose = false
|
27
|
+
end
|
28
|
+
|
29
|
+
task default: :test
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate_if_nil/railtie"
|
4
|
+
|
5
|
+
module DelegateIfNil
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def nil_delegate(*attrs, to:)
|
9
|
+
attrs.each do |attr|
|
10
|
+
define_method attr do
|
11
|
+
self[attr].nil? ? send(to)&.send(attr) : self[attr]
|
12
|
+
end
|
13
|
+
|
14
|
+
source_method = "#{attr}_source".to_sym
|
15
|
+
define_method source_method do
|
16
|
+
return "self" unless self[attr].nil?
|
17
|
+
|
18
|
+
if send(to)&.respond_to?(source_method)
|
19
|
+
to_source = send(to).send(source_method)
|
20
|
+
return to.to_s if to_source == "self"
|
21
|
+
|
22
|
+
return to_source
|
23
|
+
else
|
24
|
+
return send(to)&.send(attr).nil? ? "unset" : to.to_s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
delegted_bool_method = "#{attr}_delegated?".to_sym
|
29
|
+
define_method delegted_bool_method do
|
30
|
+
self[attr].nil?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delegate_if_nil
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steven Allen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
description: Add to a model to simply delegate attributes to an associated record
|
28
|
+
email:
|
29
|
+
- sallen@amberstyle.ca
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/delegate_if_nil.rb
|
38
|
+
- lib/delegate_if_nil/railtie.rb
|
39
|
+
- lib/delegate_if_nil/version.rb
|
40
|
+
- lib/tasks/delegate_if_nil_tasks.rake
|
41
|
+
homepage: https://github.com/stevenallen05/delegate_if_nil
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubygems_version: 3.0.1
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Simple delegation of nil attributes to an association
|
64
|
+
test_files: []
|