Sha256: 9a4a1e1ad1ad202d9f991d4d5d7ec7e9ac0fdd8d9dd40a9ddbbfcaaa02460380

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

require 'typesafe_enum'

require 'leeroy/helpers/logging'

module Leeroy
  module Types
    ##
    # Implements the [Typesafe Enum
    # pattern](http://www.oracle.com/technetwork/java/page1-139488.html#replaceenums).
    # Implement your own classes that inherit from this.
    #
    # Valid values for these enums must be Strings or otherwise stringifiable.
    #
    # Usage:
    # ```
    # class EnumClass < Leeroy::Types::Enum
    #   # 'foo' and 'bar' are the only acceptable values for EnumClass
    #   new :FOO
    #   new :BAR
    # end
    #
    # enum_valid = Leeroy::Types::EnumClass.resolve('foo')
    # # returns a valid instance of Leeroy::Types::EnumClass::FOO
    # enum_valid.to_s
    # # returns 'foo'
    # enum_invalid = Leeroy::Types::EnumClass.resolve('baz')
    # # raises an exception
    # ```
    #
    class Enum < TypesafeEnum::Base

      def to_s
        self.value
      end

      ##
      # Given a string or something that can be stringified, returns a subclass
      # of the parent enum with a value matching the provided string.
      #
      # Accepts an optional second argument; if the first argument cannot be
      # resolved by the enum, the second argument will be resolved.
      #
      def self.resolve(candidate, alternate = nil)
        resolved = candidate.kind_of?(Leeroy::Types::Enum) ? candidate : self.from_s(candidate)

        if candidate.nil?
          resolved = self.resolve(alternate, nil) unless alternate.nil?
        end

        # FIXME raise some more appropriate type of error
        raise "invalid value for enum" if resolved.nil?

        resolved
      end

      def resolve(candidate, alternate = nil)
        self.resolve(candidate, alternate)
      end

      def self.from_s(x)
        self.find_by_value_str(x.to_s)
      end

      def from_s(x)
        self.from_s(x)
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
leeroy_app-0.5.4 lib/leeroy/types/enum.rb
leeroy_app-0.4.0 lib/leeroy/types/enum.rb
leeroy_app-0.3.1 lib/leeroy/types/enum.rb
leeroy_app-0.3.0 lib/leeroy/types/enum.rb