Sha256: 2921dc24c6d884eb87395e149231330551d52ba593388f61d8aa3887510ee1e6

Contents?: true

Size: 1.89 KB

Versions: 25

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true
require 'algebrick/serializer'

module Dynflow
  def self.serializer
    @serializer ||= Serializer.new
  end

  class Serializer < Algebrick::Serializer

    ARBITRARY_TYPE_KEY = :class
    MARSHAL_KEY        = :marshaled

    def load(data, options = {})
      case data
      when ::Array
        data.collect { |v| load(v) }
      else
        super
      end
    end

    def dump(object, options = {})
      case object
      when ::Array
        object.collect { |v| dump(v) }
      else
        super
      end
    end

    protected

    def parse_other(other, options = {})
      if Hash === other
        if (marshal_value = other[MARSHAL_KEY] || other[MARSHAL_KEY.to_s])
          return Marshal.load(Base64.strict_decode64(marshal_value))
        end

        if (type_name = other[ARBITRARY_TYPE_KEY] || other[ARBITRARY_TYPE_KEY.to_s])
          if type_name == 'Time' && ( time_str = other['value'] )
            return Serializable.send(:string_to_time, time_str)
          end
          type = Utils.constantize(type_name) rescue nil
          if type && type.respond_to?(:from_hash)
            return type.from_hash other
          end
        end
      end

      return other
    end

    def generate_other(object, options = {})
      hash = case
             when object.respond_to?(:to_h)
               object.to_h
             when object.respond_to?(:to_hash)
               object.to_hash
             when object.is_a?(Time) && !options[:marshaled_time]
               { ARBITRARY_TYPE_KEY => 'Time', 'value' => object.utc.strftime(Serializable::TIME_FORMAT) }
             else
               { ARBITRARY_TYPE_KEY => object.class.to_s,
                 MARSHAL_KEY        => Base64.strict_encode64(Marshal.dump(object)) }
             end
      raise "Missing #{ARBITRARY_TYPE_KEY} key in #{hash.inspect}" unless hash.key?(ARBITRARY_TYPE_KEY)
      hash
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
dynflow-1.8.2 lib/dynflow/serializer.rb
dynflow-1.8.1 lib/dynflow/serializer.rb
dynflow-1.8.0 lib/dynflow/serializer.rb
dynflow-1.7.0 lib/dynflow/serializer.rb
dynflow-1.6.11 lib/dynflow/serializer.rb
dynflow-1.6.10 lib/dynflow/serializer.rb
dynflow-1.6.8 lib/dynflow/serializer.rb
dynflow-1.6.7 lib/dynflow/serializer.rb
dynflow-1.6.6 lib/dynflow/serializer.rb
dynflow-1.6.5 lib/dynflow/serializer.rb
dynflow-1.6.4 lib/dynflow/serializer.rb
dynflow-1.6.3 lib/dynflow/serializer.rb
dynflow-1.6.2 lib/dynflow/serializer.rb
dynflow-1.6.1 lib/dynflow/serializer.rb
dynflow-1.4.9 lib/dynflow/serializer.rb
dynflow-1.4.8 lib/dynflow/serializer.rb
dynflow-1.5.0 lib/dynflow/serializer.rb
dynflow-1.4.7 lib/dynflow/serializer.rb
dynflow-1.4.6 lib/dynflow/serializer.rb
dynflow-1.4.5 lib/dynflow/serializer.rb