Class: Graphiti::Serializer

Inherits:
JSONAPI::Serializable::Resource
  • Object
show all
Includes:
Extensions::BooleanAttribute, Extensions::ExtraAttribute, SerializableHash, SerializableTempId
Defined in:
lib/graphiti/serializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SerializableHash

#polymorphic_subclass?, #remote_resource?, #to_hash

Methods included from Extensions::ExtraAttribute

included

Methods included from Extensions::BooleanAttribute

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &blk) ⇒ Object

Allow access to resource methods



56
57
58
59
60
61
62
# File 'lib/graphiti/serializer.rb', line 56

def method_missing(id, *args, &blk)
  if @resource.respond_to?(id, true)
    @resource.send(id, *args, &blk)
  else
    super
  end
end

Class Method Details

.inherited(klass) ⇒ Object



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

def self.inherited(klass)
  super
  klass.class_eval do
    extend JSONAPI::Serializable::Resource::ConditionalFields

    # See #requested_relationships
    def self.relationship(name, options = {}, &block)
      super
      field_condition_blocks.delete(name)
      _register_condition(relationship_condition_blocks, name, options)
    end

    # NB - avoid clobbering includes when sparse fieldset
    # https://github.com/jsonapi-rb/jsonapi-serializable/pull/102
    #
    # We also override this method to ensure attributes and relationships
    # have separate condition blocks. This way an attribute and
    # relationship can have the same name, and the attribute can be
    # conditional without affecting the relationship.
    def requested_relationships(fields)
      @_relationships.select do |k, _|
        _conditionally_included?(self.class.relationship_condition_blocks, k)
      end
    end
  end
end

Instance Method Details

#as_jsonapi(kwargs = {}) ⇒ Object



48
49
50
51
52
53
# File 'lib/graphiti/serializer.rb', line 48

def as_jsonapi(kwargs = {})
  super(**kwargs).tap do |hash|
    strip_relationships!(hash) if strip_relationships?
    add_links!(hash)
  end
end

#respond_to_missing?(method_name, include_private = true) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/graphiti/serializer.rb', line 64

def respond_to_missing?(method_name, include_private = true)
  @resource.respond_to?(method_name, include_private) || super
end