Module: JSONAPI::Serializer::InstanceMethods

Defined in:
lib/jsonapi-serializers/serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject

Returns the value of attribute context.



26
27
28
# File 'lib/jsonapi-serializers/serializer.rb', line 26

def context
  @context
end

#objectObject

Returns the value of attribute object.



25
26
27
# File 'lib/jsonapi-serializers/serializer.rb', line 25

def object
  @object
end

Instance Method Details

#attributesObject



137
138
139
140
141
142
143
144
145
# File 'lib/jsonapi-serializers/serializer.rb', line 137

def attributes
  attributes = {}
  self.class.attributes_map.each do |attribute_name, attr_data|
    next if !should_include_attr?(attr_data[:options][:if], attr_data[:options][:unless])
    value = evaluate_attr_or_block(attribute_name, attr_data[:attr_or_block])
    attributes[format_name(attribute_name)] = value
  end
  attributes
end

#format_name(attribute_name) ⇒ Object

Override this to customize how attribute names are formatted. By default, attribute names are dasherized per the spec naming recommendations: jsonapi.org/recommendations/#naming



53
54
55
# File 'lib/jsonapi-serializers/serializer.rb', line 53

def format_name(attribute_name)
  attribute_name.to_s.dasherize
end

#has_many_relationshipsObject



157
158
159
160
161
162
163
164
165
# File 'lib/jsonapi-serializers/serializer.rb', line 157

def has_many_relationships
  return {} if self.class.to_many_associations.nil?
  data = {}
  self.class.to_many_associations.each do |attribute_name, attr_data|
    next if !should_include_attr?(attr_data[:options][:if], attr_data[:options][:unless])
    data[attribute_name] = evaluate_attr_or_block(attribute_name, attr_data[:attr_or_block])
  end
  data
end

#has_one_relationshipsObject



147
148
149
150
151
152
153
154
155
# File 'lib/jsonapi-serializers/serializer.rb', line 147

def has_one_relationships
  return {} if self.class.to_one_associations.nil?
  data = {}
  self.class.to_one_associations.each do |attribute_name, attr_data|
    next if !should_include_attr?(attr_data[:options][:if], attr_data[:options][:unless])
    data[attribute_name] = evaluate_attr_or_block(attribute_name, attr_data[:attr_or_block])
  end
  data
end

#idObject

Override this to customize the JSON:API “id” for this object. Always return a string from this method to conform with the JSON:API spec.



38
39
40
# File 'lib/jsonapi-serializers/serializer.rb', line 38

def id
  object.id.to_s
end

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



28
29
30
31
32
33
34
# File 'lib/jsonapi-serializers/serializer.rb', line 28

def initialize(object, options = {})
  @object = object
  @context = options[:context] || {}

  # Internal serializer options, not exposed through attr_accessor. No touchie.
  @_include_linkages = options[:include_linkages] || []
end


79
80
81
82
# File 'lib/jsonapi-serializers/serializer.rb', line 79

def links
  data = {}
  data.merge!({'self' => self_link}) if self_link
end

#metaObject

Override this to provide resource-object metadata. jsonapi.org/format/#document-structure-resource-objects



64
65
# File 'lib/jsonapi-serializers/serializer.rb', line 64

def meta
end


75
76
77
# File 'lib/jsonapi-serializers/serializer.rb', line 75

def relationship_related_link(attribute_name)
  "#{self_link}/#{format_name(attribute_name)}"
end


71
72
73
# File 'lib/jsonapi-serializers/serializer.rb', line 71

def relationship_self_link(attribute_name)
  "#{self_link}/links/#{format_name(attribute_name)}"
end

#relationshipsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jsonapi-serializers/serializer.rb', line 84

def relationships
  data = {}
  # Merge in data for has_one relationships.
  has_one_relationships.each do |attribute_name, object|
    formatted_attribute_name = format_name(attribute_name)
    data[formatted_attribute_name] = {
      'self' => relationship_self_link(attribute_name),
      'related' => relationship_related_link(attribute_name),
    }
    if @_include_linkages.include?(formatted_attribute_name)
      if object.nil?
        # Spec: Resource linkage MUST be represented as one of the following:
        # - null for empty to-one relationships.
        # http://jsonapi.org/format/#document-structure-resource-relationships
        data[formatted_attribute_name].merge!({'linkage' => nil})
      else
        related_object_serializer = JSONAPI::Serializer.find_serializer(object)
        data[formatted_attribute_name].merge!({
          'linkage' => {
            'type' => related_object_serializer.type.to_s,
            'id' => related_object_serializer.id.to_s,
          },
        })
      end
    end
  end

  # Merge in data for has_many relationships.
  has_many_relationships.each do |attribute_name, objects|
    formatted_attribute_name = format_name(attribute_name)
    data[formatted_attribute_name] = {
      'self' => relationship_self_link(attribute_name),
      'related' => relationship_related_link(attribute_name),
    }
    # Spec: Resource linkage MUST be represented as one of the following:
    # - an empty array ([]) for empty to-many relationships.
    # - an array of linkage objects for non-empty to-many relationships.
    # http://jsonapi.org/format/#document-structure-resource-relationships
    if @_include_linkages.include?(formatted_attribute_name)
      data[formatted_attribute_name].merge!({'linkage' => []})
      objects = objects || []
      objects.each do |obj|
        related_object_serializer = JSONAPI::Serializer.find_serializer(obj)
        data[formatted_attribute_name]['linkage'] << {
          'type' => related_object_serializer.type.to_s,
          'id' => related_object_serializer.id.to_s,
        }
      end
    end
  end
  data
end


67
68
69
# File 'lib/jsonapi-serializers/serializer.rb', line 67

def self_link
  "/#{type}/#{id}"
end

#typeObject

Override this to customize the JSON:API “type” for this object. By default, the type is the object’s class name lowercased, pluralized, and dasherized, per the spec naming recommendations: jsonapi.org/recommendations/#naming For example, ‘MyApp::LongCommment’ will become the ‘long-comments’ type.



46
47
48
# File 'lib/jsonapi-serializers/serializer.rb', line 46

def type
  object.class.name.demodulize.tableize.dasherize
end

#unformat_name(attribute_name) ⇒ Object

The opposite of format_name. Override this if you override format_name.



58
59
60
# File 'lib/jsonapi-serializers/serializer.rb', line 58

def unformat_name(attribute_name)
  attribute_name.to_s.underscore
end