delegate_it 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/delegate_it/main.rb +14 -26
- data/lib/delegate_it/version.rb +1 -1
- data/spec/delegate_it/main_spec.rb +33 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc80e0b4e561ceeb1b577e6177c2a792ce09e01b
|
4
|
+
data.tar.gz: b68bf200456ebc5d774cac5e037a1eac1c512101
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89f4b36d01c98e19628f1c32e64ddbaa3d1eca96e152028c4971aaefd54fe54f0cbe84cb9d38b521cf435eb96ab03a8710791c9363eb5321b6ad0c13d1880165
|
7
|
+
data.tar.gz: 0b99c3256034d4b67d50733fa14ab35ae444077fc8e61473f0394ce6a8ff2308639b17bffbfabd25f77478efe41b0981f17126fd3bb4b628d8f04075c02cc7fc
|
data/README.md
CHANGED
@@ -14,11 +14,15 @@ gem 'delegate_it'
|
|
14
14
|
|
15
15
|
```` ruby
|
16
16
|
class Offer
|
17
|
-
|
17
|
+
extend DelegateIt
|
18
18
|
|
19
19
|
delegate :name, :description, to: :product
|
20
20
|
delegate :brand, to: :manufacturer, allow_nil: true
|
21
21
|
|
22
|
+
def manufacturer
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
|
22
26
|
private
|
23
27
|
|
24
28
|
def product
|
data/lib/delegate_it/main.rb
CHANGED
@@ -1,35 +1,23 @@
|
|
1
1
|
#encoding: UTF-8
|
2
2
|
module DelegateIt
|
3
|
-
def
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def method_missing(method, *args)
|
15
|
-
setting = @@delegated_settings.select do |s|
|
16
|
-
s.name == method
|
17
|
-
end.first
|
18
|
-
|
19
|
-
if setting
|
20
|
-
receiver = if self.respond_to?(setting.options[:to], true)
|
21
|
-
self.send(setting.options[:to])
|
3
|
+
def delegate *methods, options
|
4
|
+
methods.each do |method|
|
5
|
+
method_name = if options[:prefix] && options[:prefix].is_a?(Symbol)
|
6
|
+
"#{options[:prefix]}_#{method}"
|
7
|
+
elsif options[:prefix]
|
8
|
+
"#{options[:to]}_#{method}"
|
22
9
|
else
|
23
|
-
|
10
|
+
method
|
24
11
|
end
|
25
12
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
13
|
+
define_method method_name do |*args|
|
14
|
+
receiver = self.send(options[:to])
|
15
|
+
if options[:allow_nil] && !receiver
|
16
|
+
nil
|
17
|
+
else
|
18
|
+
receiver.send(method, *args)
|
19
|
+
end
|
30
20
|
end
|
31
|
-
else
|
32
|
-
super
|
33
21
|
end
|
34
22
|
end
|
35
23
|
end
|
data/lib/delegate_it/version.rb
CHANGED
@@ -2,13 +2,16 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
class Dummy
|
5
|
-
|
5
|
+
extend DelegateIt
|
6
6
|
delegate :name, :surname, to: :name_delegate
|
7
7
|
delegate :hello, to: :no_exist, allow_nil: true
|
8
8
|
delegate :there, to: :there_delegate, allow_nil: true
|
9
9
|
delegate :one, to: :args_delegate
|
10
10
|
delegate :print, to: :args_delegate
|
11
11
|
delegate :secret, to: :keeper
|
12
|
+
delegate :i_am_not, to: :no_exist
|
13
|
+
delegate :price, to: :offer, prefix: true
|
14
|
+
delegate :cost, to: :offer, prefix: :delivery
|
12
15
|
|
13
16
|
def name_delegate
|
14
17
|
Struct.new(:name, :surname).new('name_val', 'surname_val')
|
@@ -32,6 +35,10 @@ class Dummy
|
|
32
35
|
dynamic_class.new
|
33
36
|
end
|
34
37
|
|
38
|
+
def offer
|
39
|
+
Struct.new(:price, :cost).new(123, 456)
|
40
|
+
end
|
41
|
+
|
35
42
|
private
|
36
43
|
|
37
44
|
def keeper
|
@@ -63,16 +70,40 @@ describe 'DelegateIt' do
|
|
63
70
|
end
|
64
71
|
end
|
65
72
|
|
73
|
+
describe "nonexisting delegate" do
|
74
|
+
it "raises an error" do
|
75
|
+
expect do
|
76
|
+
subject.i_am_not
|
77
|
+
end.to raise_error
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
66
81
|
describe "private delegators" do
|
67
82
|
it "works" do
|
68
83
|
expect(subject.secret).to eq 42
|
69
84
|
end
|
70
85
|
end
|
71
86
|
|
87
|
+
describe "prefixes" do
|
88
|
+
context "default value" do
|
89
|
+
it "prefixes method name with delegate name" do
|
90
|
+
expect(subject.offer_price).to eq 123
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "custom value" do
|
95
|
+
it "prefixes method name with it" do
|
96
|
+
expect(subject.delivery_cost).to eq 456
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
72
101
|
describe "allow_nil case" do
|
73
102
|
context "receiver is nil" do
|
74
103
|
it "returns nil" do
|
75
|
-
expect
|
104
|
+
expect do
|
105
|
+
subject.hello
|
106
|
+
end.to raise_error
|
76
107
|
end
|
77
108
|
end
|
78
109
|
|