Class: WolfCore::ApplicationSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/wolf_core/infrastructure/application_serializer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ApplicationSerializer.



7
8
9
10
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 7

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

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 5

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 5

def options
  @options
end

Class Method Details

._attributesObject



32
33
34
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 32

def self._attributes
  @_attributes ||= []
end

._relationshipsObject



36
37
38
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 36

def self._relationships
  @_relationships ||= {}
end

.attributes(*attrs) ⇒ Object



20
21
22
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 20

def self.attributes(*attrs)
  _attributes.concat(attrs.map(&:to_sym))
end

.has_many(relationship, serializer:) ⇒ Object

rubocop:disable Naming/PredicateName



24
25
26
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 24

def self.has_many(relationship, serializer:) # rubocop:disable Naming/PredicateName
  _relationships[relationship.to_sym] = serializer
end

.has_one(relationship, serializer:) ⇒ Object

rubocop:disable Naming/PredicateName



28
29
30
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 28

def self.has_one(relationship, serializer:) # rubocop:disable Naming/PredicateName
  _relationships[relationship.to_sym] = serializer
end

.serialize(entity, options = {}) ⇒ Object



16
17
18
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 16

def self.serialize(entity, options = {})
  new(entity, options).as_json.with_indifferent_access
end

.serialize_all(collection, options = {}) ⇒ Object



12
13
14
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 12

def self.serialize_all(collection, options = {})
  collection.map { |item| serialize(item, options) }
end

Instance Method Details

#as_jsonObject



40
41
42
43
44
# File 'lib/wolf_core/infrastructure/application_serializer.rb', line 40

def as_json(*)
  attributes = serialized_attributes
  relationships = serialized_relationships
  attributes.merge(relationships)
end