Class: GitlabSettings::Options
- Inherits:
-
Object
- Object
- GitlabSettings::Options
show all
- Extended by:
- Forwardable
- Defined in:
- lib/gitlab_settings/options.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(value) ⇒ Options
Returns a new instance of Options.
52
53
54
|
# File 'lib/gitlab_settings/options.rb', line 52
def initialize(value)
@options = value.deep_stringify_keys
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/gitlab_settings/options.rb', line 146
def method_missing(name, *args, &block)
name_string = +name.to_s
if name_string.chomp!("=")
return self[name_string] = args.first if key?(name_string)
elsif key?(name_string)
return self[name_string]
end
if @options.respond_to?(name)
error_msg = "Calling a hash method on #{self.class}: `#{name}`"
log_and_raise_dev_exception(error_msg, method: name)
return @options.public_send(name, *args, &block) end
raise ::GitlabSettings::MissingSetting, "option '#{name}' not defined"
end
|
Class Method Details
.build(obj) ⇒ Object
Recursively build GitlabSettings::Options
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/gitlab_settings/options.rb', line 41
def self.build(obj)
case obj
when Hash
new(obj.transform_values { |value| build(value) })
when Array
obj.map { |value| build(value) }
else
obj
end
end
|
Instance Method Details
#[](key) ⇒ Object
56
57
58
|
# File 'lib/gitlab_settings/options.rb', line 56
def [](key)
@options[key.to_s]
end
|
#[]=(key, value) ⇒ Object
60
61
62
|
# File 'lib/gitlab_settings/options.rb', line 60
def []=(key, value)
@options[key.to_s] = self.class.build(value)
end
|
#deep_merge(other) ⇒ Object
100
101
102
|
# File 'lib/gitlab_settings/options.rb', line 100
def deep_merge(other)
self.class.build(to_hash.deep_merge(other.deep_stringify_keys))
end
|
#deep_merge!(other) ⇒ Object
104
105
106
|
# File 'lib/gitlab_settings/options.rb', line 104
def deep_merge!(other)
@options = to_hash.deep_merge(other.deep_stringify_keys)
end
|
#default ⇒ Object
74
75
76
|
# File 'lib/gitlab_settings/options.rb', line 74
def default
@options['default']
end
|
#dup ⇒ Object
84
85
86
|
# File 'lib/gitlab_settings/options.rb', line 84
def dup
self.class.build(to_hash)
end
|
#is_a?(klass) ⇒ Boolean
108
109
110
111
112
|
# File 'lib/gitlab_settings/options.rb', line 108
def is_a?(klass)
return true if klass == Hash
super(klass)
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?
64
65
66
|
# File 'lib/gitlab_settings/options.rb', line 64
def key?(key)
@options.key?(key.to_s)
end
|
#merge(other) ⇒ Object
88
89
90
|
# File 'lib/gitlab_settings/options.rb', line 88
def merge(other)
self.class.build(to_hash.merge(other.deep_stringify_keys))
end
|
#merge!(other) ⇒ Object
92
93
94
|
# File 'lib/gitlab_settings/options.rb', line 92
def merge!(other)
@options = to_hash.merge(other.deep_stringify_keys)
end
|
#respond_to_missing?(name, include_all = false) ⇒ Boolean
166
167
168
169
170
|
# File 'lib/gitlab_settings/options.rb', line 166
def respond_to_missing?(name, include_all = false)
return true if key?(name)
@options.respond_to?(name, include_all)
end
|
#reverse_merge!(other) ⇒ Object
96
97
98
|
# File 'lib/gitlab_settings/options.rb', line 96
def reverse_merge!(other)
@options = to_hash.reverse_merge(other.deep_stringify_keys)
end
|
#stringify_keys! ⇒ Object
Also known as:
deep_stringify_keys!
Don't alter the internal keys
127
128
129
130
131
132
133
|
# File 'lib/gitlab_settings/options.rb', line 127
def stringify_keys!
error_msg = "Warning: Do not mutate #{self.class} objects: `#{__method__}`"
log_and_raise_dev_exception(error_msg, method: __method__)
to_hash.deep_stringify_keys
end
|
#symbolize_keys! ⇒ Object
Also known as:
deep_symbolize_keys!
Don't alter the internal keys
137
138
139
140
141
142
143
|
# File 'lib/gitlab_settings/options.rb', line 137
def symbolize_keys!
error_msg = "Warning: Do not mutate #{self.class} objects: `#{__method__}`"
log_and_raise_dev_exception(error_msg, method: __method__)
to_hash.deep_symbolize_keys
end
|
#to_hash ⇒ Object
Also known as:
to_h
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/gitlab_settings/options.rb', line 114
def to_hash
@options.deep_transform_values do |option|
case option
when self.class
option.to_hash
else
option
end
end
end
|
#with_indifferent_access ⇒ Object
80
81
82
|
# File 'lib/gitlab_settings/options.rb', line 80
def with_indifferent_access
to_hash.with_indifferent_access
end
|