conditional_delegate 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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@conditional_delegate --create
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-19mode # JRuby in 1.9 mode
5
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in conditional_delegate.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Roel van Dijk
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.
@@ -0,0 +1,61 @@
1
+ # ConditionalDelegate
2
+
3
+ **ConditionalDelegate** allows delegates to be conditional.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'conditional_delegate'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install conditional_delegate
18
+
19
+ ## Usage
20
+
21
+ Here is a short example with a parent and child class. Calls
22
+ to the child object are delegated to its parent if the
23
+ condition for that delegation is met.
24
+
25
+ Simple parent class with four accessors:
26
+
27
+ ```ruby
28
+ class Parent
29
+ attr_accessor :foo, :bar, :baz, :qux
30
+ end
31
+ ```
32
+
33
+ Child class with the same accessors as the parent, and some
34
+ conditional delegates defined:
35
+
36
+ ```ruby
37
+ class Child
38
+ extend ConditionalDelegate
39
+
40
+ attr_accessor :parent, :foo, :bar, :baz, :qux
41
+
42
+ conditional_delegate :foo, to: :parent, if: :empty?
43
+ conditional_delegate :bar, to: :parent, if: lambda { |value| value == "something" }
44
+
45
+ conditional_delegate :baz, :qux, to: :parent, if: :empty?
46
+ end
47
+ ```
48
+
49
+ The field `foo` is delegated to the parent if the value is `empty?`.
50
+
51
+ The field `bar` is delegated to the parent if the given lambda returns true for the child's `foo` value.
52
+
53
+ Both the fields `baz` and `qux` are delegated to the parent if the field's values are `empty?`.
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new
10
+
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/conditional_delegate/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Roel van Dijk"]
6
+ gem.email = ["roel@rustradio.org"]
7
+ gem.description = %q{ConditionalDelegate allows delegates to be conditional.}
8
+ gem.summary = %q{ConditionalDelegate allows delegates to be conditional.}
9
+ gem.homepage = "http://github.com/rdvdijk/conditional_delegate"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "conditional_delegate"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ConditionalDelegate::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "rspec"
20
+ end
@@ -0,0 +1,33 @@
1
+ require "conditional_delegate/version"
2
+
3
+ module ConditionalDelegate
4
+
5
+ def conditional_delegate(*attributes, options)
6
+ attributes.each do |attribute|
7
+ module_eval do
8
+ define_delegate_method(attribute, options)
9
+ end
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def define_delegate_method(attribute, options)
16
+ own_method = :"_#{attribute}"
17
+ alias_method own_method, :"#{attribute}"
18
+
19
+ define_method :"#{attribute}" do
20
+ value = send own_method
21
+ condition = options[:if]
22
+
23
+ if condition.is_a? Proc and condition.call(value)
24
+ send(options[:to]).send(attribute)
25
+ elsif condition.is_a? Symbol and value.send(condition)
26
+ send(options[:to]).send(attribute)
27
+ else
28
+ value
29
+ end
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,3 @@
1
+ module ConditionalDelegate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ class Parent
4
+ attr_accessor :foo, :bar, :baz, :qux
5
+ end
6
+
7
+ class Child
8
+ extend ConditionalDelegate
9
+
10
+ attr_accessor :parent, :foo, :bar, :baz, :qux
11
+
12
+ conditional_delegate :foo, to: :parent, if: :empty?
13
+ conditional_delegate :bar, to: :parent, if: lambda { |value| value == "something" }
14
+
15
+ conditional_delegate :baz, :qux, to: :parent, if: :empty?
16
+ end
17
+
18
+ describe ConditionalDelegate do
19
+
20
+ context "delegation" do
21
+
22
+ it "should delegate if condition is met" do
23
+ parent = Parent.new
24
+ parent.foo = "parent foo"
25
+
26
+ child = Child.new
27
+ child.parent = parent
28
+ child.foo = ""
29
+
30
+ child.foo.should == "parent foo"
31
+ end
32
+
33
+ it "should not delegate if condition is not met" do
34
+ parent = Parent.new
35
+ parent.foo = "parent foo"
36
+
37
+ child = Child.new
38
+ child.parent = parent
39
+ child.foo = "child foo"
40
+
41
+ child.foo.should == "child foo"
42
+ end
43
+
44
+ it "should delegate multiple fields if condition is met" do
45
+ parent = Parent.new
46
+ parent.baz = "parent baz"
47
+ parent.qux = "parent qux"
48
+
49
+ child = Child.new
50
+ child.parent = parent
51
+ child.baz = "child baz"
52
+ child.qux = ""
53
+
54
+ child.baz.should == "child baz"
55
+ child.qux.should == "parent qux"
56
+ end
57
+
58
+ end
59
+
60
+ context "raising errors" do
61
+
62
+ it "should raise an error if the delegate is nil" do
63
+ child = Child.new
64
+ child.foo = ""
65
+
66
+ lambda { child.foo }.should raise_error(NoMethodError)
67
+ end
68
+
69
+ it "should not raise an error if the delegate is not nil" do
70
+ parent = Parent.new
71
+ parent.foo = "bar"
72
+
73
+ child = Child.new
74
+ child.parent = parent
75
+ child.foo = "foo"
76
+
77
+ lambda { child.foo }.should_not raise_error(NoMethodError)
78
+ end
79
+
80
+ end
81
+
82
+ context "lambda condition" do
83
+
84
+ it "should delegate if lambda condition is met" do
85
+ parent = Parent.new
86
+ parent.bar = "qux"
87
+
88
+ child = Child.new
89
+ child.parent = parent
90
+ child.bar = "something"
91
+
92
+ child.bar.should == "qux"
93
+ end
94
+
95
+ it "should not delegate if lambda condition is not met" do
96
+ parent = Parent.new
97
+ parent.bar = "qux"
98
+
99
+ child = Child.new
100
+ child.parent = parent
101
+ child.bar = "something else"
102
+
103
+ child.bar.should == "something else"
104
+ end
105
+
106
+ end
107
+
108
+ end
@@ -0,0 +1,20 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+ require 'conditional_delegate'
20
+
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conditional_delegate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Roel van Dijk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
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: ConditionalDelegate allows delegates to be conditional.
47
+ email:
48
+ - roel@rustradio.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - .rvmrc
56
+ - .travis.yml
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - conditional_delegate.gemspec
62
+ - lib/conditional_delegate.rb
63
+ - lib/conditional_delegate/version.rb
64
+ - spec/conditional_delegate_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: http://github.com/rdvdijk/conditional_delegate
67
+ licenses: []
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
+ segments:
79
+ - 0
80
+ hash: -3630418352649556830
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ segments:
88
+ - 0
89
+ hash: -3630418352649556830
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.24
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: ConditionalDelegate allows delegates to be conditional.
96
+ test_files:
97
+ - spec/conditional_delegate_spec.rb
98
+ - spec/spec_helper.rb