Module: Optser
- Defined in:
- lib/optser.rb,
lib/optser/opt_set.rb,
lib/optser/version.rb
Overview
Module that provides helpers for dealing with options hash passed to initializers.
require 'optser'
class Example
def initialize(*args, &block)
opts = Optser. args
@a = opts.get! :mandatory_option
@b = opts.get :my_option, 'Default Value'
puts args # Rest of args w/o the options hash.
end
end
Example.new(1, 2, 3, 4, mandatory_option: true)
Defined Under Namespace
Classes: OptSet
Constant Summary collapse
- VERSION =
'0.1.1'
Class Method Summary collapse
-
.extract_options(args) ⇒ Object
Parses the options from the arguments list, and creates a brand new OptSet instance to lookup the options; but does not modify the args.
-
.extract_options!(args) ⇒ Object
Extracts the options from the arguments list, and creates a brand new OptSet instance to lookup the options; pops the options off the args list if we have it.
-
.parse_options(options) ⇒ Object
Creates a new OptSet from the given options.
Class Method Details
.extract_options(args) ⇒ Object
Parses the options from the arguments list, and creates a brand new OptSet instance to lookup the options; but does not modify the args.
29 30 31 |
# File 'lib/optser.rb', line 29 def self.(args) return (args.last.is_a?(::Hash) ? args.last : nil) end |
.extract_options!(args) ⇒ Object
Extracts the options from the arguments list, and creates a brand new OptSet instance to lookup the options; pops the options off the args list if we have it.
38 39 40 |
# File 'lib/optser.rb', line 38 def self.(args) return (args.last.is_a?(::Hash) ? args.pop : nil) end |