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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f50eddfb21b4ce5194f165f8a54aa0c0289de12
4
- data.tar.gz: 160a17d66a8b07abcd2b6d815c20f158d6f9c395
3
+ metadata.gz: fc80e0b4e561ceeb1b577e6177c2a792ce09e01b
4
+ data.tar.gz: b68bf200456ebc5d774cac5e037a1eac1c512101
5
5
  SHA512:
6
- metadata.gz: 290e75c1e27cf94be088089cadf1c8364dd042edf8c57e30cfd1c7bcf095cbc4c9ba647f011fd4a8c452110a485db9355584f89a052bd5421985ef864723c7e2
7
- data.tar.gz: 84782ae5584bbd95de9fed4d37c5b6bef3de28f958264a8e5855be9d97d14f1210c4efd661c742e5faa4f4aad9fac8ba48c96e682e5ab4b05994cfefa0e9524c
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
- include DelegateIt
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
@@ -1,35 +1,23 @@
1
1
  #encoding: UTF-8
2
2
  module DelegateIt
3
- def self.included(klass)
4
- klass.instance_eval do
5
- def delegate *methods, options
6
- @@delegated_settings ||= []
7
- methods.each do |method|
8
- @@delegated_settings.push(Struct.new(:name, :options).new(method, options))
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
- nil
10
+ method
24
11
  end
25
12
 
26
- if setting.options[:allow_nil] && !receiver
27
- nil
28
- else
29
- receiver.send(method, *args)
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
@@ -1,3 +1,3 @@
1
1
  module DelegateIt
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -2,13 +2,16 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  class Dummy
5
- include DelegateIt
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(subject.hello).to eq nil
104
+ expect do
105
+ subject.hello
106
+ end.to raise_error
76
107
  end
77
108
  end
78
109
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delegate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb