Class: Agilibox::MiniModelSerializer::Serialize

Inherits:
Object
  • Object
show all
Defined in:
app/libs/agilibox/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.



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

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/agilibox/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/agilibox/mini_model_serializer/serialize.rb', line 2

def options
  @options
end

Class Method Details

.call(*args) ⇒ Object



21
22
23
# File 'app/libs/agilibox/mini_model_serializer/serialize.rb', line 21

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

Instance Method Details

#callObject



9
10
11
12
13
14
15
16
17
18
19
# File 'app/libs/agilibox/mini_model_serializer/serialize.rb', line 9

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