module Optser

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)

Constants

VERSION

Public Class Methods

extract_options(args) click to toggle source

Parses the options from the arguments list, and creates a brand new OptSet instance to lookup the options; but does not modify the args.

# 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) click to toggle source

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.

# 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) click to toggle source

Creates a new OptSet from the given options.

# File lib/optser.rb, line 45
def self.parse_options(options)
  return Optser::OptSet.new options
end