Sha256: a0ff815a770142019e9f5d31045dd2a6ad1b18f917a77d4717d0bc5e3b0a2d00
Contents?: true
Size: 1.85 KB
Versions: 1
Compression:
Stored size: 1.85 KB
Contents
require 'command_mapper/types/type' module CommandMapper module Types class Map < Type # The map of values to Strings. # # @return [Hash{Object => String}] attr_reader :map # # Initializes the map value type. # # @param [Hash{Object => String}] map # The map of values to Strings. # def initialize(map) @map = map end # # Creates a new map. # # @param [Hash{Object => String}] map # The map of values to Strings. # # @return [Map] # def self.[](map) new(map) end # Maps boolean values to "yes" and "no" YesNo = new(true => 'yes', false => 'no') # Maps boolean values to "enabled" and "disabled" EnabledDisabled = new(true => 'enabled', false => 'disabled') # # Validates a value. # # @param [Object] value # The given value to validate. # # @return [true, (false, String)] # Returns true if the value is valid, or `false` and a validation error # message if the value is not compatible. # def validate(value) unless (@map.has_key?(value) || @map.has_value?(value)) return [false, "unknown value (#{value.inspect})"] end return true end # # Maps a value. # # @param [Object] value # The given value. # # @return [String] # The mapped value. # # @raise [KeyError] # The given value is not a key or value in the map. # 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 end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
command_mapper-0.2.0 | lib/command_mapper/types/map.rb |