Class: Interfacets::Server::Facets::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/interfacets/server/facets/serializer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(facet, mount_point) ⇒ Serializer

Returns a new instance of Serializer.



12
13
14
15
# File 'lib/interfacets/server/facets/serializer.rb', line 12

def initialize(facet, mount_point)
  @facet = facet
  @mount_point = mount_point
end

Instance Attribute Details

#facet ⇒ Object (readonly)

Returns the value of attribute facet.



11
12
13
# File 'lib/interfacets/server/facets/serializer.rb', line 11

def facet
  @facet
end

#mount_point ⇒ Object (readonly)

Returns the value of attribute mount_point.



11
12
13
# File 'lib/interfacets/server/facets/serializer.rb', line 11

def mount_point
  @mount_point
end

Class Method Details

.call ⇒ Object



7
8
9
# File 'lib/interfacets/server/facets/serializer.rb', line 7

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

Instance Method Details

#attributes(entity) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/interfacets/server/facets/serializer.rb', line 25

def attributes(entity)
  entity
    .attribute_specs
    .each_with_object({}) { |(name, _attribute), json|
      # refactor this, shouldn't be attributes...
      # next if attribute.is_a?(Facets::Attributes::ClientEvent)
      # next if attribute.is_a?(Facets::Attributes::ServerEvent)
      # next if attribute.is_a?(Facets::Attributes::Inherit)

      value = entity.public_send(name)

      json[name] = (
        if value.is_a?(Array)
          value.map { _1.is_a?(Entity) || _1.is_a?(Facet::Handler) ? attributes(_1) : _1 }
        elsif value.is_a?(Entity) || value.is_a?(Facet::Handler)
          attributes(value)
        else
          value
        end
      )
    }
end

#call ⇒ Object



17
18
19
20
21
22
23
# File 'lib/interfacets/server/facets/serializer.rb', line 17

def call
  {
    id: facet.entity.id,
    facet_class: facet.class.name,
    attributes: facet.entity.serialize,
  }
end