Class: OptionList
- Inherits:
-
Object
- Object
- OptionList
- Defined in:
- lib/option_list.rb,
lib/option_list/version.rb
Overview
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.
Constant Summary collapse
- VERSION =
'1.1.5'
Class Method Summary collapse
-
.version ⇒ Object
The option list code version.
Instance Method Summary collapse
-
#initialize(*option_specs, &select_block) ⇒ OptionList
constructor
Create an option list from an array of option specifications.
-
#select(selections = []) ⇒ Object
From the possible options, select the actual, in force, options and return an access object for use in the code.
-
#version ⇒ Object
The option list code version.
Constructor Details
#initialize(*option_specs, &select_block) ⇒ OptionList
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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 |
Class Method Details
.version ⇒ Object
The option list code version.
20 21 22 |
# File 'lib/option_list.rb', line 20 def self.version OptionList::VERSION end |
Instance Method Details
#select(selections = []) ⇒ Object
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.
71 72 73 74 75 76 77 78 79 80 81 |
# 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 ⇒ Object
The option list code version. This is a redirect to the class method.
25 26 27 |
# File 'lib/option_list.rb', line 25 def version OptionList::VERSION end |