Class: SimpleAMS::Adapters::JSONAPI
- Inherits:
-
Object
- Object
- SimpleAMS::Adapters::JSONAPI
- Defined in:
- lib/simple_ams/adapters/jsonapi.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Collection
Constant Summary collapse
- DEFAULT_OPTIONS =
{ skip_id_in_attributes: false, key_transform: nil }.freeze
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #as_json ⇒ Object
- #data ⇒ Object
- #embedded_relation_data_for(relation) ⇒ Object
- #embedded_relation_links_for(relation) ⇒ Object
- #fields ⇒ Object
- #forms ⇒ Object
- #included ⇒ Object
-
#initialize(document, options = {}) ⇒ JSONAPI
constructor
A new instance of JSONAPI.
- #links ⇒ Object
- #metas ⇒ Object
- #relationships ⇒ Object
- #transform_key(key) ⇒ Object
Constructor Details
#initialize(document, options = {}) ⇒ JSONAPI
Returns a new instance of JSONAPI.
11 12 13 14 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 11 def initialize(document, = {}) @document = document @options = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
9 10 11 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 9 def document @document end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 9 def @options end |
Instance Method Details
#as_json ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 16 def as_json hash = { data: data } hash.merge!(links: links) unless links.empty? hash.merge!(metas: ) unless .empty? hash.merge!(included: included) unless included.empty? hash end |
#data ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 28 def data data = { transform_key(document.primary_id.name) => document.primary_id.value.to_s, type: document.type.value, attributes: fields } data.merge!(relationships: relationships) unless relationships.empty? data end |
#embedded_relation_data_for(relation) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 73 def (relation) return {} if relation..generics[:skip_data]&.value if relation.folder? relation.each.map do |doc| { doc.primary_id.name => doc.primary_id.value.to_s, type: doc.type.name } end else { relation.primary_id.name => relation.primary_id.value.to_s, type: relation.type.name } end end |
#embedded_relation_links_for(relation) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 91 def (relation) return {} if relation..links.empty? relation..links.each_with_object({}) do |link, hash| hash[link.name] = link.value end end |
#fields ⇒ Object
40 41 42 43 44 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 40 def fields @fields ||= document.fields.each_with_object({}) do |field, hash| hash[transform_key(field.key)] = field.value unless [:skip_id_in_attributes] && field_is_primary_id?(field) end end |
#forms ⇒ Object
113 114 115 116 117 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 113 def forms @forms ||= document.forms.each_with_object({}) do |form, hash| hash[form.name] = form.value end end |
#included ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 119 def included return @included ||= [] if document.relations.available.empty? @included ||= document.relations.available.each_with_object([]) do |relation, array| next array if relation..generics[:skip_data]&.value array << if relation.folder? relation.map { |doc| self.class.new(doc, ).as_json[:data] } else self.class.new(relation, ).as_json[:data] end end.flatten end |
#links ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 99 def links return @links ||= {} if document.links.empty? @links ||= document.links.each_with_object({}) do |link, hash| hash[link.name] = link.value end end |
#metas ⇒ Object
107 108 109 110 111 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 107 def @metas ||= document..each_with_object({}) do |, hash| hash[.name] = .value end end |
#relationships ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 59 def relationships return @relationships ||= {} if document.relations.empty? @relationships ||= document.relations.each_with_object({}) do |relation, hash| = (relation) hash.merge!(data: (relation)) unless .empty? = (relation) hash.merge!(links: (relation)) unless .empty? hash.merge!(relation.name => hash) unless hash.empty? end end |
#transform_key(key) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 46 def transform_key(key) case [:key_transform] when :camel key.to_s.split('_').map(&capitalize).join when :kebab key.to_s.gsub('_', '-') when :snake key else key end end |