Skip to content

Commit 10a5164

Browse files
authored
Merge pull request #12 from jmgarnier/add_reader_options
Add reader options
2 parents c9caf52 + ddd87a1 commit 10a5164

File tree

2 files changed

+89
-15
lines changed

2 files changed

+89
-15
lines changed

lib/dry/initializer/builder.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,19 @@ def copy(&block)
7676
end
7777

7878
def define_readers(mixin)
79-
readers = @signature.select { |item| item.settings[:reader] != false }
80-
.map(&:name)
79+
define_reader(
80+
mixin,
81+
:attr_reader,
82+
->(item) { item.settings[:reader] != false }
83+
)
84+
define_reader mixin, :private
85+
define_reader mixin, :protected
86+
end
8187

82-
mixin.send :attr_reader, *readers if readers.any?
88+
def define_reader(mixin, method, filter_lambda = nil)
89+
filter_lambda ||= ->(item) { item.settings[:reader] == method }
90+
readers = @signature.select(&filter_lambda).map(&:name)
91+
mixin.send method, *readers if readers.any?
8392
end
8493

8594
def reload_initializer(mixin)

spec/dry/reader_spec.rb

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,87 @@
11
describe "reader" do
2-
subject do
3-
class Test::Foo
4-
extend Dry::Initializer::Mixin
2+
shared_examples "it has no public attr_reader" do
3+
it "does not define a public attr_reader" do
4+
expect(subject).not_to respond_to :foo
5+
expect(subject).not_to respond_to :bar
6+
end
7+
end
8+
9+
context "with reader: :public or no reader: option" do
10+
subject do
11+
class Test::Foo
12+
extend Dry::Initializer::Mixin
13+
14+
param :foo
15+
param :foo2, reader: :public
16+
option :bar
17+
option :bar2, reader: :public
18+
end
19+
20+
Test::Foo.new 1, 2, bar: 3, bar2: 4
21+
end
522

6-
param :foo, reader: false
7-
option :bar, reader: false
23+
it "defines a public attr_reader by default" do
24+
expect(subject).to respond_to(:foo, :foo2)
25+
expect(subject).to respond_to :bar
26+
expect(subject).to respond_to :bar2
827
end
28+
end
29+
30+
context "with reader: false" do
31+
subject do
32+
class Test::Foo
33+
extend Dry::Initializer::Mixin
34+
35+
param :foo, reader: false
36+
option :bar, reader: false
37+
end
38+
39+
Test::Foo.new 1, bar: 2
40+
end
41+
42+
it_behaves_like "it has no public attr_reader"
943

10-
Test::Foo.new 1, bar: 2
44+
it "keeps assigning variables" do
45+
expect(subject.instance_variable_get(:@foo)).to eql 1
46+
expect(subject.instance_variable_get(:@bar)).to eql 2
47+
end
1148
end
1249

13-
it "skips attr_reader" do
14-
expect(subject).not_to respond_to :foo
15-
expect(subject).not_to respond_to :bar
50+
context "with reader: :private" do
51+
subject do
52+
class Test::Foo
53+
extend Dry::Initializer::Mixin
54+
55+
param :foo, reader: :private
56+
option :bar, reader: :private
57+
end
58+
59+
Test::Foo.new 1, bar: 2
60+
end
61+
62+
it_behaves_like "it has no public attr_reader"
63+
64+
it "adds a private attr_reader" do
65+
expect(subject.send(:foo)).to eql 1
66+
expect(subject.send(:bar)).to eql 2
67+
end
1668
end
1769

18-
it "keeps assigning variables" do
19-
expect(subject.instance_variable_get(:@foo)).to eql 1
20-
expect(subject.instance_variable_get(:@bar)).to eql 2
70+
context "with reader: :protected" do
71+
subject do
72+
class Test::Foo
73+
extend Dry::Initializer::Mixin
74+
75+
param :foo, reader: :protected
76+
option :bar, reader: :protected
77+
end
78+
79+
Test::Foo.new 1, bar: 2
80+
end
81+
82+
it "adds a protected attr_reader" do
83+
protected_instance_methods = subject.class.protected_instance_methods
84+
expect(protected_instance_methods).to match_array([:foo, :bar])
85+
end
2186
end
2287
end

0 commit comments

Comments
 (0)