Sha256: 29ceb831812525964cba11625c86a98c6251966c49a520b49e0205ac03529941

Contents?: true

Size: 861 Bytes

Versions: 21

Compression:

Stored size: 861 Bytes

Contents

# frozen_string_literal: true

module Eventsimple
  class MetadataType < ActiveModel::Type::Value
    def type
      :metadata_type
    end

    def cast_value(value)
      case value
      when String
        decoded = ActiveSupport::JSON.decode(value)
        return decoded if decoded.empty?

        Eventsimple.configuration.metadata_klass.new(decoded)
      when Hash
        Eventsimple.configuration.metadata_klass.new(value)
      when Eventsimple.configuration.metadata_klass
        value
      end
    end

    def serialize(value)
      case value
      when Hash, Eventsimple.configuration.metadata_klass
        ActiveSupport::JSON.encode(value)
      else
        super
      end
    end

    def deserialize(value)
      decoded = ActiveSupport::JSON.decode(value)

      Eventsimple.configuration.metadata_klass.new(decoded)
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
eventsimple-1.0.0 lib/eventsimple/metadata_type.rb