Class: CommandMapper::Types::Map
- Defined in:
- lib/command_mapper/types/map.rb
Overview
Represents a mapping of Ruby values to other String values.
Direct Known Subclasses
Constant Summary collapse
- YesNo =
Maps boolean values to "yes" and "no"
new(true => 'yes', false => 'no')
- EnabledDisabled =
Maps boolean values to "enabled" and "disabled"
new(true => 'enabled', false => 'disabled')
Instance Attribute Summary collapse
-
#map ⇒ Hash{Object => String}
readonly
The map of values to Strings.
Class Method Summary collapse
-
.[](map) ⇒ Map
Creates a new map.
Instance Method Summary collapse
-
#format(value) ⇒ String
Maps a value.
-
#initialize(map) ⇒ Map
constructor
Initializes the map value type.
-
#validate(value) ⇒ true, (false, String)
Validates a value.
Constructor Details
#initialize(map) ⇒ Map
Initializes the map value type.
23 24 25 |
# File 'lib/command_mapper/types/map.rb', line 23 def initialize(map) @map = map end |
Instance Attribute Details
#map ⇒ Hash{Object => String} (readonly)
The map of values to Strings.
15 16 17 |
# File 'lib/command_mapper/types/map.rb', line 15 def map @map end |
Class Method Details
.[](map) ⇒ Map
Creates a new map.
35 36 37 |
# File 'lib/command_mapper/types/map.rb', line 35 def self.[](map) new(map) end |
Instance Method Details
#format(value) ⇒ String
Maps a value.
79 80 81 82 83 84 85 86 87 |
# File 'lib/command_mapper/types/map.rb', line 79 def format(value) if @map.has_key?(value) super(@map[value]) elsif @map.has_value?(value) super(value) else raise(KeyError,"value (#{value.inspect}) is not a key or value in the map: #{@map.inspect}") end end |
#validate(value) ⇒ true, (false, String)
Validates a value.
57 58 59 60 61 62 63 |
# File 'lib/command_mapper/types/map.rb', line 57 def validate(value) unless (@map.has_key?(value) || @map.has_value?(value)) return [false, "unknown value (#{value.inspect}) must be #{@map.keys.map(&:inspect).join(', ')}, or #{@map.values.map(&:inspect).join(', ')}"] end return true end |