Class: Dynaccount::DynaccountObject
- Inherits:
-
Object
- Object
- Dynaccount::DynaccountObject
show all
- Defined in:
- lib/dynaccount/dynaccount_object.rb
Direct Known Subclasses
Account, Accounting, AccountingBank, Bank, BankRecon, Contact, ContactUser, Creditor, CreditorAccountingModule, CreditorGroup, CreditorInvoice, CreditorInvoiceMatch, CreditorInvoiceProduct, CreditorOrder, CreditorOrderProduct, CreditorOrderProductSerialno, Currency, DataVoucher, Debtor, DebtorAccountingModule, DebtorCreditnote, DebtorCreditnoteProduct, DebtorCreditnoteProductList, DebtorGroup, DebtorGroupProductDiscount, DebtorGroupProductGroupDiscount, DebtorInvoice, DebtorInvoiceMatch, DebtorInvoiceProduct, DebtorInvoiceProductList, DebtorOrder, DebtorOrderProduct, DebtorOrderProductList, DebtorOrderProductSerialno, DebtorProductDiscount, DebtorProductGroupDiscount, DebtorQuote, DebtorQuoteProduct, DebtorQuoteProductList, DebtorReminder, DebtorSubscription, DebtorSubscriptionGroup, DebtorSubscriptionGroupProduct, DebtorSubscriptionGroupProductList, DebtorSubscriptionProduct, DebtorSubscriptionProductList, Dimension, Draft, Enclosure, Followup, Location, Opportunity, Payment, Product, ProductCurrency, ProductGroup, ProductList, ProductListProduct, ProductSupplier, StockAdjustment, Unit, Vatcode, Year, YearPeriod
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of DynaccountObject.
5
6
7
8
9
10
11
12
|
# File 'lib/dynaccount/dynaccount_object.rb', line 5
def initialize(attributes)
@keys = attributes.keys
@values = attributes
attributes.each do |k, v|
singleton_class.send(:attr_accessor, k.to_sym)
send("#{k}=", v)
end
end
|
Instance Attribute Details
#values ⇒ Object
Returns the value of attribute values.
3
4
5
|
# File 'lib/dynaccount/dynaccount_object.rb', line 3
def values
@values
end
|
Class Method Details
.all ⇒ Object
39
40
41
|
# File 'lib/dynaccount/dynaccount_object.rb', line 39
def self.all
find(nil) || []
end
|
.create(attributes = {}) ⇒ Object
34
35
36
37
|
# File 'lib/dynaccount/dynaccount_object.rb', line 34
def self.create(attributes = {})
req = JSON.parse(Dynaccount.request(url(nil, 'put'), attributes, :post).body).fetch('result', []).map { |res| new(res) }
req[0]
end
|
.find(id) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/dynaccount/dynaccount_object.rb', line 43
def self.find(id)
req = JSON.parse(Dynaccount.request(url(id, 'get'), {}, :post).body).fetch('result', []).map { |res| new(res) }
return req[0] if req.size == 1
req
rescue JSON::ParserError => _e
return nil
end
|
.find_by(params = {}) ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/dynaccount/dynaccount_object.rb', line 51
def self.find_by(params = {})
req = JSON.parse(Dynaccount.request(url(nil, 'get', params), {}, :post).body).fetch('result', []).map { |res| new(res) }
return req[0] if req.size == 1
req
rescue JSON::ParserError => _e
return nil
end
|
.query ⇒ Object
73
74
75
|
# File 'lib/dynaccount/dynaccount_object.rb', line 73
def self.query
Dynaccount::QueryBuilder.new(self)
end
|
.run_query(select: [], where: {}, limit: nil, offset: nil, order: []) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/dynaccount/dynaccount_object.rb', line 59
def self.run_query(select: [], where: {}, limit: nil, offset: nil, order: [])
params = {}
params.merge!(select: select.join('%2C')) if select&.any?
params.merge!(where.tap {|w| w&.delete(:select) }) if where
params.merge!(order: "#{order[0]}+#{order[1]}") if order&.count == 2
params.merge!(limit: "#{[offset, limit].select {|i| i.is_a?(Integer) }.join('%2C')}") if [offset, limit].select {|i| i.is_a?(Integer) }.any?
JSON.parse(
Dynaccount.request(url(nil, 'get', params), {}, :post).body
).fetch('result', []).map { |res| new(res) }
rescue JSON::ParserError => _e
return nil
end
|
.url(id, action, params = {}) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/dynaccount/dynaccount_object.rb', line 77
def self.url(id, action, params = {})
url = "/v6/#{Dynaccount.api_id}/#{Dynaccount.api_key}/#{action}/#{api_path}/#{"#{id}/" unless id.nil?}"
url += "?" + params.map do |k,v|
"#{k}=#{v.to_s.gsub(/[^a-zA-Z0-9_\-.]/n) { sprintf("%%%02X", $&.unpack("C")[0]) }.encode('utf-8')}"
end.sort.join('&') if params.any?
url
end
|
Instance Method Details
#destroy ⇒ Object
23
24
25
|
# File 'lib/dynaccount/dynaccount_object.rb', line 23
def destroy
Dynaccount.request(self.class.url(id, 'delete'), {}, :post).body
end
|
#marshal_dump ⇒ Object
85
86
87
|
# File 'lib/dynaccount/dynaccount_object.rb', line 85
def marshal_dump
values
end
|
#marshal_load(val) ⇒ Object
89
90
91
|
# File 'lib/dynaccount/dynaccount_object.rb', line 89
def marshal_load(val)
initialize(val)
end
|
#save ⇒ Object
27
28
29
30
31
32
|
# File 'lib/dynaccount/dynaccount_object.rb', line 27
def save
updt = @keys.select { |k| !self.class.ignore_put.include?(k.to_sym) }
.map { |k| [k, send(k)] }
.to_h
Dynaccount.request(self.class.url(id, 'put'), updt, :post).body
end
|
#update(attributes = {}) ⇒ Object
14
15
16
17
18
19
20
21
|
# File 'lib/dynaccount/dynaccount_object.rb', line 14
def update(attributes = {})
return true if attributes.empty?
updt = @keys.select { |k| !self.class.ignore_put.include?(k.to_sym) }
.map { |k| [k, send(k)] }
.to_h
attributes.merge!(updt)
Dynaccount.request(self.class.url(id, 'put'), attributes, :post).body
end
|