Class: CrisalidOdooClient::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/crisalid_odoo_client/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Query

Returns a new instance of Query.



4
5
6
7
8
9
# File 'lib/crisalid_odoo_client/query.rb', line 4

def initialize(client)
  @models = client.models
  @odoo_db = client.odoo_db
  @uid = client.uid
  @odoo_pass = client.odoo_pass
end

Instance Method Details

#create(table, params) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/crisalid_odoo_client/query.rb', line 39

def create(table, params)
  if params.is_a?(Hash) && params.size > 0
    @models.execute_kw(@odoo_db, @uid, @odoo_pass, table, 'create', [params])
  else
    raise CrisalidOdooClient::Error::OdooResourceManagerError.new('params must be a non empty Hash')
  end
end

#create_in_batch(table, headers, params) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/crisalid_odoo_client/query.rb', line 47

def create_in_batch(table, headers, params)
  if headers.is_a?(Array) && headers.size > 0
    if params.is_a?(Array) && params.size > 0 && params.first.is_a?(Array) && params.first.size == headers.size
      @models.execute_kw(@odoo_db, @uid, @odoo_pass, table, 'load', [headers, params])["ids"]
    else
      raise CrisalidOdooClient::Error::OdooResourceManagerError.new('params must be a non empty Array of Array who has the same size as headers')
    end
  else
    raise CrisalidOdooClient::Error::OdooResourceManagerError.new('headers must be a non empty Array')
  end
end

#destroy(table, ids) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/crisalid_odoo_client/query.rb', line 67

def destroy(table, ids)
  if ids.is_a?(Array) && ids.size > 0
    @models.execute_kw(@odoo_db, @uid, @odoo_pass, table, 'unlink', [ids])
  else
    raise CrisalidOdooClient::Error::OdooResourceManagerError.new('ids must be a non empty Array')
  end
end

#find(table, ids = [], params: {}, fields: [], first: false) ⇒ Object



25
26
27
28
29
30
# File 'lib/crisalid_odoo_client/query.rb', line 25

def find(table, ids = [], params: {}, fields: [], first: false)
  params[:limit]  = 1      if first
  params[:fields] = fields if fields.size > 0

  @models.execute_kw(@odoo_db, @uid, @odoo_pass, table, 'read', [ids], params)
end

#search(table, rules: [[]], params: {}) ⇒ Object



15
16
17
# File 'lib/crisalid_odoo_client/query.rb', line 15

def search(table, rules: [[]], params: {})
  @models.execute_kw(@odoo_db, @uid, @odoo_pass, table, 'search', rules, params)
end

#search_read(table, rules: [[]], params: {}, fields: []) ⇒ Object



19
20
21
22
23
# File 'lib/crisalid_odoo_client/query.rb', line 19

def search_read(table, rules: [[]], params: {}, fields: [])
  params[:fields] = fields if fields.size > 0

  @models.execute_kw(@odoo_db, @uid, @odoo_pass, table, 'search_read', rules, params)
end

#table_schema(table, attributes: []) ⇒ Object



11
12
13
# File 'lib/crisalid_odoo_client/query.rb', line 11

def table_schema(table, attributes: [])
  @models.execute_kw(@odoo_db, @uid, @odoo_pass, table, 'fields_get', [], {attributes: attributes})
end

#update(table, ids, params) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/crisalid_odoo_client/query.rb', line 59

def update(table, ids, params)
  if params.is_a?(Hash) && params.size > 0 && ids.is_a?(Array) && ids.size > 0
    @models.execute_kw(@odoo_db, @uid, @odoo_pass, table, 'write', [ids, params])
  else
    raise CrisalidOdooClient::Error::OdooResourceManagerError.new('params must be a non empty Hash and ids must be a non empty Array')
  end
end

#where(table, rules: [[]], params: {}, fields: [], first: false) ⇒ Object



32
33
34
35
36
37
# File 'lib/crisalid_odoo_client/query.rb', line 32

def where(table, rules: [[]], params: {}, fields: [], first: false)
  params[:limit]  = 1      if first
  params[:fields] = fields if fields.size > 0

  search_read(table, rules: rules, params: params)
end