Sha256: 5af689ea909c016f4c6b096dea26b2ec1d60f83e57dad0982a32a3deea172fa3

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

# encoding: UTF-8
#
# A class to model a Java-like Enum.
# To create an Enum extend this class and override :allowed_values method with allowed enum values.
#
# @author:: Nayyara Samuel (mailto: nayyara.samuel@opower.com)
#
module MockServer::Model
  # Enum generic class
  class Enum
    # Create an instance of the enum from the value supplied
    # @param supplied_value [Object] value used to create instance of an enum
    # @raise [Exception] if the supplied value is not valid for this enum
    def initialize(supplied_value)
      supplied_value = pre_process_value(supplied_value)
      fail "Supplied value: #{supplied_value} is not valid. Allowed values are: #{supplied_value}" unless allowed_values.include?(supplied_value)
      @value = supplied_value
    end

    # @return [Array] a list of values allowed by this enum
    def allowed_values
      fail 'Override :allowed_values in Enum class'
    end

    # A pre-process hook for a value before it is stored
    # @param value [Object] a value used to instantiate the enum
    # @return [Object] the processed value. By default, a no-op implementation.
    def pre_process_value(value)
      value
    end

    # Override this for JSON representation
    def to_s
      @value.to_s
    end
  end

  # Subclass of Enum that has a list of symbols as allowed values.
  class SymbolizedEnum < Enum
    # Pre-process the value passed in and convert to a symbol
    # @param value [Object] a value used to instantiate the enum
    # @return [Symbol] a symbolized version of the value passed in (first calls to_s)
    def pre_process_value(value)
      value.to_s.to_sym
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mockserver-client-1.0.4 lib/mockserver/model/enum.rb
mockserver-client-1.0.3 lib/mockserver/model/enum.rb
mockserver-client-1.0.2.pre lib/mockserver/model/enum.rb
mockserver-client-1.0.1.pre lib/mockserver/model/enum.rb
mockserver-client-1.0.1 lib/mockserver/model/enum.rb
mockserver-client-1.0.0 lib/mockserver/model/enum.rb
mockserver-client-1.0.0.pre lib/mockserver/model/enum.rb
mockserver-client-0.0.3 lib/mockserver/model/enum.rb
mockserver-client-0.0.1 lib/mockserver/model/enum.rb