Sha256: 0947704d92781269b3b33220578c67f419fea544577d2bf49c32979c21c92aea

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 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
      #
      # @return [true, (false, String)]
      #
      def validate(value)
        unless @map.has_key?(value)
          return [false, "unknown value (#{value.inspect})"]
        end

        return true
      end

      #
      # @param [Object] value
      #
      # @return [String]
      #
      def format(value)
        super(@map.fetch(value))
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
command_mapper-0.1.0 lib/command_mapper/types/map.rb
command_mapper-0.1.0.pre1 lib/command_mapper/types/map.rb