Class: BMC::MiniModelSerializer::Serialize

Inherits:
Object
  • Object
show all
Defined in:
app/libs/bmc/mini_model_serializer/serialize.rb

Direct Known Subclasses

Serializer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Serialize

Returns a new instance of Serialize.



8
9
10
11
# File 'app/libs/bmc/mini_model_serializer/serialize.rb', line 8

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

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



2
3
4
# File 'app/libs/bmc/mini_model_serializer/serialize.rb', line 2

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'app/libs/bmc/mini_model_serializer/serialize.rb', line 2

def options
  @options
end

Class Method Details

.callObject



4
5
6
# File 'app/libs/bmc/mini_model_serializer/serialize.rb', line 4

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

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
# File 'app/libs/bmc/mini_model_serializer/serialize.rb', line 13

def call
  if object.is_a?(Hash)
    object.to_h { |k, v| [k.to_s, serialize(v)] }
  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