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 }
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.
10 11 12 13 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 10 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
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 15 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
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 27 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 82 def (relation) return {} if relation..generics[:skip_data]&.value if relation.folder? value = relation.each.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
100 101 102 103 104 105 106 107 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 100 def (relation) return {} if relation..links.empty? relation..links.inject({}){ |hash, link| hash[link.name] = link.value hash } end |
#fields ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 39 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
126 127 128 129 130 131 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 126 def forms @forms ||= document.forms.inject({}){ |hash, form| hash[form.name] = form.value hash } end |
#included ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 133 def included return @included ||= [] if document.relations.available.empty? @included ||= document.relations.available.inject([]){ |array, relation| next array if relation..generics[:skip_data]&.value 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
110 111 112 113 114 115 116 117 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 110 def links return @links ||= {} if document.links.empty? @links ||= document.links.inject({}){ |hash, link| hash[link.name] = link.value hash } end |
#metas ⇒ Object
119 120 121 122 123 124 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 119 def @metas ||= document..inject({}){ |hash, | hash[.name] = .value hash } end |
#relationships ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 61 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
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 48 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 |