Class: Geotrigger::Model

Inherits:
Object
  • Object
show all
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

Application, Device, Tag, Trigger

Defined Under Namespace

Modules: Taggable Classes: StateError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Model

Create an instance and from given options Hash.

:session

Session underlying session to use when talking to the API



37
38
39
# File 'lib/geotrigger/model.rb', line 37

def initialize opts = {}
  @session = opts[:session] || Session.new(opts)
end

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

#dataObject

The data behind the model object.



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

def data
  @data
end

#sessionObject (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

String name of the model to request listed data for

params

Hash parameters to send with the request

default_params

Hash default parameters to merge params into



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