Sha256: 298967fc6950771b373962c3077ff7cff862c95b6f1e9ac3c4ad7ff2fe16bff4

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require "active_model"

module StoreModel
  module Types
    # Implements ActiveModel::Type::Value type for handling Rails-like enums
    class EnumType < ActiveModel::Type::Value
      # Initializes type for mapping
      #
      # @param mapping [Hash] mapping for enum values
      #
      # @return [StoreModel::Types::EnumType]
      def initialize(mapping, raise_on_invalid_values)
        @mapping = mapping
        @raise_on_invalid_values = raise_on_invalid_values
        super()
      end

      # Returns type
      #
      # @return [Symbol]
      def type
        :integer
      end

      # Casts +value+ from DB or user to StoreModel::Model instance
      #
      # @param value [Object] a value to cast
      #
      # @return StoreModel::Model
      def cast_value(value)
        return if value.blank?

        case value
        when String, Symbol then cast_symbol_value(value)
        when Integer then cast_integer_value(value)
        else
          raise StoreModel::Types::CastError,
                "failed casting #{value.inspect}, only String, Symbol or " \
                "Integer instances are allowed"
        end
      end

      private

      def cast_symbol_value(value)
        raise_invalid_value!(value) if @raise_on_invalid_values && !@mapping.key?(value.to_sym)
        @mapping[value.to_sym] || value
      end

      def cast_integer_value(value)
        raise_invalid_value!(value) if @raise_on_invalid_values && !@mapping.value?(value)
        value
      end

      def raise_invalid_value!(value)
        raise ArgumentError, "invalid value '#{value}' is assigned"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
store_model-4.0.0 lib/store_model/types/enum_type.rb