Sha256: 01f619a52d15d67390ceacd80bda7aea18c3deb9ba3ee12d2887c9467280a7d1

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

require 'command_mapper/types/type'

module CommandMapper
  module Types
    class Map < Type

      # @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

2 entries across 2 versions & 1 rubygems

Version Path
command_mapper-0.1.2 lib/command_mapper/types/map.rb
command_mapper-0.1.1 lib/command_mapper/types/map.rb