not_my_job 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +18 -0
- data/README.md +93 -0
- data/lib/not_my_job.rb +28 -0
- data/not_my_job.gemspec +15 -0
- data/spec/not_my_job_spec.rb +90 -0
- data/spec/spec_helper.rb +1 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.2.2)
|
5
|
+
rspec (2.13.0)
|
6
|
+
rspec-core (~> 2.13.0)
|
7
|
+
rspec-expectations (~> 2.13.0)
|
8
|
+
rspec-mocks (~> 2.13.0)
|
9
|
+
rspec-core (2.13.1)
|
10
|
+
rspec-expectations (2.13.0)
|
11
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
12
|
+
rspec-mocks (2.13.0)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
#Not My Job
|
2
|
+
|
3
|
+
Also known as EmpleadoPublico ;) provides a delegate class method to easily expose contained object's methods as your own.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
```
|
9
|
+
gem 'not_my_job'
|
10
|
+
```
|
11
|
+
And then execute:
|
12
|
+
```
|
13
|
+
$ bundle install
|
14
|
+
```
|
15
|
+
Or install it yourself as:
|
16
|
+
```
|
17
|
+
$ gem install not_my_job
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
```
|
22
|
+
delegate(*methods, to: target [, with_prefix: true|false ]) { block } → obj
|
23
|
+
```
|
24
|
+
In order to access the delegate object's methods, NotMyJob creates instance methods for each one specified method.
|
25
|
+
|
26
|
+
If the delegate object is nil, there are several options: If no block is provided, it will raise an NoMethodError exception; if the optional code block is specified, then that will be run and its result returned.
|
27
|
+
|
28
|
+
## Examples
|
29
|
+
|
30
|
+
Simple delegation:
|
31
|
+
```ruby
|
32
|
+
class Cuisine
|
33
|
+
attr_accessor :name, :chef
|
34
|
+
|
35
|
+
def initialize(name, chef)
|
36
|
+
@name = name
|
37
|
+
@chef = chef
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Restaurant
|
42
|
+
extend NotMyJob
|
43
|
+
|
44
|
+
delegate :name, to: :cuisine
|
45
|
+
delegate :chef, to: :cuisine, with_prefix: false
|
46
|
+
|
47
|
+
def initialize(cuisine)
|
48
|
+
@cuisine = cuisine
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
cuisine = Cuisine.new("Italian", "Mario")
|
53
|
+
restaurant = Restauran.new(cuisine)
|
54
|
+
|
55
|
+
restaurant.cuisine_name #=> "Italian"
|
56
|
+
restaurant.chef #=> "Mario"
|
57
|
+
```
|
58
|
+
|
59
|
+
Multiple methods delegation:
|
60
|
+
```ruby
|
61
|
+
class Restaurant
|
62
|
+
extend NotMyJob
|
63
|
+
delegate :name, :chef to: :cuisine
|
64
|
+
end
|
65
|
+
|
66
|
+
restaurant.cuisine_name #=> "Italian"
|
67
|
+
restaurant.cuisine_chef #=> "Mario"
|
68
|
+
```
|
69
|
+
|
70
|
+
ActiveRecord Associations:
|
71
|
+
```ruby
|
72
|
+
class Restaurant < ActiveRecord::Base
|
73
|
+
extend NotMyJob
|
74
|
+
|
75
|
+
belongs_to :place
|
76
|
+
delegate :name, to: :place
|
77
|
+
end
|
78
|
+
restaurant.place_name #=> "Argentina"
|
79
|
+
```
|
80
|
+
|
81
|
+
If the delegate object is nil and a block is provided:
|
82
|
+
```ruby
|
83
|
+
class Restaurant
|
84
|
+
extend NotMyJob
|
85
|
+
|
86
|
+
delegate :name, to: :category do
|
87
|
+
logger.debug "Category is nil"
|
88
|
+
send_notification_email
|
89
|
+
"No category has been assigned to this restaurant"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
restaurant.category_name #=> "No category has been assigned to this restaurant"
|
93
|
+
```
|
data/lib/not_my_job.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module NotMyJob
|
2
|
+
|
3
|
+
def delegate(*methods, options, &block)
|
4
|
+
unless options.is_a? Hash
|
5
|
+
raise ArgumentError, "At least one method and the :to option are required."
|
6
|
+
end
|
7
|
+
|
8
|
+
to = options.fetch(:to) do
|
9
|
+
raise ArgumentError, "The :to option is required."
|
10
|
+
end
|
11
|
+
|
12
|
+
with_prefix = options.fetch(:with_prefix, true)
|
13
|
+
method_prefix = with_prefix ? "#{to}_" : ""
|
14
|
+
|
15
|
+
methods.each do |method|
|
16
|
+
method_name = "#{method_prefix}#{method}"
|
17
|
+
|
18
|
+
define_method method_name do
|
19
|
+
begin
|
20
|
+
object = instance_variable_get("@#{to}") || self.send(to)
|
21
|
+
object.public_send method
|
22
|
+
rescue NoMethodError
|
23
|
+
block_given? ? yield : raise
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/not_my_job.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = "not_my_job"
|
5
|
+
gem.version = "0.0.1"
|
6
|
+
gem.summary = "Provides a delegate class method to easily expose contained object's methods as your own."
|
7
|
+
gem.description = "Provides a delegate class method to easily expose contained object's methods as your own. If an optional code block is specified, then that will be run and its result returned when the delegate object is nil."
|
8
|
+
gem.author = "Alfredo Ramírez"
|
9
|
+
gem.email = "alfredormz@gmail.com"
|
10
|
+
gem.homepage = 'http://github.com/alfredormz/not_my_job'
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($/)
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.add_development_dependency "rspec"
|
15
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
describe Module do
|
5
|
+
|
6
|
+
class Place
|
7
|
+
attr_accessor :name, :latitude, :longitude
|
8
|
+
def initialize(name, latitude=nil, longitude=nil)
|
9
|
+
@name = name
|
10
|
+
@latitude = latitude
|
11
|
+
@longitude = longitude
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Restaurant
|
16
|
+
extend NotMyJob
|
17
|
+
|
18
|
+
attr_accessor :place
|
19
|
+
def initialize(place=nil)
|
20
|
+
@place = place || Place.new("Argentina", -34, -58)
|
21
|
+
end
|
22
|
+
|
23
|
+
def category
|
24
|
+
OpenStruct.new(name: "Italian")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "Simple delegation" do
|
29
|
+
let(:restaurant) do
|
30
|
+
class Restaurant
|
31
|
+
delegate :name, to: :place
|
32
|
+
delegate :latitude, to: :place, with_prefix: false
|
33
|
+
delegate :name, to: :category
|
34
|
+
end
|
35
|
+
|
36
|
+
Restaurant.new
|
37
|
+
end
|
38
|
+
|
39
|
+
it {expect(restaurant.respond_to? :place_name).to be_true }
|
40
|
+
it {expect(restaurant.respond_to? :latitude).to be_true }
|
41
|
+
it {expect(restaurant.respond_to? :category_name).to be_true }
|
42
|
+
|
43
|
+
it {expect(restaurant.place_name).to eq "Argentina"}
|
44
|
+
it {expect(restaurant.latitude).to eq(-34)}
|
45
|
+
it {expect(restaurant.category_name).to eq "Italian" }
|
46
|
+
end
|
47
|
+
|
48
|
+
context "Delegate multiple methods" do
|
49
|
+
let(:restaurant) do
|
50
|
+
class Restaurant
|
51
|
+
delegate :name, :latitude, :longitude, to: :place
|
52
|
+
end
|
53
|
+
|
54
|
+
Restaurant.new
|
55
|
+
end
|
56
|
+
|
57
|
+
it {expect(restaurant.respond_to? :place_name).to be_true }
|
58
|
+
it {expect(restaurant.respond_to? :place_latitude).to be_true }
|
59
|
+
it {expect(restaurant.respond_to? :place_longitude).to be_true }
|
60
|
+
|
61
|
+
it {expect(restaurant.place_name).to eq "Argentina"}
|
62
|
+
it {expect(restaurant.place_latitude).to eq(-34)}
|
63
|
+
it {expect(restaurant.place_longitude).to eq(-58)}
|
64
|
+
end
|
65
|
+
|
66
|
+
context "The delegate object is nil" do
|
67
|
+
let(:restaurant) do
|
68
|
+
class Restaurant
|
69
|
+
delegate :name, to: :some_object
|
70
|
+
end
|
71
|
+
|
72
|
+
Restaurant.new
|
73
|
+
end
|
74
|
+
it {expect{restaurant.some_object_name}.to raise_error(NoMethodError)}
|
75
|
+
end
|
76
|
+
|
77
|
+
context "The delegate object is nil but a block is provided" do
|
78
|
+
let(:restaurant) do
|
79
|
+
class Restaurant
|
80
|
+
delegate :name, to: :some_object do
|
81
|
+
"No data found"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
Restaurant.new
|
86
|
+
end
|
87
|
+
it {expect(restaurant.some_object_name).to eq "No data found"}
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require './lib/not_my_job'
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: not_my_job
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alfredo Ramírez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
type: :development
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
name: rspec
|
30
|
+
description: Provides a delegate class method to easily expose contained object's
|
31
|
+
methods as your own. If an optional code block is specified, then that will be run
|
32
|
+
and its result returned when the delegate object is nil.
|
33
|
+
email: alfredormz@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- Gemfile.lock
|
41
|
+
- README.md
|
42
|
+
- lib/not_my_job.rb
|
43
|
+
- not_my_job.gemspec
|
44
|
+
- spec/not_my_job_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
homepage: http://github.com/alfredormz/not_my_job
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Provides a delegate class method to easily expose contained object's methods
|
70
|
+
as your own.
|
71
|
+
test_files:
|
72
|
+
- spec/not_my_job_spec.rb
|
73
|
+
- spec/spec_helper.rb
|