Class: Dato::JsonApiSerializer
- Inherits:
-
Object
- Object
- Dato::JsonApiSerializer
- Defined in:
- lib/dato/json_api_serializer.rb
Instance Attribute Summary collapse
-
#link ⇒ Object
readonly
Returns the value of attribute link.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #attributes(resource) ⇒ Object
-
#initialize(type, link) ⇒ JsonApiSerializer
constructor
A new instance of JsonApiSerializer.
- #link_attributes ⇒ Object
- #link_relationships ⇒ Object
- #relationships ⇒ Object
- #required_attributes ⇒ Object
- #required_relationships ⇒ Object
- #serialize(resource, id = nil) ⇒ Object
- #serialized_attributes(resource) ⇒ Object
- #serialized_relationships(resource) ⇒ Object
Constructor Details
#initialize(type, link) ⇒ JsonApiSerializer
Returns a new instance of JsonApiSerializer.
8 9 10 11 |
# File 'lib/dato/json_api_serializer.rb', line 8 def initialize(type, link) @link = link @type = type end |
Instance Attribute Details
#link ⇒ Object (readonly)
Returns the value of attribute link.
6 7 8 |
# File 'lib/dato/json_api_serializer.rb', line 6 def link @link end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/dato/json_api_serializer.rb', line 6 def type @type end |
Instance Method Details
#attributes(resource) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dato/json_api_serializer.rb', line 88 def attributes(resource) if type == 'item' return resource.keys.map(&:to_sym) - %i[ item_type id created_at updated_at creator ] end link_attributes['properties'].keys.map(&:to_sym) end |
#link_attributes ⇒ Object
116 117 118 |
# File 'lib/dato/json_api_serializer.rb', line 116 def link_attributes link.schema.properties['data'].properties['attributes'] end |
#link_relationships ⇒ Object
120 121 122 |
# File 'lib/dato/json_api_serializer.rb', line 120 def link_relationships link.schema.properties['data'].properties['relationships'] end |
#relationships ⇒ Object
108 109 110 |
# File 'lib/dato/json_api_serializer.rb', line 108 def relationships @relationships ||= JsonSchemaRelationships.new(link.schema).relationships end |
#required_attributes ⇒ Object
102 103 104 105 106 |
# File 'lib/dato/json_api_serializer.rb', line 102 def required_attributes return [] if type == 'item' (link_attributes.required || []).map(&:to_sym) end |
#required_relationships ⇒ Object
112 113 114 |
# File 'lib/dato/json_api_serializer.rb', line 112 def required_relationships (link_relationships.required || []).map(&:to_sym) end |
#serialize(resource, id = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dato/json_api_serializer.rb', line 13 def serialize(resource, id = nil) resource = resource.with_indifferent_access data = {} data[:id] = id || resource[:id] if id || resource[:id] if resource.has_key?(:meta) resource.delete(:meta) end data[:type] = type data[:attributes] = serialized_attributes(resource) if relationships.any? data[:relationships] = serialized_relationships(resource) end { data: data } end |
#serialized_attributes(resource) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dato/json_api_serializer.rb', line 33 def serialized_attributes(resource) result = {} attributes(resource).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
47 48 49 50 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 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dato/json_api_serializer.rb', line 47 def serialized_relationships(resource) result = {} relationships.each do |relationship, | if resource.key? relationship value = resource[relationship] data = if value if [:types].length > 1 if [:collection] value.map(&:symbolize_keys) else value.symbolize_keys end else type = [:types].first if [:collection] value.map do |id| { type: type, id: id.to_s } end else { type: type, id: value.to_s } end end end result[relationship] = { data: data } elsif required_relationships.include?(relationship) throw "Required attribute: #{relationship}" end end result end |