Class: Magento::Model
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Includes:
- ModelParser
- Defined in:
- lib/magento/model.rb
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included, #to_h
Class Attribute Details
.primary_key ⇒ Object
71
72
73
|
# File 'lib/magento/model.rb', line 71
def primary_key
@primary_key || :id
end
|
Class Method Details
.api_resource ⇒ Object
63
64
65
|
# File 'lib/magento/model.rb', line 63
def api_resource
endpoint || Magento.inflector.pluralize(entity_name)
end
|
.create(attributes) ⇒ Object
46
47
48
49
50
|
# File 'lib/magento/model.rb', line 46
def create(attributes)
body = { entity_key => attributes }
hash = request.post(api_resource, body).parse
build(hash)
end
|
.delete(id) ⇒ Object
52
53
54
|
# File 'lib/magento/model.rb', line 52
def delete(id)
request.delete("#{api_resource}/#{id}").status.success?
end
|
.entity_name ⇒ Object
67
68
69
|
# File 'lib/magento/model.rb', line 67
def entity_name
Magento.inflector.underscore(name).sub('magento/', '')
end
|
.find(id) ⇒ Object
41
42
43
44
|
# File 'lib/magento/model.rb', line 41
def find(id)
hash = request.get("#{api_resource}/#{id}").parse
build(hash)
end
|
.update(id, attributes) ⇒ Object
56
57
58
59
60
61
|
# File 'lib/magento/model.rb', line 56
def update(id, attributes)
body = { entity_key => attributes }
hash = request.put("#{api_resource}/#{id}", body).parse
block_given? ? yield(hash) : build(hash)
end
|
Instance Method Details
#delete ⇒ Object
23
24
25
|
# File 'lib/magento/model.rb', line 23
def delete
self.class.delete(send(self.class.primary_key))
end
|
#id ⇒ Object
27
28
29
|
# File 'lib/magento/model.rb', line 27
def id
@id || send(self.class.primary_key)
end
|
#save ⇒ Object
9
10
11
12
13
|
# File 'lib/magento/model.rb', line 9
def save
self.class.update(send(self.class.primary_key), to_h) do |hash|
update_attributes(hash)
end
end
|
#update(attrs) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/magento/model.rb', line 15
def update(attrs)
raise "#{self.class.name} not saved" if send(self.class.primary_key).nil?
self.class.update(send(self.class.primary_key), attrs) do |hash|
update_attributes(hash)
end
end
|