class Object
Public Instance Methods
Add query method for the selected category.
# File lib/option_list.rb, line 201 def add_option_reader(name) duplicate_entry_check(@default, name, 'category') @default.define_singleton_method(name) { self[name] } end
Add a query method (eg: has_stuff? ) for the selected option.
# File lib/option_list.rb, line 207 def add_option_tester(target, value) qry = (value.to_s + '?').to_sym @default.define_singleton_method(qry) { self[target] == value} end
Process an array spec that lists all the valid values for an option. See the new method for more information on these specs.
# File lib/option_list.rb, line 109 def array_spec(spec) category, default, spec_len = spec[0], spec[1], spec.length error "Invalid number of entries for #{category}." unless spec_len > 2 add_option_reader(category) array_spec_default(category, default) array_spec_tail_rest(category, spec[2...spec_len]) end
Process the first element of the array spec tail.
# File lib/option_list.rb, line 118 def array_spec_default(category, opt) opt && array_spec_single(category, opt) @default[category] = opt @mandatory << category if opt == false end
Process a single array spec option
# File lib/option_list.rb, line 136 def array_spec_single(category, opt) duplicate_entry_check(@categories, opt, 'option') @categories[opt] = category add_option_tester(category, opt) end
Process the rest of the array spec tail.
# File lib/option_list.rb, line 125 def array_spec_tail_rest(category, spec_tail_rest) spec_tail_rest.each do |opt| if opt array_spec_single(category, opt) else error "The values nil/false are only allowed as the default option." end end end
Flag any duplicate entry errors.
# File lib/option_list.rb, line 213 def duplicate_entry_check(target, entry, detail) error "Found #{entry.class}, expected Symbol." unless entry.is_a?(Symbol) error "Duplicate #{detail}: #{entry}" if target.has_key?(entry) end
Fail with an argument error.
# File lib/option_list.rb, line 224 def error(messsage) fail(ArgumentError, messsage, caller) end
Add to set with no duplicates allowed.
# File lib/option_list.rb, line 196 def hash_option_dup_check(category, dup) error "Category #{category} has multiple values." unless dup.add?(category) end
Validate a hash option value.
# File lib/option_list.rb, line 188 def hash_option_value_check(value_category, value) if (@categories[value_category] != value_entry) && value error "Found #{value.class}, expected Symbol." unless value.is_a?(Symbol) error "Invalid option: #{value}." unless @categories[value] == value_category end end
Process a hash of option selection values.
Parameters:¶ ↑
-
options - a hash of options to process.
-
selected - a hash of selected data.
-
dup - a set of categories that have been set. Used to detect duplicates.
# File lib/option_list.rb, line 177 def hash_selections(hash_options, selected, dup) hash_options.each do |category, value| missing_entry_check(@default, category, 'category') hash_option_value_check(category, value) hash_option_dup_check(category, dup) selected[category] = value end end
Process a hash spec that lists only the default value for an option. See the new method for more information on these specs.
# File lib/option_list.rb, line 144 def hash_spec(spec) error "Hash contains no specs." unless spec.length > 0 spec.each do |category, value| add_option_reader(category) set_default_option_value(category, value) @mandatory << category if value == false end end
Flag any missing entry errors.
# File lib/option_list.rb, line 219 def missing_entry_check(target, entry, detail) error "Unknown #{detail}: #{entry}" unless target.has_key?(entry) end
Set the default value of a value entry.
# File lib/option_list.rb, line 155 def set_default_option_value(category, value) @categories[category] = value_entry @default[category] = value end
Process a symbolic option selection.
Parameters:¶ ↑
-
option - a symbol to process.
-
selected - a hash of selected data.
-
dup - a set of categories that have been set. Used to detect duplicates.
# File lib/option_list.rb, line 165 def symbolic_selection(symbol_option, selected, dup) missing_entry_check(@categories, symbol_option, 'option') category = @categories[symbol_option] hash_option_dup_check(category, dup) selected[category] = symbol_option end
Return a internal category marker constant for value entries.
# File lib/option_list.rb, line 103 def value_entry 'A value entry.' end