Class: Justa::Model

Inherits:
JustaObject show all
Defined in:
lib/justa/model.rb

Direct Known Subclasses

OrderCommom

Constant Summary

Constants inherited from JustaObject

JustaObject::RESOURCES

Instance Attribute Summary

Attributes inherited from JustaObject

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from JustaObject

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

Constructor Details

This class inherits a constructor from Justa::JustaObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Justa::JustaObject

Class Method Details

.class_nameObject



62
63
64
# File 'lib/justa/model.rb', line 62

def class_name
  name.split("::").last.downcase
end

.create(*args) ⇒ Object



33
34
35
# File 'lib/justa/model.rb', line 33

def create(*args)
  new(*args).create
end

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

Raises:



37
38
39
40
41
# File 'lib/justa/model.rb', line 37

def find_by_id(id, **options)
  raise RequestError, "Invalid ID" unless id.present?

  Justa::Request.get(url(id), options.merge({ append_document: false })).call underscored_class_name
end

.underscored_class_nameObject



66
67
68
# File 'lib/justa/model.rb', line 66

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

.url(*params) ⇒ Object

def all(*args, **params)

params = extract_page_count_or_params(*args, **params)
find_by params

end alias :where :all



58
59
60
# File 'lib/justa/model.rb', line 58

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

Instance Method Details

#class_nameObject



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

def class_name
  self.class.to_s.split("::").last
end

#createObject



3
4
5
6
# File 'lib/justa/model.rb', line 3

def create
  update Justa::Request.post(self.class.url, params: to_hash).call(class_name)
  self
end

#fetchObject



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

def fetch
  update self.class.find(primary_key, client_key: client_key)
  self
end

#primary_keyObject



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

def primary_key
  tx_id
end

#url(*params) ⇒ Object

def save

update Justa::Request.put(url, params: unsaved_attributes).call(class_name)
self

end

Raises:



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

def url(*params)
  raise RequestError, "Invalid ID" unless primary_key.present?

  self.class.url CGI.escape(primary_key.to_s), *params
end