Module: JSONAPI::Serializer::InstanceMethods
- Defined in:
- lib/jsonapi-serializers/serializer.rb
Constant Summary collapse
- @@class_names =
{}
- @@formatted_attribute_names =
{}
- @@unformatted_attribute_names =
{}
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Override this to set a base URL (example.com) for all links.
-
#context ⇒ Object
Returns the value of attribute context.
-
#object ⇒ Object
Returns the value of attribute object.
Instance Method Summary collapse
- #attributes ⇒ Object
-
#format_name(attribute_name) ⇒ Object
Override this to customize how attribute names are formatted.
- #has_many_relationship(attribute_name, attr_data) ⇒ Object
- #has_many_relationships ⇒ Object
- #has_one_relationship(attribute_name, attr_data) ⇒ Object
- #has_one_relationships ⇒ Object
-
#id ⇒ Object
Override this to customize the JSON:API “id” for this object.
- #initialize(object, options = {}) ⇒ Object
-
#jsonapi ⇒ Object
Override this to provide resource-object jsonapi object containing the version in use.
- #links ⇒ Object
-
#meta ⇒ Object
Override this to provide resource-object metadata.
- #relationship_related_link(attribute_name) ⇒ Object
- #relationship_self_link(attribute_name) ⇒ Object
- #relationships ⇒ Object
- #self_link ⇒ Object
-
#type ⇒ Object
Override this to customize the JSON:API “type” for this object.
-
#unformat_name(attribute_name) ⇒ Object
The opposite of format_name.
Instance Attribute Details
#base_url ⇒ Object
Override this to set a base URL (example.com) for all links. No trailing slash.
84 85 86 |
# File 'lib/jsonapi-serializers/serializer.rb', line 84 def base_url @base_url end |
#context ⇒ Object
Returns the value of attribute context.
30 31 32 |
# File 'lib/jsonapi-serializers/serializer.rb', line 30 def context @context end |
#object ⇒ Object
Returns the value of attribute object.
29 30 31 |
# File 'lib/jsonapi-serializers/serializer.rb', line 29 def object @object end |
Instance Method Details
#attributes ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/jsonapi-serializers/serializer.rb', line 172 def attributes return {} if self.class.attributes_map.nil? attributes = {} self.class.attributes_map.each do |attribute_name, attr_data| next if !should_include_attr?(attribute_name, attr_data) 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
62 63 64 65 |
# File 'lib/jsonapi-serializers/serializer.rb', line 62 def format_name(attribute_name) attr_name = attribute_name.to_s @@formatted_attribute_names[attr_name] ||= attr_name.dasherize.freeze end |
#has_many_relationship(attribute_name, attr_data) ⇒ Object
207 208 209 |
# File 'lib/jsonapi-serializers/serializer.rb', line 207 def has_many_relationship(attribute_name, attr_data) evaluate_attr_or_block(attribute_name, attr_data[:attr_or_block]) end |
#has_many_relationships ⇒ Object
197 198 199 200 201 202 203 204 205 |
# File 'lib/jsonapi-serializers/serializer.rb', line 197 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?(attribute_name, attr_data) data[attribute_name] = attr_data end data end |
#has_one_relationship(attribute_name, attr_data) ⇒ Object
193 194 195 |
# File 'lib/jsonapi-serializers/serializer.rb', line 193 def has_one_relationship(attribute_name, attr_data) evaluate_attr_or_block(attribute_name, attr_data[:attr_or_block]) end |
#has_one_relationships ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/jsonapi-serializers/serializer.rb', line 183 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?(attribute_name, attr_data) data[attribute_name] = attr_data end data end |
#id ⇒ Object
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.
46 47 48 |
# File 'lib/jsonapi-serializers/serializer.rb', line 46 def id object.id.to_s end |
#initialize(object, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jsonapi-serializers/serializer.rb', line 33 def initialize(object, = {}) @object = object @options = @context = [:context] || {} @base_url = [:base_url] # Internal serializer options, not exposed through attr_accessor. No touchie. @_fields = [:fields] || {} @_include_linkages = [:include_linkages] || [] end |
#jsonapi ⇒ Object
Override this to provide resource-object jsonapi object containing the version in use. jsonapi.org/format/#document-jsonapi-object
75 76 |
# File 'lib/jsonapi-serializers/serializer.rb', line 75 def jsonapi end |
#links ⇒ Object
100 101 102 103 104 |
# File 'lib/jsonapi-serializers/serializer.rb', line 100 def links data = {} data['self'] = self_link if self_link data end |
#meta ⇒ Object
Override this to provide resource-object metadata. jsonapi.org/format/#document-structure-resource-objects
80 81 |
# File 'lib/jsonapi-serializers/serializer.rb', line 80 def end |
#relationship_related_link(attribute_name) ⇒ Object
96 97 98 |
# File 'lib/jsonapi-serializers/serializer.rb', line 96 def (attribute_name) "#{self_link}/#{format_name(attribute_name)}" end |
#relationship_self_link(attribute_name) ⇒ Object
92 93 94 |
# File 'lib/jsonapi-serializers/serializer.rb', line 92 def relationship_self_link(attribute_name) "#{self_link}/relationships/#{format_name(attribute_name)}" end |
#relationships ⇒ Object
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/jsonapi-serializers/serializer.rb', line 106 def relationships data = {} # Merge in data for has_one relationships. has_one_relationships.each do |attribute_name, attr_data| formatted_attribute_name = format_name(attribute_name) data[formatted_attribute_name] = {} if attr_data[:options][:include_links] links_self = relationship_self_link(attribute_name) = (attribute_name) data[formatted_attribute_name]['links'] = {} if links_self || data[formatted_attribute_name]['links']['self'] = links_self if links_self data[formatted_attribute_name]['links']['related'] = if end if @_include_linkages.include?(formatted_attribute_name) || attr_data[:options][:include_data] object = has_one_relationship(attribute_name, attr_data) 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]['data'] = nil else = JSONAPI::Serializer.find_serializer(object, @options) data[formatted_attribute_name]['data'] = { 'type' => .type.to_s, 'id' => .id.to_s, } end end end # Merge in data for has_many relationships. has_many_relationships.each do |attribute_name, attr_data| formatted_attribute_name = format_name(attribute_name) data[formatted_attribute_name] = {} if attr_data[:options][:include_links] links_self = relationship_self_link(attribute_name) = (attribute_name) data[formatted_attribute_name]['links'] = {} if links_self || data[formatted_attribute_name]['links']['self'] = links_self if links_self data[formatted_attribute_name]['links']['related'] = if end # 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) || attr_data[:options][:include_data] data[formatted_attribute_name]['data'] = [] objects = has_many_relationship(attribute_name, attr_data) || [] objects.each do |obj| = JSONAPI::Serializer.find_serializer(obj, @options) data[formatted_attribute_name]['data'] << { 'type' => .type.to_s, 'id' => .id.to_s, } end end end data end |
#self_link ⇒ Object
88 89 90 |
# File 'lib/jsonapi-serializers/serializer.rb', line 88 def self_link "#{base_url}/#{type}/#{id}" end |
#type ⇒ Object
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.
54 55 56 57 |
# File 'lib/jsonapi-serializers/serializer.rb', line 54 def type class_name = object.class.name @@class_names[class_name] ||= class_name.demodulize.tableize.dasherize.freeze end |
#unformat_name(attribute_name) ⇒ Object
The opposite of format_name. Override this if you override format_name.
68 69 70 71 |
# File 'lib/jsonapi-serializers/serializer.rb', line 68 def unformat_name(attribute_name) attr_name = attribute_name.to_s @@unformatted_attribute_names[attr_name] ||= attr_name.underscore.freeze end |