Sha256: 78c63af0955261521ecc131c4bf4c84994408acd0e3346ab565838ef681bdaf0

Contents?: true

Size: 616 Bytes

Versions: 6

Compression:

Stored size: 616 Bytes

Contents

# frozen_string_literal: true

module EnumIsh
  class ActiveRecordEnumType < ActiveRecord::Type::Value
    def initialize(name, mapping, subtype)
      @name = name
      @mapping = mapping
      @subtype = subtype
    end

    def cast(value)
      return if value.nil?
      if @mapping.has_key?(value.to_s.to_sym)
        value.to_s.to_sym
      elsif @mapping.has_value?(value)
        @mapping.key(value)
      else
        value
      end
    end

    def deserialize(value)
      @mapping.key(@subtype.deserialize(value))
    end

    def serialize(value)
      @mapping.fetch(value, value)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
enum_ish-1.6.3 lib/enum_ish/active_record_enum_type.rb
enum_ish-1.6.2 lib/enum_ish/active_record_enum_type.rb
enum_ish-1.6.1 lib/enum_ish/active_record_enum_type.rb
enum_ish-1.6.0 lib/enum_ish/active_record_enum_type.rb
enum_ish-1.5.1 lib/enum_ish/active_record_enum_type.rb
enum_ish-1.5.0 lib/enum_ish/active_record_enum_type.rb