Skip to content

Commit 7f4860d

Browse files
Copilotfletchto99
andcommitted
Refactor NOOP config creation to eliminate code duplication
Co-authored-by: fletchto99 <718681+fletchto99@users.noreply.github.com>
1 parent 622ddf3 commit 7f4860d

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

lib/secure_headers/configuration.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ class << self
1414
# Returns nothing
1515
def disable!
1616
@disabled = true
17-
# Create a NOOP config that opts out of all headers
18-
@noop_config = new do |config|
19-
CONFIG_ATTRIBUTES.each do |attr|
20-
config.instance_variable_set("@#{attr}", OPT_OUT)
21-
end
22-
end.freeze
17+
@noop_config = create_noop_config.freeze
2318
end
2419

2520
# Public: Check if secure_headers is disabled
@@ -41,11 +36,7 @@ def default(&block)
4136

4237
# Define a built-in override that clears all configuration options and
4338
# results in no security headers being set.
44-
override(NOOP_OVERRIDE) do |config|
45-
CONFIG_ATTRIBUTES.each do |attr|
46-
config.instance_variable_set("@#{attr}", OPT_OUT)
47-
end
48-
end
39+
override(NOOP_OVERRIDE, &method(:create_noop_config_block))
4940

5041
new_config = new(&block).freeze
5142
new_config.validate_config!
@@ -137,6 +128,18 @@ def deep_copy_if_hash(value)
137128
value
138129
end
139130
end
131+
132+
# Private: Creates a NOOP configuration that opts out of all headers
133+
def create_noop_config
134+
new(&method(:create_noop_config_block))
135+
end
136+
137+
# Private: Block for creating NOOP configuration
138+
def create_noop_config_block(config)
139+
CONFIG_ATTRIBUTES.each do |attr|
140+
config.instance_variable_set("@#{attr}", OPT_OUT)
141+
end
142+
end
140143
end
141144

142145
CONFIG_ATTRIBUTES_TO_HEADER_CLASSES = {

0 commit comments

Comments
 (0)