Class: Switches
- Inherits:
-
Object
show all
- Defined in:
- lib/Switches/VERSION.rb,
lib/Switches.rb
Overview
lib/Switches/VERSION.rb
Switches::VERSION
Constant Summary
collapse
- NEVER_FORWARDED =
Names Ruby may call implicitly, and which must reach the settings rather than OptionParser. #to_a would otherwise answer Array() and the splat with the parser's help.
[:to_a, :to_ary, :to_h, :to_hash, :to_s, :to_str, :pretty_print]
- VERSION =
'0.11.0'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Switches
Returns a new instance of Switches.
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/Switches.rb', line 171
def initialize(*args)
options = args.
self.class.send(:include, CastingInterfaceMethods) if options[:include_casting_interface_methods].default_is(true)
@as_h = (options[:as_h] || options[:to_h] || options[:as_hash] || options[:as_a_hash]).default_is(false)
@settings = OpenStruct.new
@op = OptionParser.new
@required_switches = []
@all_switches = []
@defaults = {}
@castings = {}
if block_given?
yield self
parse!
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
282
283
284
285
286
287
288
|
# File 'lib/Switches.rb', line 282
def method_missing(method_name, *args, &block)
if option_parser_method?(method_name) && !declared_switch?(method_name)
@op.send(method_name, *args, &block)
else
@settings.send(method_name, *args, &block)
end
end
|
Instance Attribute Details
Returns the value of attribute settings.
169
170
171
|
# File 'lib/Switches.rb', line 169
def settings
@settings
end
|
Class Method Details
.as_h(*args) ⇒ Object
161
162
163
164
|
# File 'lib/Switches.rb', line 161
def as_h(*args)
switches = new(*args)
switches.to_h
end
|
Instance Method Details
229
230
231
232
233
234
235
|
# File 'lib/Switches.rb', line 229
def parse!
@op.parse!
check_required_switches
set_switches_with_defaults
set_unset_switches
to_h if @as_h
end
|
209
210
211
|
# File 'lib/Switches.rb', line 209
def perform(*attrs, &block)
do_action(false, *attrs, &block)
end
|
214
215
216
|
# File 'lib/Switches.rb', line 214
def perform!(*attrs, &block)
do_action(true, *attrs, &block)
end
|
#required(*attrs, &block) ⇒ Object
Also known as:
required_switch
197
198
199
200
|
# File 'lib/Switches.rb', line 197
def required(*attrs, &block)
attrs.poke_options!({:required => true})
set(*attrs, &block)
end
|
#required!(*attrs, &block) ⇒ Object
Also known as:
required_switch!
203
204
205
206
|
# File 'lib/Switches.rb', line 203
def required!(*attrs, &block)
attrs.poke_options!({:required => true})
set!(*attrs, &block)
end
|
219
220
221
222
|
# File 'lib/Switches.rb', line 219
def required_perform(*attrs, &block)
attrs.poke_options!({:required => true})
perform(*attrs, &block)
end
|
224
225
226
227
|
# File 'lib/Switches.rb', line 224
def required_perform!(*attrs, &block)
attrs.poke_options!({:required => true})
perform!(*attrs, &block)
end
|
#set(*attrs, &block) ⇒ Object
187
188
189
|
# File 'lib/Switches.rb', line 187
def set(*attrs, &block)
do_set(false, *attrs, &block)
end
|
#set!(*attrs, &block) ⇒ Object
192
193
194
|
# File 'lib/Switches.rb', line 192
def set!(*attrs, &block)
do_set(true, *attrs, &block)
end
|
#supplied_switches ⇒ Object
237
238
239
|
# File 'lib/Switches.rb', line 237
def supplied_switches
@settings.to_h.keys.collect{|s| s.to_s}
end
|
#switches_with_castings ⇒ Object
245
246
247
|
# File 'lib/Switches.rb', line 245
def switches_with_castings
@castings.keys.collect{|default| default.to_s}
end
|
#switches_with_defaults ⇒ Object
241
242
243
|
# File 'lib/Switches.rb', line 241
def switches_with_defaults
@defaults.keys.collect{|default| default.to_s}
end
|
253
254
255
|
# File 'lib/Switches.rb', line 253
def to_h
@settings.to_h
end
|
#unset_switches ⇒ Object
249
250
251
|
# File 'lib/Switches.rb', line 249
def unset_switches
@all_switches - supplied_switches - switches_with_defaults
end
|