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: true }
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.
9 10 11 12 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 9 def initialize(document, = {}) @document = document = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
8 9 10 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 8 def document @document end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 8 def end |
Instance Method Details
#as_json ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 14 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? return hash end |
#data ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 26 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 71 def (relation) return {} if relation..generics[:skip_data]&.value if relation.folder? value = relation.documents.map{|doc| { doc.primary_id.name => doc.primary_id.value.to_s, type: doc.type.name } } else value = { relation.primary_id.name => relation.primary_id.value.to_s, type: relation.type.name } end end |
#embedded_relation_links_for(relation) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 89 def (relation) return {} if relation..links.empty? relation..links.inject({}){ |hash, link| hash[link.name] = link.value hash } end |
#fields ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 38 def fields @fields ||= document.fields.inject({}){ |hash, field| unless [:skip_id_in_attributes] && field_is_primary_id?(field) hash[transform_key(field.key)] = field.value end hash } end |
#forms ⇒ Object
115 116 117 118 119 120 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 115 def forms @forms ||= document.forms.inject({}){ |hash, form| hash[form.name] = form.value hash } end |
#included ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 122 def included return @included ||= [] if document.relations.available.empty? @included ||= document.relations.available.inject([]){ |array, relation| if relation.folder? array << relation.map{|doc| self.class.new(doc).as_json[:data]} else array << self.class.new(relation).as_json[:data] end array }.flatten end |
#links ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 99 def links return @links ||= {} if document.links.empty? @links ||= document.links.inject({}){ |hash, link| hash[link.name] = link.value hash } end |
#metas ⇒ Object
108 109 110 111 112 113 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 108 def ||= document..inject({}){ |hash, | hash[.name] = .value hash } end |
#relationships ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 51 def relationships return @relationships ||= {} if document.relations.empty? @relationships ||= document.relations.inject({}){ |hash, relation| _hash = {} = (relation) unless .empty? _hash.merge!(data: (relation)) end = (relation) unless .empty? _hash.merge!(links: (relation)) end hash.merge!(relation.name => _hash) unless _hash.empty? hash } end |
#transform_key(key) ⇒ Object
47 48 49 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 47 def transform_key(key) key.to_s.gsub('_', '-') end |