Class: Mosaic::Sdk::Models::Base::Instance Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/mosaic/sdk/models/base.rb

Overview

This class is abstract.

Subclass and implement #uri

Abstract class representing an individual resource instance.

Handles lazy loading of data and associations to other resource types via the Instance.association class method.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, data: nil, **extra) ⇒ Instance

Initializes a resource instance.

Parameters:

  • id (String, Integer)

    the resource identifier

  • data (Hash, nil) (defaults to: nil)

    pre-loaded data (optional)

  • extra (Hash)

    additional context (e.g. parent IDs)

Class Method Details

.association(name) ⇒ void

This method returns an undefined value.

Declares an association to another resource type.

Dynamically defines an instance method that allows navigating to an associated collection or a specific instance.

Examples:

Declare an association

class Dna::Instance < Base::Instance
  association :clones
end

Use an association

dna = Mosaic::Sdk.Dna(42)
dna.clones        # => Clone (collection)
dna.clones(7)     # => Clone::Instance (specific instance)

Parameters:

  • name (Symbol)

    the association name (plural, e.g. :clones)

Instance Method Details

#[](key) ⇒ Object?

Returns a single attribute from #data (lazily fetched), by string or symbol name.

Parameters:

  • key (Symbol, String)

    the attribute name

Returns:

  • (Object, nil)

    the attribute value

#dataHash

Returns the resource data, loading it from the API if needed (lazy loading).

Returns:

  • (Hash)

    the resource data

#update(**kwargs) ⇒ self

Addresses the resource by its id, unlike a create that has to locate its target from the very attributes it is given.

Examples:

Attach a code to a clone

Mosaic::Sdk.Clone(4242).update(codes: [{ type: 'valoren', value: '12105022' }])

Parameters:

  • kwargs (Hash)

    the attributes to update

Returns:

  • (self)