Module: Prestashop::Mapper::Extension::ClassMethods
- Defined in:
- lib/prestashop/mapper/extension.rb
Instance Method Summary collapse
-
#all(options = {}) ⇒ Object
Get models all results by class resource, you can specifi what you should to see as result by specifiyng
:display. -
#destroy(id) ⇒ Object
Destroy model by class resource and given id.
-
#exists?(id) ⇒ Boolean
Determinate if model with class resource exists with given id.
-
#find(id) ⇒ Object
Find model by class resource and given id, returns hash with all nodes, based on node name as key, node value as value.
-
#find_by(options = {}) ⇒ Object
Find model by class resource and params in hash Returns first result, see #where for more informations.
-
#update(id, options = {}) ⇒ Object
Update model, with class resource by
idand given updates. -
#update_hash(id, options = {}) ⇒ Object
Create hash suitable for update, contains #fixed_hash as hash with deleted keys, which shouldn’t be in payload, if exist.
-
#update_payload(id, options = {}) ⇒ Object
Create payload for update, converts hash to XML.
-
#where(options = {}) ⇒ Object
Get results by class resource and given conditionals.
Instance Method Details
#all(options = {}) ⇒ Object
Get models all results by class resource, you can specifi what you should to see as result by specifiyng :display
Car.all # => [1,2,3]
Car.all(display: ['name']) # => [{ name: { language: { attr: { id: 2, href: 'http://localhost.com/api/languages/2'}, val: 'BMW 7'} }]
42 43 44 45 46 47 48 49 |
# File 'lib/prestashop/mapper/extension.rb', line 42 def all = {} result = if [:display] Client.read self.resource, nil, display: [:display] else Client.read self.resource end handle_result result, end |
#destroy(id) ⇒ Object
Destroy model by class resource and given id
Car.destroy(1) # => true
64 65 66 |
# File 'lib/prestashop/mapper/extension.rb', line 64 def destroy id Client.delete self.resource, id end |
#exists?(id) ⇒ Boolean
Determinate if model with class resource exists with given id
Car.exists?(1) # => true # if given car exist
Car.exists?(2) # => false # if given car don't exist
11 12 13 |
# File 'lib/prestashop/mapper/extension.rb', line 11 def exists? id Client.check self.resource, id end |
#find(id) ⇒ Object
Find model by class resource and given id, returns hash with all nodes, based on node name as key, node value as value
Car.find(1) # => { id: 1, name: 'BMW' }
Car.find(2) # => nil
21 22 23 24 |
# File 'lib/prestashop/mapper/extension.rb', line 21 def find id result = Client.read self.resource, id result ? result[self.model] : nil end |
#find_by(options = {}) ⇒ Object
Find model by class resource and params in hash Returns first result, see #where for more informations
Car.find_by(name: 'BMW') # => 1
31 32 33 34 |
# File 'lib/prestashop/mapper/extension.rb', line 31 def find_by = {} results = where() results ? results.first : nil end |
#update(id, options = {}) ⇒ Object
Update model, with class resource by id and given updates
Car.update(1, name: 'BMW 7') # => {id: 1, name: 'BMW 7'}
90 91 92 93 |
# File 'lib/prestashop/mapper/extension.rb', line 90 def update id, = {} result = Client.update self.resource, id, update_payload(id, ) result ? result[self.model] : nil end |
#update_hash(id, options = {}) ⇒ Object
Create hash suitable for update, contains #fixed_hash as hash with deleted keys, which shouldn’t be in payload, if exist
Car.update_hash(1, name: 'BMW7') # => {name: 'BMW7', manufacturer: 'BMW'}
73 74 75 76 |
# File 'lib/prestashop/mapper/extension.rb', line 73 def update_hash id, = {} original = defined?(fixed_hash(nil)) ? fixed_hash(id) : find(id) original.merge() end |
#update_payload(id, options = {}) ⇒ Object
Create payload for update, converts hash to XML
Car.update_payload(1, name: 'BMW 7') # => <prestashop xmlns:xlink="http://www.w3.org/1999/xlink"><car><name><![CDATA[BMW 7]]></name></car></prestashop>
82 83 84 |
# File 'lib/prestashop/mapper/extension.rb', line 82 def update_payload id, = {} Api::Converter.build(self.resource, self.model, update_hash(id, )) end |
#where(options = {}) ⇒ Object
Get results by class resource and given conditionals
Car.where('filter[id_supplier' => 1) # => [1, 2]
55 56 57 58 |
# File 'lib/prestashop/mapper/extension.rb', line 55 def where = {} result = Client.read self.resource, nil, handle_result result, end |