Sha256: f2ceb81118e2741695bac7f7be8f83fe6a20f0795cc7c916504a529d1e341820

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

module ArtemisApi
  class Model
    attr_reader :client, :id, :attributes, :relationships

    def self.related_to_one(name)
      self.send(:define_method, name.to_sym) do
        relationship = relationships[name.to_s]['data']
        @client.find_one(relationship['type'], relationship['id'])
      end
    end

    def self.related_to_many(name)
      self.send(:define_method, name.to_sym) do
        @client.find_all(self.relationships[name.to_s]['data']['type'])
      end
    end

    def self.json_type(type = nil)
      if type
        @json_type = type
        @@registered_classes ||= {}
        @@registered_classes[type] = self
      end
      @json_type
    end

    def self.instance_for(type, data, client)
      @@registered_classes[type]&.new(client, data)
    end

    def method_missing(name)
      attributes[name.to_s]
    end

    def initialize(client, data)
      @client = client
      @id = data['id'].to_i
      @attributes = data['attributes']
      @relationships = data['relationships']
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
artemis_api-0.5.1 lib/artemis_api/model.rb
artemis_api-0.4.0 lib/artemis_api/model.rb
artemis_api-0.3.0 lib/artemis_api/model.rb
artemis_api-0.2.1 lib/artemis_api/model.rb
artemis_api-0.2.0 lib/artemis_api/model.rb
artemis_api-0.1.0 lib/artemis_api/model.rb