module Prestashop::Mapper::Extension::ClassMethods

Public Instance Methods

all(options = {}) click to toggle source

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'} }]
# File lib/prestashop/mapper/extension.rb, line 42
def all options = {}
  result = if options[:display] 
    Client.read self.resource, nil, display: options[:display]
  else
    Client.read self.resource
  end
  handle_result result, options
end
destroy(id) click to toggle source

Destroy model by class resource and given id

Car.destroy(1) # => true
# File lib/prestashop/mapper/extension.rb, line 64
def destroy id
  Client.delete self.resource, id
end
exists?(id) click to toggle source

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
# File lib/prestashop/mapper/extension.rb, line 11
def exists? id
  Client.check self.resource, id
end
find(id) click to toggle source

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
# 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 = {}) click to toggle source

Find model by class resource and params in hash Returns first result, see where for more informations

Car.find_by(name: 'BMW') # => 1
# File lib/prestashop/mapper/extension.rb, line 31
def find_by options = {}
  results = where(options)
  results ? results.first : nil
end
update(id, options = {}) click to toggle source

Update model, with class resource by id and given updates

Car.update(1, name: 'BMW 7') # => {id: 1, name: 'BMW 7'}
# File lib/prestashop/mapper/extension.rb, line 90
def update id, options = {}
  result = Client.update self.resource, id, update_payload(id, options)
  result ? result[self.model] : nil
end
update_hash(id, options = {}) click to toggle source

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'}
# File lib/prestashop/mapper/extension.rb, line 73
def update_hash id, options = {}
  original = defined?(fixed_hash(nil)) ? fixed_hash(id) : find(id)
  original.merge(options)
end
update_payload(id, options = {}) click to toggle source

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>
# File lib/prestashop/mapper/extension.rb, line 82
def update_payload id, options = {}
  Api::Converter.build(self.resource, self.model, update_hash(id, options))
end
where(options = {}) click to toggle source

Get results by class resource and given conditionals

Car.where('filter[id_supplier' => 1) # => [1, 2]
# File lib/prestashop/mapper/extension.rb, line 55
def where options = {}
  result = Client.read self.resource, nil, options
  handle_result result, options
end

Private Instance Methods

handle_result(result, options = {}) click to toggle source

Handle result to return id or array with ids of requested objects

handle_result({ customers: { customer: [ 1,2 ] } }) # => [1, 2]
handle_result({ customers: { customer: { attr: { id: 1 }} } }) # => [1]
# File lib/prestashop/mapper/extension.rb, line 101
def handle_result result, options = {}
  if options[:display]
    if result[self.resource].kind_of?(Hash) and result[self.resource][self.model]
      objects = result[self.resource][self.model]
      objects.kind_of?(Array) ? objects : [objects]
    end
  else
    if result[self.resource].kind_of?(Hash) and result[self.resource][self.model]
      [objects = result[self.resource][self.model]]
      objects.kind_of?(Array) ? objects.map{ |o| o[:attr][:id] } : [ objects[:attr][:id] ]
    else
      nil
    end    
  end
end