Class: Ame::Types::Enumeration
- Inherits:
-
Object
- Object
- Ame::Types::Enumeration
- 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.
Instance Attribute Summary collapse
-
#default ⇒ Symbol
readonly
The Symbol to use if no argument has been given.
Instance Method Summary collapse
-
#initialize(first, second, *rest) ⇒ Enumeration
constructor
Creates an Enumeration of valid Symbols FIRST, SECOND, and REST for an argument and sets #default to FIRST.
-
#parse(argument) ⇒ Symbol
The result of ARGUMENT#to_sym.
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.
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
#default ⇒ Symbol (readonly)
Returns 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.
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 |