Skip to content

Commit 8ff424f

Browse files
authored
Protect internal accessors (#5)
1 parent 9addc32 commit 8ff424f

File tree

13 files changed

+58
-6
lines changed

13 files changed

+58
-6
lines changed

lib/stroma/hooks/collection.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def initialize(collection = Set.new)
5959
# @return [void]
6060
def initialize_dup(original)
6161
super
62-
@collection = original.instance_variable_get(:@collection).dup
62+
@collection = original.collection.dup
6363
end
6464

6565
# Adds a new hook to the collection.
@@ -96,6 +96,10 @@ def before(key)
9696
def after(key)
9797
@collection.select { |hook| hook.after? && hook.target_key == key }
9898
end
99+
100+
protected
101+
102+
attr_reader :collection
99103
end
100104
end
101105
end

lib/stroma/settings/collection.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def initialize(storage = {})
6161
# @return [void]
6262
def initialize_dup(original)
6363
super
64-
@storage = original.instance_variable_get(:@storage).transform_values(&:dup)
64+
@storage = original.storage.transform_values(&:dup)
6565
end
6666

6767
# Accesses or creates RegistrySettings for a registry key.
@@ -83,6 +83,10 @@ def [](registry_key)
8383
def to_h
8484
@storage.transform_values(&:to_h)
8585
end
86+
87+
protected
88+
89+
attr_reader :storage
8690
end
8791
end
8892
end

lib/stroma/settings/registry_settings.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def initialize(storage = {})
5959
# @return [void]
6060
def initialize_dup(original)
6161
super
62-
@storage = original.instance_variable_get(:@storage).transform_values(&:dup)
62+
@storage = original.storage.transform_values(&:dup)
6363
end
6464

6565
# Accesses or creates a Setting for an extension.
@@ -81,6 +81,10 @@ def [](extension_name)
8181
def to_h
8282
@storage.transform_values(&:to_h)
8383
end
84+
85+
protected
86+
87+
attr_reader :storage
8488
end
8589
end
8690
end

lib/stroma/settings/setting.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def initialize(data = {})
6969
# @return [void]
7070
def initialize_dup(original)
7171
super
72-
@data = deep_dup(original.instance_variable_get(:@data))
72+
@data = deep_dup(original.data)
7373
end
7474

7575
# Converts to a plain Hash.
@@ -93,6 +93,10 @@ def fetch(key, *args, &block)
9393
@data.fetch(key.to_sym, *args, &block)
9494
end
9595

96+
protected
97+
98+
attr_reader :data
99+
96100
private
97101

98102
# Recursively duplicates nested Hash and Array structures.

lib/stroma/state.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def initialize
6060
# @return [void]
6161
def initialize_dup(original)
6262
super
63-
@hooks = original.instance_variable_get(:@hooks).dup
64-
@settings = original.instance_variable_get(:@settings).dup
63+
@hooks = original.hooks.dup
64+
@settings = original.settings.dup
6565
end
6666
end
6767
end

sig/lib/stroma/hooks/collection.rbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ module Stroma
2222
def empty?: () -> bool
2323

2424
def size: () -> Integer
25+
26+
# Protected in Ruby; declared here for Steep visibility in initialize_dup
27+
attr_reader collection: Set[Hook]
2528
end
2629
end
2730
end

sig/lib/stroma/settings/collection.rbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ module Stroma
2222
def empty?: () -> bool
2323

2424
def to_h: () -> Hash[Symbol, Hash[Symbol, Hash[Symbol, untyped]]]
25+
26+
# Protected in Ruby; declared here for Steep visibility in initialize_dup
27+
attr_reader storage: Hash[Symbol, RegistrySettings]
2528
end
2629
end
2730
end

sig/lib/stroma/settings/registry_settings.rbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ module Stroma
2222
def empty?: () -> bool
2323

2424
def to_h: () -> Hash[Symbol, Hash[Symbol, untyped]]
25+
26+
# Protected in Ruby; declared here for Steep visibility in initialize_dup
27+
attr_reader storage: Hash[Symbol, Setting]
2528
end
2629
end
2730
end

sig/lib/stroma/settings/setting.rbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ module Stroma
3232
| (Symbol key, untyped default) -> untyped
3333
| (Symbol key) { (Symbol) -> untyped } -> untyped
3434

35+
# Protected in Ruby; declared here for Steep visibility in initialize_dup
36+
attr_reader data: Hash[Symbol, untyped]
37+
3538
private
3639

3740
def deep_dup: (untyped obj) -> untyped

spec/stroma/hooks/collection_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,10 @@
110110
expect(hooks.map(&:itself)).to all(be_a(Stroma::Hooks::Hook))
111111
end
112112
end
113+
114+
describe "protected interface" do
115+
it "does not expose collection publicly" do
116+
expect { hooks.collection }.to raise_error(NoMethodError)
117+
end
118+
end
113119
end

0 commit comments

Comments
 (0)