Class: SimpleJsonApi::Serializer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, DSL
Defined in:
lib/simple_json_api/serializer.rb

Overview

The Serializer will serialize a model

Direct Known Subclasses

ArraySerializer, ResourceSerializer

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from DSL

#_associations, #_attributes, #_default_fields, #_required_associations, #_root_name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

attribute, belongs_to, default_attributes, has_many, has_one, inherited, required_associations, serializes

Constructor Details

#initialize(object, builder, each_serializer = nil, base = nil) ⇒ Serializer

Returns a new instance of Serializer.



15
16
17
18
19
20
# File 'lib/simple_json_api/serializer.rb', line 15

def initialize(object, builder, each_serializer = nil, base = nil)
  @_object = object
  @_builder = builder
  @_each_serializer = each_serializer
  @_base = base
end

Class Attribute Details

.registered_serializersObject (readonly)

Returns the value of attribute registered_serializers.



51
52
53
# File 'lib/simple_json_api/serializer.rb', line 51

def registered_serializers
  @registered_serializers
end

Instance Attribute Details

#_builderObject (readonly)

Returns the value of attribute _builder.



11
12
13
# File 'lib/simple_json_api/serializer.rb', line 11

def _builder
  @_builder
end

#_each_serializerObject (readonly)

Returns the value of attribute _each_serializer.



12
13
14
# File 'lib/simple_json_api/serializer.rb', line 12

def _each_serializer
  @_each_serializer
end

#_objectObject (readonly)

Returns the value of attribute _object.



13
14
15
# File 'lib/simple_json_api/serializer.rb', line 13

def _object
  @_object
end

Class Method Details

.for(model, association) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/simple_json_api/serializer.rb', line 59

def for(model, association)
  return association.serializer if association.serializer.present?
  if association[:polymorphic]
    for_polymorphic(model)
  else
    for_regular(association)
  end
end

.for_polymorphic(model) ⇒ Object



81
82
83
84
85
86
# File 'lib/simple_json_api/serializer.rb', line 81

def for_polymorphic(model)
  serializer = @registered_serializers.find do |entry|
    entry[:model] == model.class
  end
  serializer[:serializer] if serializer
end

.for_regular(association) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/simple_json_api/serializer.rb', line 73

def for_regular(association)
  resource = association[:name].to_s.pluralize.to_sym
  serializer = @registered_serializers.find do |entry|
    entry[:resource] == resource
  end
  serializer[:serializer] if serializer
end

.includes(association) ⇒ Object



68
69
70
71
# File 'lib/simple_json_api/serializer.rb', line 68

def includes(association)
  serializer = Serializer.for_regular(association)
  serializer._associations.reject(&:polymorphic).map { |a| a[:name] } if serializer
end

.register_serializer(serializer_hash, options) ⇒ Object



53
54
55
56
57
# File 'lib/simple_json_api/serializer.rb', line 53

def register_serializer(serializer_hash, options)
  options.reverse_merge!(primary_key: :id)
  @registered_serializers ||= []
  @registered_serializers << serializer_hash.merge(options)
end

Instance Method Details

#associated_object(association_name) ⇒ Object



22
23
24
# File 'lib/simple_json_api/serializer.rb', line 22

def associated_object(association_name)
  send(association_name)
end