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.1'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Switches
Returns a new instance of Switches.
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/Switches.rb', line 163
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
276
277
278
279
280
281
282
|
# File 'lib/Switches.rb', line 276
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.
161
162
163
|
# File 'lib/Switches.rb', line 161
def settings
@settings
end
|
Class Method Details
.as_h(*args) ⇒ Object
153
154
155
156
|
# File 'lib/Switches.rb', line 153
def as_h(*args)
switches = new(*args)
switches.to_h
end
|
Instance Method Details
221
222
223
224
225
226
227
|
# File 'lib/Switches.rb', line 221
def parse!
@op.parse!
check_required_switches
set_switches_with_defaults
set_unset_switches
to_h if @as_h
end
|
201
202
203
|
# File 'lib/Switches.rb', line 201
def perform(*attrs, &block)
do_action(false, *attrs, &block)
end
|
206
207
208
|
# File 'lib/Switches.rb', line 206
def perform!(*attrs, &block)
do_action(true, *attrs, &block)
end
|
#required(*attrs, &block) ⇒ Object
Also known as:
required_switch
189
190
191
192
|
# File 'lib/Switches.rb', line 189
def required(*attrs, &block)
attrs.poke_options!({:required => true})
set(*attrs, &block)
end
|
#required!(*attrs, &block) ⇒ Object
Also known as:
required_switch!
195
196
197
198
|
# File 'lib/Switches.rb', line 195
def required!(*attrs, &block)
attrs.poke_options!({:required => true})
set!(*attrs, &block)
end
|
211
212
213
214
|
# File 'lib/Switches.rb', line 211
def required_perform(*attrs, &block)
attrs.poke_options!({:required => true})
perform(*attrs, &block)
end
|
216
217
218
219
|
# File 'lib/Switches.rb', line 216
def required_perform!(*attrs, &block)
attrs.poke_options!({:required => true})
perform!(*attrs, &block)
end
|
#set(*attrs, &block) ⇒ Object
179
180
181
|
# File 'lib/Switches.rb', line 179
def set(*attrs, &block)
do_set(false, *attrs, &block)
end
|
#set!(*attrs, &block) ⇒ Object
184
185
186
|
# File 'lib/Switches.rb', line 184
def set!(*attrs, &block)
do_set(true, *attrs, &block)
end
|
#supplied_switches ⇒ Object
229
230
231
|
# File 'lib/Switches.rb', line 229
def supplied_switches
@settings.to_h.keys.collect{|s| s.to_s}
end
|
#switches_with_castings ⇒ Object
237
238
239
|
# File 'lib/Switches.rb', line 237
def switches_with_castings
@castings.keys.collect{|default| default.to_s}
end
|
#switches_with_defaults ⇒ Object
233
234
235
|
# File 'lib/Switches.rb', line 233
def switches_with_defaults
@defaults.keys.collect{|default| default.to_s}
end
|
245
246
247
|
# File 'lib/Switches.rb', line 245
def to_h
@settings.to_h
end
|
#unset_switches ⇒ Object
241
242
243
|
# File 'lib/Switches.rb', line 241
def unset_switches
@all_switches - supplied_switches - switches_with_defaults
end
|