Class: Geotrigger::Model
- Inherits:
-
Object
- Object
- Geotrigger::Model
- Extended by:
- Forwardable
- Defined in:
- lib/geotrigger/model.rb
Overview
Superclass for Geotrigger “objects” - Application, Trigger, Device, Tag.
Contains the base logic for interacting with the Geotrigger API in an ORM-like fashion. Never instantiated directly by the user.
Direct Known Subclasses
Defined Under Namespace
Modules: Taggable Classes: StateError
Instance Attribute Summary collapse
-
#data ⇒ Object
The data behind the model object.
-
#session ⇒ Object
readonly
The
Sessionthe model object uses to talk to the API.
Class Method Summary collapse
-
.from_api(data, session) ⇒ Object
Create an instance of the subclassed model object from data retrieved from the API.
Instance Method Summary collapse
-
#==(obj) ⇒ Object
Compares underlying data for equality.
-
#initialize(opts = {}) ⇒ Model
constructor
Create an instance and from given options
Hash. -
#method_missing(meth, *args) ⇒ Object
Allows snake_case accessor to top-level data values keyed by their camelCase counterparts.
-
#post_list(models, params = {}, default_params = {}) ⇒ Object
POST a request to this model’s /list route, passing parameters.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
Allows snake_case accessor to top-level data values keyed by their camelCase counterparts. An attempt to be moar Rubyish.
device.tracking_profile
#=> 'adaptive'
device.trackingProfile
#=> 'adaptive'
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/geotrigger/model.rb', line 66 def method_missing meth, *args meth_s = meth.to_s if meth_s =~ /=$/ and args.length == 1 key = meth_s.sub(/=$/,'').camelcase if @data and @data.key? key @data[key] = args[0] else super meth, *args end else key = meth_s.camelcase if @data and @data.key? key @data[key] else super meth, *args end end end |
Instance Attribute Details
#data ⇒ Object
The data behind the model object.
18 19 20 |
# File 'lib/geotrigger/model.rb', line 18 def data @data end |
#session ⇒ Object (readonly)
The Session the model object uses to talk to the API.
22 23 24 |
# File 'lib/geotrigger/model.rb', line 22 def session @session end |
Class Method Details
.from_api(data, session) ⇒ Object
Create an instance of the subclassed model object from data retrieved from the API.
27 28 29 30 31 |
# File 'lib/geotrigger/model.rb', line 27 def self.from_api data, session i = self.new session: session i.data = data return i end |
Instance Method Details
#==(obj) ⇒ Object
Compares underlying data for equality.
87 88 89 90 91 92 93 |
# File 'lib/geotrigger/model.rb', line 87 def == obj if Model === obj self.data == obj.data else false end end |
#post_list(models, params = {}, default_params = {}) ⇒ Object
POST a request to this model’s /list route, passing parameters. Returns a new instance of the model object with populated data via Model.from_api.
- models
-
Stringname of the model to request listed data for - params
-
Hashparameters to send with the request - default_params
-
Hashdefault parameters to mergeparamsinto
49 50 51 52 53 54 55 |
# File 'lib/geotrigger/model.rb', line 49 def post_list models, params = {}, default_params = {} model = models.sub /s$/, '' params = default_params.merge params post(model + '/list', params)[models].map do |data| Geotrigger.const_get(model.capitalize).from_api data, @session end end |