Class: Infuser::Tables::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers::Hashie
Defined in:
lib/infuser/tables/base.rb

Direct Known Subclasses

Company, Contact, Invoice, InvoiceItem, OrderItem

Constant Summary collapse

PAGINATION =
1000

Instance Method Summary collapse

Methods included from Helpers::Hashie

#camelize_hash, #safe_classify

Constructor Details

#initialize(client) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/infuser/tables/base.rb', line 8

def initialize client
  @client = client
  self
end

Instance Method Details

#allObject



52
53
54
# File 'lib/infuser/tables/base.rb', line 52

def all
  find_by id: '%'
end

#build(data) ⇒ Object



13
14
15
16
17
# File 'lib/infuser/tables/base.rb', line 13

def build data
  item = model_klass.new(data)
  item.instance_variable_set(:@client, client)
  item
end

#create(data) ⇒ Object



19
20
21
# File 'lib/infuser/tables/base.rb', line 19

def create data
  build(data).save
end

#find(id) ⇒ Object



23
24
25
26
27
28
# File 'lib/infuser/tables/base.rb', line 23

def find id
  item = model_klass.new
  item.instance_variable_set(:@id, id)
  item.instance_variable_set(:@client, client)
  item.load
end

#find_by(hash) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/infuser/tables/base.rb', line 30

def find_by hash
  page    = 0
  count   = PAGINATION
  records = []

  begin
    response = client.get("DataService.query", klass_name, PAGINATION, page, camelize_hash(hash), (model_klass.fieldset.dup << 'Id'))
    page += 1
    count = response.count

    records << response.map do |hash|
      item = model_klass.new
      item.instance_variable_set(:@client, client)
      item.instance_variable_set(:@id, hash['Id'])
      item.populate(hash.reject { |k, v| k == 'Id'})
      item
    end
  end while count == PAGINATION

  records.flatten
end