Class: SimpleJSONAPIClient::Base
- Inherits:
-
Object
- Object
- SimpleJSONAPIClient::Base
- Defined in:
- lib/simple_jsonapi_client/base.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- ._attributes ⇒ Object
- .attributes(*attrs) ⇒ Object
- .create(opts) ⇒ Object
- .delete(opts) ⇒ Object
- .fetch(opts) ⇒ Object
- .fetch_all(opts) ⇒ Object
- .has_many(relationship_name, opts) ⇒ Object
- .has_one(relationship_name, opts) ⇒ Object
- .include_records(included_hash, records) ⇒ Object
- .interpreted_included(records, included) ⇒ Object
- .meta(*attrs) ⇒ Object
- .model_from(record, included, connection, context = nil) ⇒ Object
- .relationships ⇒ Object
- .template(id: nil, attributes:, relationships: {}) ⇒ Object
- .update(opts) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
- #attributes ⇒ Object
- #delete ⇒ Object
-
#initialize(meta: nil, id:, attributes: nil, relationships: nil, included: {}, connection:, context: nil) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #meta ⇒ Object
- #relationships ⇒ Object
- #same_record_as?(other) ⇒ Boolean
- #to_json(*args) ⇒ Object
- #to_relationship ⇒ Object
- #update(**attrs) ⇒ Object
Constructor Details
#initialize(meta: nil, id:, attributes: nil, relationships: nil, included: {}, connection:, context: nil) ⇒ Base
Returns a new instance of Base.
240 241 242 243 244 245 246 247 248 |
# File 'lib/simple_jsonapi_client/base.rb', line 240 def initialize(meta: nil, id:, attributes: nil, relationships: nil, included: {}, connection:, context: nil) @meta = .symbolize_keys if @id = id @included = included @connection = connection @context = context @attributes = attributes.symbolize_keys if attributes @input_relationships = relationships end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
238 239 240 |
# File 'lib/simple_jsonapi_client/base.rb', line 238 def context @context end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
238 239 240 |
# File 'lib/simple_jsonapi_client/base.rb', line 238 def id @id end |
Class Method Details
._attributes ⇒ Object
14 15 16 |
# File 'lib/simple_jsonapi_client/base.rb', line 14 def _attributes @_attributes ||= {} end |
.attributes(*attrs) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/simple_jsonapi_client/base.rb', line 18 def attributes(*attrs) attrs.each do |attr| define_method(attr) { attributes[attr] } define_method("#{attr}=") { |x| attributes[attr] = x } _attributes[attr] = true end end |
.create(opts) ⇒ Object
57 58 59 |
# File 'lib/simple_jsonapi_client/base.rb', line 57 def create(opts) operation(:create_request, :singular, opts) end |
.delete(opts) ⇒ Object
65 66 67 68 |
# File 'lib/simple_jsonapi_client/base.rb', line 65 def delete(opts) operation(:delete_request, :empty, opts) true end |
.fetch(opts) ⇒ Object
47 48 49 |
# File 'lib/simple_jsonapi_client/base.rb', line 47 def fetch(opts) operation(:fetch_request, :singular, opts) end |
.fetch_all(opts) ⇒ Object
51 52 53 54 55 |
# File 'lib/simple_jsonapi_client/base.rb', line 51 def fetch_all(opts) Redirection::FetchAll.new(opts) do |request_opts| operation(:fetch_all_request, :plural, request_opts) end end |
.has_many(relationship_name, opts) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/simple_jsonapi_client/base.rb', line 33 def has_many(relationship_name, opts) model_class = opts.fetch(:class) { opts.fetch(:class_name) } define_relationship_methods!(relationship_name) relationships[relationship_name.to_sym] = Relationships::HasManyRelationship.new(model_class) end |
.has_one(relationship_name, opts) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/simple_jsonapi_client/base.rb', line 40 def has_one(relationship_name, opts) define_relationship_methods!(relationship_name) model_class = opts.fetch(:class) { opts.fetch(:class_name) } relationships[relationship_name.to_sym] = Relationships::HasOneRelationship.new(model_class) end |
.include_records(included_hash, records) ⇒ Object
90 91 92 93 94 |
# File 'lib/simple_jsonapi_client/base.rb', line 90 def include_records(included_hash, records) records.to_a.each do |record| included_hash[{ 'id' => record['id'], 'type' => record['type'] }] = record end end |
.interpreted_included(records, included) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/simple_jsonapi_client/base.rb', line 83 def interpreted_included(records, included) {}.tap do |included_hash| include_records(included_hash, records) include_records(included_hash, included) end end |
.meta(*attrs) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/simple_jsonapi_client/base.rb', line 26 def (*attrs) attrs.each do |attr| define_method(attr) { [attr] } define_method("#{attr}=") { |x| [attr] = x } end end |
.model_from(record, included, connection, context = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/simple_jsonapi_client/base.rb', line 70 def model_from(record, included, connection, context = nil) return unless record new( meta: record['meta'], id: record['id'], attributes: record.fetch('attributes', {}), relationships: record.fetch('relationships', {}), context: context, included: included, connection: connection ) end |
.relationships ⇒ Object
10 11 12 |
# File 'lib/simple_jsonapi_client/base.rb', line 10 def relationships @relationships ||= {} end |
.template(id: nil, attributes:, relationships: {}) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/simple_jsonapi_client/base.rb', line 96 def template(id: nil, attributes:, relationships: {}) data = { type: self::TYPE, attributes: attributes, relationships: interpreted_relationships(relationships) } data[:id] = id if id { data: data } end |
.update(opts) ⇒ Object
61 62 63 |
# File 'lib/simple_jsonapi_client/base.rb', line 61 def update(opts) operation(:update_request, :singular, opts) end |
Instance Method Details
#as_json ⇒ Object
293 294 295 296 297 298 299 |
# File 'lib/simple_jsonapi_client/base.rb', line 293 def as_json self.class.template( id: id, attributes: attributes, relationships: relationships ) end |
#attributes ⇒ Object
258 259 260 |
# File 'lib/simple_jsonapi_client/base.rb', line 258 def attributes @attributes ||= loaded_record.attributes end |
#delete ⇒ Object
286 287 288 289 290 291 |
# File 'lib/simple_jsonapi_client/base.rb', line 286 def delete self.class.delete( connection: connection, url_opts: { id: id } ) end |
#inspect ⇒ Object
305 306 307 308 309 310 311 |
# File 'lib/simple_jsonapi_client/base.rb', line 305 def inspect parsed_attributes = attributes.map { |key, value| "#{key}=#{value.inspect}" }.join(' ') parsed_attributes = " #{parsed_attributes}" unless parsed_attributes.empty? parsed_relationships = relationships.map { |key, value| "#{key}=#{value.inspect}" }.join(' ') parsed_relationships = " #{parsed_relationships}" unless parsed_relationships.empty? "#<#{self.class.name} id=#{id}#{parsed_attributes}#{parsed_relationships}>" end |
#meta ⇒ Object
262 263 264 |
# File 'lib/simple_jsonapi_client/base.rb', line 262 def @meta ||= loaded_record. end |
#relationships ⇒ Object
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/simple_jsonapi_client/base.rb', line 266 def relationships @relationships ||= begin if input_relationships relationships_to_models(input_relationships.symbolize_keys) else loaded_record.relationships end end end |
#same_record_as?(other) ⇒ Boolean
254 255 256 |
# File 'lib/simple_jsonapi_client/base.rb', line 254 def same_record_as?(other) to_relationship == other.to_relationship end |
#to_json(*args) ⇒ Object
301 302 303 |
# File 'lib/simple_jsonapi_client/base.rb', line 301 def to_json(*args) as_json.to_json(*args) end |
#to_relationship ⇒ Object
250 251 252 |
# File 'lib/simple_jsonapi_client/base.rb', line 250 def to_relationship { type: self.class::TYPE, id: id } end |
#update(**attrs) ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/simple_jsonapi_client/base.rb', line 277 def update(**attrs) self.class.update( connection: connection, id: id, url_opts: { id: id }, **attrs ) end |