delegate_when_nil 0.0.1
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.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +1 -0
- data/delegate_when_nil.gemspec +25 -0
- data/lib/delegate_when_nil.rb +28 -0
- data/lib/delegate_when_nil/version.rb +3 -0
- data/spec/delegate_when_nil_spec.rb +59 -0
- data/spec/spec_helper.rb +19 -0
- metadata +93 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 conanite
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# DelegateWhenNil
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'delegate_when_nil'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install delegate_when_nil
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
First of all require the gem
|
22
|
+
|
23
|
+
require 'delegate_when_nil'
|
24
|
+
|
25
|
+
Mix in the DelegateWhenNil module
|
26
|
+
|
27
|
+
class Widget
|
28
|
+
extend DelegateWhenNil
|
29
|
+
...
|
30
|
+
|
31
|
+
To use with ActiveRecord, put this in an initializer instead
|
32
|
+
|
33
|
+
ActiveRecord::Base.send :extend, DelegateNil
|
34
|
+
|
35
|
+
Then #delegate_when_nil is available as a class method on your model,
|
36
|
+
|
37
|
+
class Widget < PopularFramework::Base
|
38
|
+
attr_accessor :parent
|
39
|
+
attr_accessor :description, :colour, :height
|
40
|
+
delegate_when_nil :description, :colour, :height, to: :parent
|
41
|
+
...
|
42
|
+
|
43
|
+
In this example, if the #description, #colour, or #height properties are accessed and are nil, DelegateWhenNil will ask the #parent property instead.
|
44
|
+
|
45
|
+
p = Parent.new :description => "Woggle"
|
46
|
+
w = Widget.new :parent => p, :colour => :blue
|
47
|
+
|
48
|
+
w.description #=> "Woggle"
|
49
|
+
w.colour #=> :blue
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- coding: utf-8; mode: ruby -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'delegate_when_nil/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |gem|
|
9
|
+
gem.name = "delegate_when_nil"
|
10
|
+
gem.version = DelegateWhenNil::VERSION
|
11
|
+
gem.authors = ["Conan Dalton"]
|
12
|
+
gem.license = "MIT"
|
13
|
+
gem.email = ["conan@conandalton.net"]
|
14
|
+
gem.description = %q{Like #delegate, but assumes the target method already exists, and delegates only when the local method returns nil}
|
15
|
+
gem.summary = %q{Like #delegate, but assumes the target method already exists, and delegates only when the local method returns nil}
|
16
|
+
gem.homepage = "https://github.com/conanite/delegate_when_nil"
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rspec', '~> 2.9'
|
19
|
+
gem.add_development_dependency 'rspec_numbering_formatter'
|
20
|
+
|
21
|
+
gem.files = `git ls-files`.split($/)
|
22
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
24
|
+
gem.require_paths = ["lib"]
|
25
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "delegate_when_nil/version"
|
2
|
+
|
3
|
+
module DelegateWhenNil
|
4
|
+
def define_conditional_delegation name, target
|
5
|
+
old_name = "delegate_when_nil_#{name}".to_sym
|
6
|
+
return if method_defined? old_name
|
7
|
+
|
8
|
+
class_eval <<DELEGATION
|
9
|
+
alias #{old_name} #{name} # alias delegate_when_nil_description description
|
10
|
+
def #{name} *args # def description *args
|
11
|
+
result = #{old_name}(*args) # result = delegate_when_nil_description(*args)
|
12
|
+
if result == nil # if result == nil
|
13
|
+
delegate = #{target} # delegate = parent
|
14
|
+
result = delegate.#{name}(*args) if delegate # result = delegate.description(*args) if delegate
|
15
|
+
end # end
|
16
|
+
result # result
|
17
|
+
end # end
|
18
|
+
DELEGATION
|
19
|
+
end
|
20
|
+
|
21
|
+
def delegate_when_nil *names
|
22
|
+
target = names.pop[:to]
|
23
|
+
raise "missing :to option to #delegate_when_nil for #{name}" if target.to_s == ""
|
24
|
+
names.each do |name|
|
25
|
+
define_conditional_delegation name, target
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DelegateWhenNil do
|
4
|
+
class Parent
|
5
|
+
attr_accessor :last_name
|
6
|
+
end
|
7
|
+
|
8
|
+
class Child
|
9
|
+
extend DelegateWhenNil
|
10
|
+
attr_accessor :last_name, :parent, :size
|
11
|
+
delegate_when_nil :last_name, :to => :parent
|
12
|
+
delegate_when_nil :size, :to => :"parent.last_name"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should not intervene when the property is present" do
|
16
|
+
child = Child.new
|
17
|
+
child.last_name = "Wong"
|
18
|
+
expect(child.last_name).to eq "Wong"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should not get upset when everything is nil" do
|
22
|
+
child = Child.new
|
23
|
+
expect(child.last_name).to eq nil
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not intervene when the property is present and the delegate target is also present" do
|
27
|
+
child = Child.new
|
28
|
+
child.last_name = "Shaw"
|
29
|
+
child.parent = Parent.new
|
30
|
+
expect(child.last_name).to eq "Shaw"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should not intervene when the property is present and the delegate target is also present" do
|
34
|
+
child = Child.new
|
35
|
+
child.parent = Parent.new
|
36
|
+
child.parent.last_name = "Wonka"
|
37
|
+
expect(child.last_name).to eq "Wonka"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should raise an error if a complex delegation target fails" do
|
41
|
+
child = Child.new
|
42
|
+
expect { child.size }.to raise_error
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should execute a complex delegation target as expected" do
|
46
|
+
child = Child.new
|
47
|
+
child.parent = Parent.new
|
48
|
+
child.parent.last_name = "1234567"
|
49
|
+
expect(child.size).to eq 7
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should ignore a complex delegation target as expected when the local property is not nil" do
|
53
|
+
child = Child.new
|
54
|
+
child.size = 21
|
55
|
+
child.parent = Parent.new
|
56
|
+
child.parent.last_name = "1234567"
|
57
|
+
expect(child.size).to eq 21
|
58
|
+
end
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'delegate_when_nil'
|
2
|
+
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
6
|
+
# loaded once.
|
7
|
+
#
|
8
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
|
14
|
+
# Run specs in random order to surface order dependencies. If you find an
|
15
|
+
# order dependency and want to debug it, you can fix the order by providing
|
16
|
+
# the seed, which is printed after each run.
|
17
|
+
# --seed 1234
|
18
|
+
config.order = 'random'
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delegate_when_nil
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Conan Dalton
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.9'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.9'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec_numbering_formatter
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: ! 'Like #delegate, but assumes the target method already exists, and
|
47
|
+
delegates only when the local method returns nil'
|
48
|
+
email:
|
49
|
+
- conan@conandalton.net
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rspec
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- delegate_when_nil.gemspec
|
61
|
+
- lib/delegate_when_nil.rb
|
62
|
+
- lib/delegate_when_nil/version.rb
|
63
|
+
- spec/delegate_when_nil_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
homepage: https://github.com/conanite/delegate_when_nil
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.24
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: ! 'Like #delegate, but assumes the target method already exists, and delegates
|
90
|
+
only when the local method returns nil'
|
91
|
+
test_files:
|
92
|
+
- spec/delegate_when_nil_spec.rb
|
93
|
+
- spec/spec_helper.rb
|