Class: Dato::JsonApiSerializer
- Inherits:
-
Object
- Object
- Dato::JsonApiSerializer
- Defined in:
- lib/dato/json_api_serializer.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#relationships ⇒ Object
readonly
Returns the value of attribute relationships.
-
#required_attributes ⇒ Object
readonly
Returns the value of attribute required_attributes.
-
#required_relationships ⇒ Object
readonly
Returns the value of attribute required_relationships.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(type:, attributes: [], required_attributes: [], relationships: {}, required_relationships: []) ⇒ JsonApiSerializer
constructor
A new instance of JsonApiSerializer.
- #serialize(resource, id = nil) ⇒ Object
- #serialized_attributes(resource) ⇒ Object
- #serialized_relationships(resource) ⇒ Object
Constructor Details
#initialize(type:, attributes: [], required_attributes: [], relationships: {}, required_relationships: []) ⇒ JsonApiSerializer
Returns a new instance of JsonApiSerializer.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dato/json_api_serializer.rb', line 7 def initialize( type:, attributes: [], required_attributes: [], relationships: {}, required_relationships: [] ) @type = type @attributes = attributes @required_attributes = required_attributes @relationships = relationships @required_relationships = required_relationships end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/dato/json_api_serializer.rb', line 4 def attributes @attributes end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
4 5 6 |
# File 'lib/dato/json_api_serializer.rb', line 4 def relationships @relationships end |
#required_attributes ⇒ Object (readonly)
Returns the value of attribute required_attributes.
5 6 7 |
# File 'lib/dato/json_api_serializer.rb', line 5 def required_attributes @required_attributes end |
#required_relationships ⇒ Object (readonly)
Returns the value of attribute required_relationships.
5 6 7 |
# File 'lib/dato/json_api_serializer.rb', line 5 def required_relationships @required_relationships end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
4 5 6 |
# File 'lib/dato/json_api_serializer.rb', line 4 def type @type end |
Instance Method Details
#serialize(resource, id = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dato/json_api_serializer.rb', line 21 def serialize(resource, id = nil) resource = resource.with_indifferent_access data = {} data[:id] = id || resource[:id] if id || resource[:id] data[:type] = type data[:attributes] = serialized_attributes(resource) if relationships.any? data[:relationships] = serialized_relationships(resource) end { data: data } end |
#serialized_attributes(resource) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dato/json_api_serializer.rb', line 37 def serialized_attributes(resource) result = {} attributes.each do |attribute| if resource.key? attribute result[attribute] = resource[attribute] elsif required_attributes.include? attribute throw "Required attribute: #{attribute}" end end result end |
#serialized_relationships(resource) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dato/json_api_serializer.rb', line 51 def serialized_relationships(resource) result = {} relationships.each do |relationship, | if resource.key? relationship value = resource[relationship] data = if value if [:collection] value.map do |id| { type: [:type], id: id.to_s } end else { type: [:type], id: value.to_s } end end result[relationship] = { data: data } elsif required_relationships.include? relationship throw "Required attribute: #{relationship}" end end result end |