class Opto::Types::Enum

A list of possible values

:options - a list of possible values for this enum :can_be_other - set to true if the value can be outside of the value list in options :in - when “can be other” is defined, this can be used to define an extra set of possible values

@example Shorthand option list

Opto::Option.new(
  name: 'foo',
  type: 'enum',
  options:
    - foo
    - bar
    - cat
  can_be_other: true
)

@example Detailed option list

Opto::Option.new(
  name: 'foo',
  type: 'enum',
  options:
    - value: cat
      label: Cat
      description: A four legged ball of fur
    - value: dog
      label: Dog
      description: A friendly furry creature with a tail, says 'woof'
)

Public Class Methods

new(options={}) click to toggle source
Calls superclass method Opto::Type::new
# File lib/opto/types/enum.rb, line 49
def initialize(options={})
  opts = normalize_opts(options.delete(:options))
  super(options)
  @options[:options] = opts
end

Public Instance Methods

normalize_opts(options) click to toggle source
# File lib/opto/types/enum.rb, line 72
def normalize_opts(options)
  case options
  when Hash
    options.each_with_object([]) do |(key, value), array|
      array << { value: key, label: key, description: value }
    end
  when ::Array
    case options.first
    when Hash
      options.each do |opt|
        if opt[:value].nil? || opt[:description].nil?
          raise TypeError, "Option definition requires value and description and can have label when using hash syntax"
        end
      end
      options
    when ::String, ( 0.class == Integer ? Integer : Fixnum )
      options.map do |opt|
        { value: opt, description: opt, label: opt }
      end
    when NilClass
      []
    else
      raise TypeError, "Invalid format for enum option list definition"
    end
  when NilClass
    []
  else
    raise TypeError, "Invalid format for enum option list definition"
  end
end