Class: Ame::Types::Enumeration

Inherits:
Object
  • Object
show all
Defined in:
lib/ame-1.0/types/enumeration.rb

Overview

Enumeration type for limiting a Symbol argument to one in a fixed set. It also has a #default, which will be the first symbol passed to its constructor.

Examples:

Using an Enumeration

class Git::CLI::Git::FormatPatch < Ame::Class
  switch '', 'thread', 'STYLE', nil,
    Ame::Types::Enumeration[:shallow, :deep],
    'Controls addition of In-Reply-To and References headers'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, second, *rest) ⇒ Enumeration

Creates an Enumeration of valid Symbols FIRST, SECOND, and REST for an argument and sets #default to FIRST.

Parameters:

  • first (#to_sym)
  • second (#to_sym)
  • rest (#to_sym, )


22
23
24
25
# File 'lib/ame-1.0/types/enumeration.rb', line 22

def initialize(first, second, *rest)
  @default = first
  @names = ([first, second] + rest).map(&:to_sym)
end

Instance Attribute Details

#defaultSymbol (readonly)

Returns The Symbol to use if no argument has been given.

Returns:

  • (Symbol)

    The Symbol to use if no argument has been given



39
40
41
# File 'lib/ame-1.0/types/enumeration.rb', line 39

def default
  @default
end

Instance Method Details

#parse(argument) ⇒ Symbol

Returns The result of ARGUMENT#to_sym.

Parameters:

Returns:

  • (Symbol)

    The result of ARGUMENT#to_sym

Raises:

  • (MalformedArgument)

    If ARGUMENT#to_sym isn’t included among the valid Symbols



32
33
34
35
36
# File 'lib/ame-1.0/types/enumeration.rb', line 32

def parse(argument)
  @names.include?(s = argument.to_sym) ? s :
    raise(Ame::MalformedArgument, 'must be one of %s, not %s' %
          [@names.join(', '), argument])
end