Sha256: f1b7d9737124e236a235203839539aa03ff42f49409336901521b5190b423a40

Contents?: true

Size: 639 Bytes

Versions: 8

Compression:

Stored size: 639 Bytes

Contents

class BMC::MiniModelSerializer::Serialize
  attr_reader :object, :options

  def initialize(object, options = {})
    @object  = object
    @options = options
  end

  def call
    if object.is_a?(Hash)
      object.map { |k, v| [k.to_s, serialize(v)] }.to_h
    elsif object.is_a?(Enumerable)
      object.map { |e| serialize(e) }
    elsif (serializer = "#{object.class}Serializer".safe_constantize)
      serializer.call(object, options)
    else
      object.as_json
    end
  end

  def self.call(...)
    new(...).call
  end

  private

  def serialize(object)
    BMC::MiniModelSerializer::Serialize.call(object, options)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bmc-1.3.2 app/libs/bmc/mini_model_serializer/serialize.rb
bmc-1.3.1 app/libs/bmc/mini_model_serializer/serialize.rb
bmc-1.3.0 app/libs/bmc/mini_model_serializer/serialize.rb
bmc-1.2.1 app/libs/bmc/mini_model_serializer/serialize.rb
bmc-1.2.0 app/libs/bmc/mini_model_serializer/serialize.rb
bmc-1.1.0 app/libs/bmc/mini_model_serializer/serialize.rb
bmc-1.0.1 app/libs/bmc/mini_model_serializer/serialize.rb
bmc-1.0.0 app/libs/bmc/mini_model_serializer/serialize.rb