Class: Particle::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/particle/model.rb

Overview

Base class for domain models

Direct Known Subclasses

BuildTarget, Device, Library, OAuthClient, Platform, Token, Webhook

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes) ⇒ Model

Returns a new instance of Model.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/particle/model.rb', line 5

def initialize(client, attributes)
  @client = client
  @attributes =
    if attributes.is_a? String
      { id: attributes }
    else
      # Consider attributes loaded when passed in through constructor
      @loaded = true
      attributes
    end
end

Class Method Details

.attribute_reader(*keys) ⇒ Object

Define accessor methods for attributes.

Will load the attributes from the cloud if not already done



30
31
32
33
34
35
36
# File 'lib/particle/model.rb', line 30

def self.attribute_reader(*keys)
  keys.each do |key|
    define_method key do
      attributes[key]
    end
  end
end

Instance Method Details

#attributesObject

Hash of all attributes returned by the cloud



39
40
41
42
# File 'lib/particle/model.rb', line 39

def attributes
  get_attributes unless @loaded
  @attributes
end

#get_attributesObject

Load the model attributes with the correct API call in subclasses



45
46
47
# File 'lib/particle/model.rb', line 45

def get_attributes
  raise "Implement in subclasses and set @loaded = true"
end

#idObject

Accessor for the id



23
24
25
# File 'lib/particle/model.rb', line 23

def id
  @attributes[:id]
end

#inspectObject

Display the attributes when inspecting the object in the console



18
19
20
# File 'lib/particle/model.rb', line 18

def inspect
  "#<#{self.class} #{@attributes}>"
end