Class: Zoop::Model

Inherits:
ZoopObject show all
Defined in:
lib/zoop/model.rb

Constant Summary

Constants inherited from ZoopObject

ZoopObject::RESOURCES

Instance Attribute Summary

Attributes inherited from ZoopObject

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZoopObject

#==, #[]=, convert, #empty?, #initialize, #respond_to?, #to_hash, #to_s, #unsaved_attributes

Constructor Details

This class inherits a constructor from Zoop::ZoopObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Zoop::ZoopObject

Class Method Details

.class_nameObject



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

def class_name
  self.name.split('::').last
end

.create(*args, &block) ⇒ Object



31
32
33
# File 'lib/zoop/model.rb', line 31

def create(*args, &block)
  self.new(*args, &block).create
end

.find_by_id(id) ⇒ Object Also known as: find

Raises:



35
36
37
38
# File 'lib/zoop/model.rb', line 35

def find_by_id(id)
  raise RequestError.new('Invalid ID') unless id.present?
  Zoop::Request.get(url id).call
end

.underscored_class_nameObject



49
50
51
# File 'lib/zoop/model.rb', line 49

def underscored_class_name
  class_name.gsub(/[a-z0-9][A-Z]/){|s| "#{s[0]}_#{s[1]}"}.downcase
end

.url(*params) ⇒ Object



41
42
43
# File 'lib/zoop/model.rb', line 41

def url(*params)
  ["/#{ CGI.escape underscored_class_name }s", *params].join '/'
end

Instance Method Details

#createObject



4
5
6
7
# File 'lib/zoop/model.rb', line 4

def create
  update_model Zoop::Request.post(@custom_url || self.class.url, params: to_hash).run
  self
end

#destroyObject



19
20
21
22
# File 'lib/zoop/model.rb', line 19

def destroy
  update_model Zoop::Request.delete(url).run
  self
end

#saveObject



14
15
16
17
# File 'lib/zoop/model.rb', line 14

def save
  update_model Zoop::Request.put(@custom_url || url, params: unsaved_attributes).run
  self
end

#update(params = {}) ⇒ Object



9
10
11
12
# File 'lib/zoop/model.rb', line 9

def update(params={})
  update_model Zoop::Request.put(@custom_url || url, params: params).run
  self
end

#url(*params) ⇒ Object

Raises:



24
25
26
27
# File 'lib/zoop/model.rb', line 24

def url(*params)
  raise RequestError.new('Invalid ID') unless id.present?
  self.class.url CGI.escape(id.to_s), *params
end