Class: Particle::Model
- Inherits:
-
Object
- Object
- Particle::Model
- Defined in:
- lib/particle/model.rb
Overview
Base class for domain models
Direct Known Subclasses
Class Method Summary collapse
-
.attribute_reader(*keys) ⇒ Object
Define accessor methods for attributes.
Instance Method Summary collapse
-
#attributes ⇒ Object
Hash of all attributes returned by the cloud.
-
#get_attributes ⇒ Object
Load the model attributes with the correct API call in subclasses.
-
#id ⇒ Object
Accessor for the id.
-
#initialize(client, attributes) ⇒ Model
constructor
A new instance of Model.
-
#inspect ⇒ Object
Display the attributes when inspecting the object in the console.
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
#attributes ⇒ Object
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_attributes ⇒ Object
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 |
#id ⇒ Object
Accessor for the id
23 24 25 |
# File 'lib/particle/model.rb', line 23 def id @attributes[:id] end |
#inspect ⇒ Object
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 |