Sha256: a8ba99750be5365b1acd47a3d0c8f4f3b23c729716bc2bb850009a32ff7ae116

Contents?: true

Size: 1.64 KB

Versions: 14

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true
require 'msgpack'

module Dynflow
  module Extensions
    module MsgPack
      module Time
        def to_msgpack(out = ''.dup)
          ::MessagePack.pack(self, out)
          out
        end
      end

      ::Time.include ::Dynflow::Extensions::MsgPack::Time
      ::MessagePack::DefaultFactory.register_type(0x00, Time, packer: MessagePack::Time::Packer, unpacker: MessagePack::Time::Unpacker)

      begin
        require 'active_support/time_with_zone'
        unpacker = ->(payload) do
          tv = MessagePack::Timestamp.from_msgpack_ext(payload)
          ::Time.zone.at(tv.sec, tv.nsec, :nanosecond)
        end
        ::ActiveSupport::TimeWithZone.include ::Dynflow::Extensions::MsgPack::Time
        ::MessagePack::DefaultFactory.register_type(0x01, ActiveSupport::TimeWithZone, packer: MessagePack::Time::Packer, unpacker: unpacker)

        ::DateTime.include ::Dynflow::Extensions::MsgPack::Time
        ::MessagePack::DefaultFactory.register_type(0x02, DateTime,
                                                    packer: ->(datetime) { MessagePack::Time::Packer.(datetime.to_time) },
                                                    unpacker: ->(payload) { unpacker.(payload).to_datetime })

        ::Date.include ::Dynflow::Extensions::MsgPack::Time
        ::MessagePack::DefaultFactory.register_type(0x03, Date,
                                                    packer: ->(date) { MessagePack::Time::Packer.(date.to_time) },
                                                    unpacker: ->(payload) { unpacker.(payload).to_date })
      rescue LoadError
        # This is fine
        nil
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
dynflow-1.8.2 lib/dynflow/extensions/msgpack.rb
dynflow-1.8.1 lib/dynflow/extensions/msgpack.rb
dynflow-1.8.0 lib/dynflow/extensions/msgpack.rb
dynflow-1.7.0 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.11 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.10 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.8 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.7 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.6 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.5 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.4 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.3 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.2 lib/dynflow/extensions/msgpack.rb
dynflow-1.6.1 lib/dynflow/extensions/msgpack.rb