Class: Ronin::Core::Params::Types::Enum
- Defined in:
- lib/ronin/core/params/types/enum.rb
Overview
Represents a mapping of Ruby values to their String equivalents.
Instance Attribute Summary collapse
-
#values ⇒ Array<Object>
readonly
The values of the enum.
Class Method Summary collapse
-
.[](*values) ⇒ Enum
Creates a new enum.
Instance Method Summary collapse
-
#coerce(value) ⇒ Symbol
private
Coerces the value into one of the enum values.
-
#initialize(values) ⇒ Enum
constructor
Initializes the enum type.
Constructor Details
#initialize(values) ⇒ Enum
Initializes the enum type.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ronin/core/params/types/enum.rb', line 48 def initialize(values) if values.empty? raise(ArgumentError,"cannot initialize an empty Enum") end @values = values @map = {} values.each do |value| @map[value.to_s] = value end end |
Instance Attribute Details
#values ⇒ Array<Object> (readonly)
The values of the enum.
35 36 37 |
# File 'lib/ronin/core/params/types/enum.rb', line 35 def values @values end |
Class Method Details
.[](*values) ⇒ Enum
Creates a new enum.
72 73 74 |
# File 'lib/ronin/core/params/types/enum.rb', line 72 def self.[](*values) new(values) end |
Instance Method Details
#coerce(value) ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Coerces the value into one of the enum values.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ronin/core/params/types/enum.rb', line 90 def coerce(value) case value when ::Symbol unless @values.include?(value) raise(ValidationError,"unknown value (#{value.inspect})") end value when ::String @map.fetch(value) do raise(ValidationError,"unknown value (#{value.inspect})") end else raise(ValidationError,"value must be either a Symbol or a String (#{value.inspect})") end end |