class OptionList

The file option_list.rb and the class OptionList.

This file contains the code for the OptionList class that implements smart and easy options for functions. It has the virtue of defining function options clearly in a single place instead of spread out in complex code.

User's Guide

Most of the documentation for this gem has been moved to Option List User’s Guide

Version 1.1.1

Embraced reek and git and created the user's guide to pair down rdoc mark up to be less obtrusive to the code.

Version 1.1.0

Added a default value of false to signify that no default exists for a mandatory parameter. Modified processing of array specs to avoid side effects in the parameters.

Constants

VERSION

Public Class Methods

new(*option_specs, &select_block) click to toggle source

Create an option list from an array of option specifications.

Parameters:

  • option_specs - The comma separated option specifications, made into an array by the splat operator.

  • select_block - An optional block of code that is called when selections have been made. This allows for custom validations to be applied at that point. This block should accept one argument, a reference to the option list value object that is calling it for validation.

Exceptions:

  • ArgumentError for a number of invalid argument conditions.

# File lib/option_list.rb, line 39
def initialize(*option_specs, &select_block)
  error "Missing option specifications." if option_specs.empty?
  @mandatory  = Array.new
  @categories = Hash.new
  @default    = Hash.new

  option_specs.each do |spec|
    if spec.is_a?(Hash)
      hash_spec(spec)
    elsif spec.is_a?(Array)
      array_spec(spec)
    else
      error "Found #{spec.class} instead of Hash or Array."
    end
  end

  @select_block = select_block
end
version() click to toggle source

The option list code version.

# File lib/option_list.rb, line 20
def self.version
  OptionList::VERSION
end

Public Instance Methods

select(selections=[]) click to toggle source

From the possible options, select the actual, in force, options and return an access object for use in the code.

Parameters:

  • selections - An array of the options passed into the client function, usually with the splat operator. Note that if select is called with no arguments, all of the default values will be selected.

Returns:

An option value object which is a singleton subclass of the Hash class.

Exceptions:

  • ArgumentError for a number of invalid argument conditions.

Notes:

After processing the selections, the selection validation block is called if one was defined for the constructor.

# File lib/option_list.rb, line 71
def select(selections=[])
  selections = [selections] unless selections.is_a?(Array)
  selected = process_selections(selections)

  @mandatory.each do |cat|
    error "Missing mandatory setting #{cat}" unless selected[cat]
  end

  @select_block.call(selected) if @select_block
  selected
end
version() click to toggle source

The option list code version. This is a redirect to the class method.

# File lib/option_list.rb, line 25
def version
  OptionList::VERSION
end