Class: PRX::Model::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
# File 'lib/prx/model/base.rb', line 7

def initialize(*args)
  return unless args
  if args[0].is_a?(Hash)
    args[0].each{|k,v| self.send("#{k.to_s}=", v)}
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/prx/model/base.rb', line 5

def id
  @id
end

Class Method Details

.request(opts) ⇒ Object



36
37
38
# File 'lib/prx/model/base.rb', line 36

def self.request(opts)
  PRX::Client.request(opts)
end

Instance Method Details

#class_path_partObject



28
29
30
# File 'lib/prx/model/base.rb', line 28

def class_path_part
  self.class.name.demodulize.underscore.pluralize
end

#find_by_id(id) ⇒ Object



14
15
16
# File 'lib/prx/model/base.rb', line 14

def find_by_id(id)
  request(:path => "#{class_path_part}/#{id}")
end

#request(opts) ⇒ Object



32
33
34
# File 'lib/prx/model/base.rb', line 32

def request(opts)
  self.class.request(opts)
end

#saveObject



18
19
20
21
22
23
24
25
26
# File 'lib/prx/model/base.rb', line 18

def save
  opts = {
    :action => (id ? :put : :post),
    :path   => (id ? "#{class_path_part}/#{id}" : class_path_part),
    :body   => as_json
  }
  response = request(opts)
  self.from_json(response.response.body)
end