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.12.0'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Switches
Returns a new instance of Switches.
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/Switches.rb', line 172
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
285
286
287
288
289
290
291
|
# File 'lib/Switches.rb', line 285
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.
170
171
172
|
# File 'lib/Switches.rb', line 170
def settings
@settings
end
|
Class Method Details
.as_h(*args) ⇒ Object
162
163
164
165
|
# File 'lib/Switches.rb', line 162
def as_h(*args)
switches = new(*args)
switches.to_h
end
|
Instance Method Details
230
231
232
233
234
235
236
|
# File 'lib/Switches.rb', line 230
def parse!
@op.parse!
check_required_switches
set_switches_with_defaults
set_unset_switches
to_h if @as_h
end
|
210
211
212
|
# File 'lib/Switches.rb', line 210
def perform(*attrs, &block)
do_action(false, *attrs, &block)
end
|
215
216
217
|
# File 'lib/Switches.rb', line 215
def perform!(*attrs, &block)
do_action(true, *attrs, &block)
end
|
#required(*attrs, &block) ⇒ Object
Also known as:
required_switch
198
199
200
201
|
# File 'lib/Switches.rb', line 198
def required(*attrs, &block)
attrs.poke_options!({:required => true})
set(*attrs, &block)
end
|
#required!(*attrs, &block) ⇒ Object
Also known as:
required_switch!
204
205
206
207
|
# File 'lib/Switches.rb', line 204
def required!(*attrs, &block)
attrs.poke_options!({:required => true})
set!(*attrs, &block)
end
|
220
221
222
223
|
# File 'lib/Switches.rb', line 220
def required_perform(*attrs, &block)
attrs.poke_options!({:required => true})
perform(*attrs, &block)
end
|
225
226
227
228
|
# File 'lib/Switches.rb', line 225
def required_perform!(*attrs, &block)
attrs.poke_options!({:required => true})
perform!(*attrs, &block)
end
|
#set(*attrs, &block) ⇒ Object
188
189
190
|
# File 'lib/Switches.rb', line 188
def set(*attrs, &block)
do_set(false, *attrs, &block)
end
|
#set!(*attrs, &block) ⇒ Object
193
194
195
|
# File 'lib/Switches.rb', line 193
def set!(*attrs, &block)
do_set(true, *attrs, &block)
end
|
#supplied_switches ⇒ Object
238
239
240
|
# File 'lib/Switches.rb', line 238
def supplied_switches
@settings.to_h.keys.collect{|s| s.to_s}
end
|
#switches_with_castings ⇒ Object
246
247
248
|
# File 'lib/Switches.rb', line 246
def switches_with_castings
@castings.keys.collect{|default| default.to_s}
end
|
#switches_with_defaults ⇒ Object
242
243
244
|
# File 'lib/Switches.rb', line 242
def switches_with_defaults
@defaults.keys.collect{|default| default.to_s}
end
|
254
255
256
|
# File 'lib/Switches.rb', line 254
def to_h
@settings.to_h
end
|
#unset_switches ⇒ Object
250
251
252
|
# File 'lib/Switches.rb', line 250
def unset_switches
@all_switches - supplied_switches - switches_with_defaults
end
|