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.extract_options! 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

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.extract_options(args)
  return parse_options(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.extract_options!(args)
  return parse_options(args.last.is_a?(::Hash) ? args.pop : nil)
end

.parse_options(options) ⇒ Object

Creates a new OptSet from the given options.



45
46
47
# File 'lib/optser.rb', line 45

def self.parse_options(options)
  return Optser::OptSet.new options
end