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
- VERSION =
'0.10.3'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Switches
Returns a new instance of Switches.
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/Switches.rb', line 169
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
280
281
282
283
284
285
286
|
# File 'lib/Switches.rb', line 280
def method_missing(method_name, *args, &block)
if (@op.methods - Switches.instance_methods).include?(method_name.to_s)
@op.send(method_name.to_s, *args, &block)
else
@settings.send(method_name.to_s, *args, &block)
end
end
|
Instance Attribute Details
Returns the value of attribute settings.
167
168
169
|
# File 'lib/Switches.rb', line 167
def settings
@settings
end
|
Class Method Details
.as_h(*args) ⇒ Object
159
160
161
162
|
# File 'lib/Switches.rb', line 159
def as_h(*args)
switches = new(*args)
switches.to_h
end
|
Instance Method Details
227
228
229
230
231
232
233
|
# File 'lib/Switches.rb', line 227
def parse!
@op.parse!
check_required_switches
set_switches_with_defaults
set_unset_switches
to_h if @as_h
end
|
207
208
209
|
# File 'lib/Switches.rb', line 207
def perform(*attrs, &block)
do_action(false, *attrs, &block)
end
|
212
213
214
|
# File 'lib/Switches.rb', line 212
def perform!(*attrs, &block)
do_action(true, *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
|
#required!(*attrs, &block) ⇒ Object
Also known as:
required_switch!
201
202
203
204
|
# File 'lib/Switches.rb', line 201
def required!(*attrs, &block)
attrs.poke_options!({:required => true})
set!(*attrs, &block)
end
|
217
218
219
220
|
# File 'lib/Switches.rb', line 217
def required_perform(*attrs, &block)
attrs.poke_options!({:required => true})
perform(*attrs, &block)
end
|
222
223
224
225
|
# File 'lib/Switches.rb', line 222
def required_perform!(*attrs, &block)
attrs.poke_options!({:required => true})
perform!(*attrs, &block)
end
|
#set(*attrs, &block) ⇒ Object
185
186
187
|
# File 'lib/Switches.rb', line 185
def set(*attrs, &block)
do_set(false, *attrs, &block)
end
|
#set!(*attrs, &block) ⇒ Object
190
191
192
|
# File 'lib/Switches.rb', line 190
def set!(*attrs, &block)
do_set(true, *attrs, &block)
end
|
#supplied_switches ⇒ Object
235
236
237
|
# File 'lib/Switches.rb', line 235
def supplied_switches
@settings.to_h.keys.collect{|s| s.to_s}
end
|
#switches_with_castings ⇒ Object
243
244
245
|
# File 'lib/Switches.rb', line 243
def switches_with_castings
@castings.keys.collect{|default| default.to_s}
end
|
#switches_with_defaults ⇒ Object
239
240
241
|
# File 'lib/Switches.rb', line 239
def switches_with_defaults
@defaults.keys.collect{|default| default.to_s}
end
|
251
252
253
|
# File 'lib/Switches.rb', line 251
def to_h
@settings.to_h
end
|
#unset_switches ⇒ Object
247
248
249
|
# File 'lib/Switches.rb', line 247
def unset_switches
@all_switches - supplied_switches - switches_with_defaults
end
|