Module: Jat::Plugins::JsonApi::ClassMethods

Defined in:
lib/jat/plugins/json_api/json_api.rb

Instance Method Summary collapse

Instance Method Details

#added_document_metaObject

Top-level document meta

jsonapi.org/format/#document-meta



214
215
216
# File 'lib/jat/plugins/json_api/json_api.rb', line 214

def added_document_meta
  @added_document_meta ||= {}
end

#added_object_metaObject



202
203
204
# File 'lib/jat/plugins/json_api/json_api.rb', line 202

def added_object_meta
  @added_object_meta ||= {}
end

#added_relationship_metaObject



226
227
228
# File 'lib/jat/plugins/json_api/json_api.rb', line 226

def added_relationship_meta
  @added_relationship_meta ||= {}
end


182
183
184
185
# File 'lib/jat/plugins/json_api/json_api.rb', line 182

def document_link(name, **opts, &block)
  new_attr = self::Attribute.new(name: name, opts: opts, block: block)
  document_links[new_attr.name] = new_attr
end

Top-level document links

jsonapi.org/format/#document-top-level



178
179
180
# File 'lib/jat/plugins/json_api/json_api.rb', line 178

def document_links
  @document_links ||= {}
end

#document_meta(name, **opts, &block) ⇒ Object



218
219
220
221
# File 'lib/jat/plugins/json_api/json_api.rb', line 218

def document_meta(name, **opts, &block)
  new_attr = self::Attribute.new(name: name, opts: opts, block: block)
  added_document_meta[new_attr.name] = new_attr
end

#get_idObject



143
144
145
# File 'lib/jat/plugins/json_api/json_api.rb', line 143

def get_id
  @id
end

#get_typeObject



135
136
137
# File 'lib/jat/plugins/json_api/json_api.rb', line 135

def get_type
  (defined?(@type) && @type) || raise(Error, "#{self} has no defined type")
end

#id(**opts, &block) ⇒ Object



147
148
149
# File 'lib/jat/plugins/json_api/json_api.rb', line 147

def id(**opts, &block)
  @id = self::Attribute.new(name: :id, opts: opts, block: block)
end

#inherited(subclass) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jat/plugins/json_api/json_api.rb', line 66

def inherited(subclass)
  super

  fields_parser_class = Class.new(self::FieldsParamParser)
  fields_parser_class.jat_class = subclass
  subclass.const_set(:FieldsParamParser, fields_parser_class)

  includes_parser_class = Class.new(self::IncludeParamParser)
  includes_parser_class.jat_class = subclass
  subclass.const_set(:IncludeParamParser, includes_parser_class)

  map_class = Class.new(self::Map)
  map_class.jat_class = subclass
  subclass.const_set(:Map, map_class)

  response_class = Class.new(self::Response)
  response_class.jat_class = subclass
  subclass.const_set(:Response, response_class)

  response_piece_class = Class.new(self::ResponsePiece)
  response_piece_class.jat_class = subclass
  subclass.const_set(:ResponsePiece, response_piece_class)

  subclass.type(@type) if defined?(@type)
  subclass.id(&get_id.params[:block])

  # Assign same jsonapi_data
  jsonapi_data.each_value do |attribute|
    params = attribute.params
    subclass.jsonapi(params[:name], **params[:opts], &params[:block])
  end

  # Assign same object_links
  object_links.each_value do |attribute|
    params = attribute.params
    subclass.object_link(params[:name], **params[:opts], &params[:block])
  end

  # Assign same document_links
  document_links.each_value do |attribute|
    params = attribute.params
    subclass.document_link(params[:name], **params[:opts], &params[:block])
  end

  # Assign same relationship_links
  relationship_links.each_value do |attribute|
    params = attribute.params
    subclass.relationship_link(params[:name], **params[:opts], &params[:block])
  end

  # Assign same added_object_meta
  added_object_meta.each_value do |attribute|
    params = attribute.params
    subclass.object_meta(params[:name], **params[:opts], &params[:block])
  end

  # Assign same added_document_meta
  added_document_meta.each_value do |attribute|
    params = attribute.params
    subclass.document_meta(params[:name], **params[:opts], &params[:block])
  end

  # Assign same added_relationship_meta
  added_relationship_meta.each_value do |attribute|
    params = attribute.params
    subclass.relationship_meta(params[:name], **params[:opts], &params[:block])
  end
end

#jsonapi(name, **opts, &block) ⇒ Object



158
159
160
161
# File 'lib/jat/plugins/json_api/json_api.rb', line 158

def jsonapi(name, **opts, &block)
  new_attr = self::Attribute.new(name: name, opts: opts, block: block)
  jsonapi_data[new_attr.name] = new_attr
end

#jsonapi_data(_value = nil) ⇒ Object



154
155
156
# File 'lib/jat/plugins/json_api/json_api.rb', line 154

def jsonapi_data(_value = nil)
  @jsonapi_data ||= {}
end

#map(context) ⇒ Object



235
236
237
# File 'lib/jat/plugins/json_api/json_api.rb', line 235

def map(context)
  self::Map.call(context)
end

#map_exposedObject



243
244
245
# File 'lib/jat/plugins/json_api/json_api.rb', line 243

def map_exposed
  @map_exposed ||= self::Map.call(exposed: :default)
end

#map_fullObject



239
240
241
# File 'lib/jat/plugins/json_api/json_api.rb', line 239

def map_full
  @map_full ||= self::Map.call(exposed: :all)
end


170
171
172
173
# File 'lib/jat/plugins/json_api/json_api.rb', line 170

def object_link(name, **opts, &block)
  new_attr = self::Attribute.new(name: name, opts: opts, block: block)
  object_links[new_attr.name] = new_attr
end


166
167
168
# File 'lib/jat/plugins/json_api/json_api.rb', line 166

def object_links
  @object_links ||= {}
end

#object_meta(name, **opts, &block) ⇒ Object



206
207
208
209
# File 'lib/jat/plugins/json_api/json_api.rb', line 206

def object_meta(name, **opts, &block)
  new_attr = self::Attribute.new(name: name, opts: opts, block: block)
  added_object_meta[new_attr.name] = new_attr
end


194
195
196
197
# File 'lib/jat/plugins/json_api/json_api.rb', line 194

def relationship_link(name, **opts, &block)
  new_attr = self::Attribute.new(name: name, opts: opts, block: block)
  relationship_links[new_attr.name] = new_attr
end


190
191
192
# File 'lib/jat/plugins/json_api/json_api.rb', line 190

def relationship_links
  @relationship_links ||= {}
end

#relationship_meta(name, **opts, &block) ⇒ Object



230
231
232
233
# File 'lib/jat/plugins/json_api/json_api.rb', line 230

def relationship_meta(name, **opts, &block)
  new_attr = self::Attribute.new(name: name, opts: opts, block: block)
  added_relationship_meta[new_attr.name] = new_attr
end

#type(new_type) ⇒ Object



139
140
141
# File 'lib/jat/plugins/json_api/json_api.rb', line 139

def type(new_type)
  @type = new_type.to_sym
end