cashctrl 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +21 -0
  3. data/README.md +831 -14
  4. data/cashctrl.gemspec +1 -1
  5. data/lib/cashctrl/client.rb +219 -0
  6. data/lib/cashctrl/collection.rb +78 -0
  7. data/lib/cashctrl/configuration.rb +30 -0
  8. data/lib/cashctrl/error.rb +29 -0
  9. data/lib/cashctrl/errors.rb +76 -0
  10. data/lib/cashctrl/localized_string.rb +115 -0
  11. data/lib/cashctrl/models/account.rb +39 -0
  12. data/lib/cashctrl/models/article.rb +48 -0
  13. data/lib/cashctrl/models/asset.rb +37 -0
  14. data/lib/cashctrl/models/attachment.rb +14 -0
  15. data/lib/cashctrl/models/bank.rb +18 -0
  16. data/lib/cashctrl/models/category.rb +13 -0
  17. data/lib/cashctrl/models/certificate_document.rb +14 -0
  18. data/lib/cashctrl/models/certificate_template.rb +13 -0
  19. data/lib/cashctrl/models/cost_center.rb +17 -0
  20. data/lib/cashctrl/models/currency.rb +18 -0
  21. data/lib/cashctrl/models/custom_field.rb +18 -0
  22. data/lib/cashctrl/models/custom_field_group.rb +12 -0
  23. data/lib/cashctrl/models/file.rb +20 -0
  24. data/lib/cashctrl/models/fiscal_period.rb +16 -0
  25. data/lib/cashctrl/models/fiscal_period_task.rb +14 -0
  26. data/lib/cashctrl/models/history.rb +19 -0
  27. data/lib/cashctrl/models/insurance_type.rb +13 -0
  28. data/lib/cashctrl/models/journal.rb +65 -0
  29. data/lib/cashctrl/models/journal_import.rb +18 -0
  30. data/lib/cashctrl/models/journal_import_entry.rb +20 -0
  31. data/lib/cashctrl/models/journal_item.rb +20 -0
  32. data/lib/cashctrl/models/loader.rb +55 -0
  33. data/lib/cashctrl/models/location.rb +22 -0
  34. data/lib/cashctrl/models/order.rb +53 -0
  35. data/lib/cashctrl/models/order_book_entry.rb +17 -0
  36. data/lib/cashctrl/models/order_document.rb +15 -0
  37. data/lib/cashctrl/models/order_item.rb +23 -0
  38. data/lib/cashctrl/models/order_layout.rb +18 -0
  39. data/lib/cashctrl/models/order_payment.rb +16 -0
  40. data/lib/cashctrl/models/person.rb +78 -0
  41. data/lib/cashctrl/models/report.rb +15 -0
  42. data/lib/cashctrl/models/report_collection.rb +13 -0
  43. data/lib/cashctrl/models/report_element.rb +16 -0
  44. data/lib/cashctrl/models/rounding.rb +16 -0
  45. data/lib/cashctrl/models/salary_book_entry.rb +16 -0
  46. data/lib/cashctrl/models/salary_certificate.rb +16 -0
  47. data/lib/cashctrl/models/salary_document.rb +15 -0
  48. data/lib/cashctrl/models/salary_field.rb +16 -0
  49. data/lib/cashctrl/models/salary_layout.rb +13 -0
  50. data/lib/cashctrl/models/salary_payment.rb +16 -0
  51. data/lib/cashctrl/models/salary_setting.rb +12 -0
  52. data/lib/cashctrl/models/salary_statement.rb +19 -0
  53. data/lib/cashctrl/models/salary_status.rb +12 -0
  54. data/lib/cashctrl/models/salary_sum.rb +14 -0
  55. data/lib/cashctrl/models/salary_template.rb +12 -0
  56. data/lib/cashctrl/models/salary_type.rb +14 -0
  57. data/lib/cashctrl/models/sequence_number.rb +13 -0
  58. data/lib/cashctrl/models/setting.rb +33 -0
  59. data/lib/cashctrl/models/tax.rb +18 -0
  60. data/lib/cashctrl/models/text.rb +13 -0
  61. data/lib/cashctrl/models/title.rb +13 -0
  62. data/lib/cashctrl/models/unit.rb +11 -0
  63. data/lib/cashctrl/relation.rb +271 -0
  64. data/lib/cashctrl/resource.rb +433 -0
  65. data/lib/cashctrl/resources/account/bank.rb +14 -0
  66. data/lib/cashctrl/resources/account/category.rb +16 -0
  67. data/lib/cashctrl/resources/account/cost_center/category.rb +18 -0
  68. data/lib/cashctrl/resources/account/cost_center.rb +26 -0
  69. data/lib/cashctrl/resources/account.rb +28 -0
  70. data/lib/cashctrl/resources/attachments.rb +32 -0
  71. data/lib/cashctrl/resources/base.rb +237 -0
  72. data/lib/cashctrl/resources/currency.rb +17 -0
  73. data/lib/cashctrl/resources/custom_field/group.rb +16 -0
  74. data/lib/cashctrl/resources/custom_field.rb +18 -0
  75. data/lib/cashctrl/resources/file/category.rb +16 -0
  76. data/lib/cashctrl/resources/file.rb +82 -0
  77. data/lib/cashctrl/resources/fiscal_period/task.rb +20 -0
  78. data/lib/cashctrl/resources/fiscal_period.rb +54 -0
  79. data/lib/cashctrl/resources/history.rb +26 -0
  80. data/lib/cashctrl/resources/import_session.rb +43 -0
  81. data/lib/cashctrl/resources/inventory/article/category.rb +18 -0
  82. data/lib/cashctrl/resources/inventory/article/import.rb +13 -0
  83. data/lib/cashctrl/resources/inventory/article.rb +19 -0
  84. data/lib/cashctrl/resources/inventory/asset/category.rb +18 -0
  85. data/lib/cashctrl/resources/inventory/asset.rb +19 -0
  86. data/lib/cashctrl/resources/inventory/unit.rb +12 -0
  87. data/lib/cashctrl/resources/journal/import/entry.rb +33 -0
  88. data/lib/cashctrl/resources/journal/import.rb +16 -0
  89. data/lib/cashctrl/resources/journal.rb +56 -0
  90. data/lib/cashctrl/resources/location.rb +10 -0
  91. data/lib/cashctrl/resources/order/book_entry.rb +12 -0
  92. data/lib/cashctrl/resources/order/category.rb +21 -0
  93. data/lib/cashctrl/resources/order/document.rb +30 -0
  94. data/lib/cashctrl/resources/order/layout.rb +12 -0
  95. data/lib/cashctrl/resources/order/payment.rb +36 -0
  96. data/lib/cashctrl/resources/order.rb +47 -0
  97. data/lib/cashctrl/resources/organization.rb +13 -0
  98. data/lib/cashctrl/resources/person/category.rb +16 -0
  99. data/lib/cashctrl/resources/person/import.rb +11 -0
  100. data/lib/cashctrl/resources/person/title.rb +12 -0
  101. data/lib/cashctrl/resources/person.rb +21 -0
  102. data/lib/cashctrl/resources/report/collection.rb +40 -0
  103. data/lib/cashctrl/resources/report/element.rb +48 -0
  104. data/lib/cashctrl/resources/report.rb +34 -0
  105. data/lib/cashctrl/resources/rounding.rb +10 -0
  106. data/lib/cashctrl/resources/salary/book_entry.rb +12 -0
  107. data/lib/cashctrl/resources/salary/category.rb +16 -0
  108. data/lib/cashctrl/resources/salary/certificate/document.rb +47 -0
  109. data/lib/cashctrl/resources/salary/certificate/template.rb +18 -0
  110. data/lib/cashctrl/resources/salary/certificate.rb +20 -0
  111. data/lib/cashctrl/resources/salary/document.rb +42 -0
  112. data/lib/cashctrl/resources/salary/field.rb +24 -0
  113. data/lib/cashctrl/resources/salary/insurance/type.rb +14 -0
  114. data/lib/cashctrl/resources/salary/layout.rb +12 -0
  115. data/lib/cashctrl/resources/salary/payment.rb +36 -0
  116. data/lib/cashctrl/resources/salary/setting.rb +12 -0
  117. data/lib/cashctrl/resources/salary/statement.rb +42 -0
  118. data/lib/cashctrl/resources/salary/status.rb +16 -0
  119. data/lib/cashctrl/resources/salary/sum.rb +12 -0
  120. data/lib/cashctrl/resources/salary/template.rb +16 -0
  121. data/lib/cashctrl/resources/salary/type.rb +17 -0
  122. data/lib/cashctrl/resources/sequence_number.rb +14 -0
  123. data/lib/cashctrl/resources/setting.rb +34 -0
  124. data/lib/cashctrl/resources/tax.rb +10 -0
  125. data/lib/cashctrl/resources/text.rb +10 -0
  126. data/lib/cashctrl/schema.yml +712 -0
  127. data/lib/cashctrl/version.rb +2 -2
  128. data/lib/cashctrl.rb +125 -3
  129. data/lib/generators/model_generator.rb +104 -0
  130. data/sig/cashctrl.rbs +1 -1
  131. metadata +126 -2
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Report < Resource
8
+ attribute :name, type: :string
9
+ attribute :type, type: :string
10
+ attribute :data, type: :json
11
+ attribute :columns, type: :json
12
+ attribute :has_children, type: :boolean, readonly: true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class ReportCollection < Resource
8
+ attribute :name, type: :string
9
+ attribute :fiscal_period_id, type: :integer
10
+ attribute :elements, type: :json
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class ReportElement < Resource
8
+ attribute :collection_id, type: :integer
9
+ attribute :report_id, type: :integer
10
+ attribute :name, type: :string
11
+ attribute :type, type: :string
12
+ attribute :params, type: :json
13
+ attribute :sequence, type: :integer
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Rounding < Resource
8
+ attribute :name, type: :string
9
+ attribute :account_id, type: :integer
10
+ attribute :account_name, type: :string, readonly: true
11
+ attribute :mode, type: :string
12
+ attribute :value, type: :decimal
13
+ belongs_to :account, resource: "Account"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryBookEntry < Resource
8
+ attribute :salary_id, type: :integer
9
+ attribute :statement_id, type: :integer
10
+ attribute :account_id, type: :integer
11
+ attribute :account_name, type: :string, readonly: true
12
+ attribute :description, type: :string
13
+ attribute :amount, type: :decimal
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryCertificate < Resource
8
+ attribute :person_id, type: :integer
9
+ attribute :person_name, type: :string, readonly: true
10
+ attribute :template_id, type: :integer
11
+ attribute :year, type: :integer
12
+ attribute :values, type: :json
13
+ belongs_to :person, resource: "Person"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryDocument < Resource
8
+ attribute :salary_id, type: :integer
9
+ attribute :statement_id, type: :integer
10
+ attribute :layout_id, type: :integer
11
+ attribute :name, type: :string
12
+ attribute :file_id, type: :integer, readonly: true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryField < Resource
8
+ attribute :type_id, type: :integer
9
+ attribute :name, type: :string
10
+ attribute :description, type: :string
11
+ attribute :calc_formula, type: :string
12
+ attribute :sequence, type: :integer
13
+ attribute :is_visible, type: :boolean
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryLayout < Resource
8
+ attribute :type, type: :string
9
+ attribute :name, type: :string
10
+ attribute :is_default, type: :boolean
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryPayment < Resource
8
+ attribute :statement_id, type: :integer
9
+ attribute :account_id, type: :integer
10
+ attribute :account_name, type: :string, readonly: true
11
+ attribute :date, type: :date
12
+ attribute :amount, type: :decimal
13
+ attribute :notes, type: :string
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalarySetting < Resource
8
+ attribute :name, type: :string
9
+ attribute :value, type: :string
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryStatement < Resource
8
+ attribute :person_id, type: :integer
9
+ attribute :person_name, type: :string, readonly: true
10
+ attribute :period_start, type: :date
11
+ attribute :period_end, type: :date
12
+ attribute :status, type: :string
13
+ attribute :total_gross, type: :decimal, readonly: true
14
+ attribute :total_net, type: :decimal, readonly: true
15
+ belongs_to :person, resource: "Person"
16
+ nested :attachments, model: "Attachment"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryStatus < Resource
8
+ attribute :name, type: :string
9
+ attribute :sequence, type: :integer
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalarySum < Resource
8
+ attribute :type_id, type: :integer
9
+ attribute :name, type: :string
10
+ attribute :description, type: :string
11
+ attribute :sequence, type: :integer
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryTemplate < Resource
8
+ attribute :name, type: :string
9
+ attribute :description, type: :string
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SalaryType < Resource
8
+ attribute :name, type: :string
9
+ attribute :description, type: :string
10
+ attribute :account_id, type: :integer
11
+ attribute :account_name, type: :string, readonly: true
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class SequenceNumber < Resource
8
+ attribute :name, type: :string
9
+ attribute :pattern, type: :string
10
+ attribute :counter, type: :integer
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Setting < Resource
8
+ attribute :fiscal_period_id, type: :integer
9
+ attribute :fiscal_period_name, type: :string, readonly: true
10
+ attribute :default_currency_id, type: :integer
11
+ attribute :default_currency_code, type: :string, readonly: true
12
+ attribute :default_location_id, type: :integer
13
+ attribute :default_location_name, type: :string, readonly: true
14
+ attribute :default_debit_id, type: :integer
15
+ attribute :default_debit_name, type: :string, readonly: true
16
+ attribute :default_credit_id, type: :integer
17
+ attribute :default_credit_name, type: :string, readonly: true
18
+ attribute :default_opening_id, type: :integer
19
+ attribute :default_opening_name, type: :string, readonly: true
20
+ attribute :default_tax_id, type: :integer
21
+ attribute :default_tax_name, type: :string, readonly: true
22
+ attribute :default_order_category_id, type: :integer
23
+ attribute :default_person_category_customer_id, type: :integer
24
+ attribute :default_person_category_vendor_id, type: :integer
25
+ attribute :logo_file_id, type: :integer
26
+ attribute :csv_delimiter, type: :string
27
+ attribute :journal_import_duplicate_check, type: :string
28
+ attribute :journal_import_force_duplicate_detection, type: :boolean
29
+ attribute :tax_accounting_method, type: :string
30
+ attribute :tax_type, type: :string
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Tax < Resource
8
+ attribute :name, type: :string
9
+ attribute :percentage, type: :decimal
10
+ attribute :document_name, type: :string
11
+ attribute :calc_type, type: :string
12
+ attribute :account_id, type: :integer
13
+ attribute :account_name, type: :string, readonly: true
14
+ attribute :is_inactive, type: :boolean
15
+ belongs_to :account, resource: "Account"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Text < Resource
8
+ attribute :type, type: :string
9
+ attribute :name, type: :string
10
+ attribute :value, type: :string
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Title < Resource
8
+ attribute :name, type: :string
9
+ attribute :sentence, type: :string
10
+ attribute :gender, type: :string
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Unit < Resource
8
+ attribute :name, type: :string
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,271 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ class Relation
5
+ include Enumerable
6
+
7
+ LOOKUP_RESOURCES = ["Account", "Tax", "Currency", "Inventory::Unit"].freeze
8
+
9
+ attr_reader :resource, :params, :filters, :sort_value, :dir_value, :limit_value, :offset_value, :includes_values
10
+
11
+ def initialize(resource, params: {}, filters: [], sort: nil, dir: nil, limit: nil, offset: nil, includes: [])
12
+ @resource = resource
13
+ @params = params
14
+ @filters = filters
15
+ @sort_value = sort
16
+ @dir_value = dir
17
+ @limit_value = limit
18
+ @offset_value = offset
19
+ @includes_values = includes
20
+ @records = nil
21
+ end
22
+
23
+ def where(conditions = {}, **kwargs)
24
+ conditions = resolve_associations(conditions.merge(kwargs))
25
+ raw_filter = conditions.delete(:filter) || conditions.delete("filter")
26
+ list_params, field_filters = conditions.partition { |key, _value| resource.list_param?(key) }.map(&:to_h)
27
+
28
+ spawn(
29
+ params: params.merge(list_params.transform_keys(&:to_sym)),
30
+ filters: filters + filters_for(field_filters) + filters_for(raw_filter)
31
+ )
32
+ end
33
+
34
+ def order(*fields, **directions)
35
+ field, direction = fields.any? ? [fields.first, :asc] : directions.first
36
+ raise ArgumentError, "order needs a field" unless field
37
+
38
+ spawn(sort: camelize(field), dir: direction.to_s.upcase)
39
+ end
40
+
41
+ def includes(*names)
42
+ names.each do |name|
43
+ next if resource.model_class.associations.key?(name.to_sym)
44
+
45
+ raise ArgumentError, "#{resource.model_class.name} has no association #{name}"
46
+ end
47
+
48
+ spawn(includes: includes_values | names.map(&:to_sym))
49
+ end
50
+
51
+ def limit(value)
52
+ spawn(limit: value)
53
+ end
54
+
55
+ def offset(value)
56
+ spawn(offset: value)
57
+ end
58
+
59
+ def to_a
60
+ load
61
+ @records.dup
62
+ end
63
+
64
+ alias to_ary to_a
65
+
66
+ def each(&)
67
+ return enum_for(:each) unless block_given?
68
+
69
+ load
70
+ @records.each(&)
71
+ end
72
+
73
+ def load
74
+ @records ||= fetch_records
75
+ self
76
+ end
77
+
78
+ def reload
79
+ @records = nil
80
+ load
81
+ end
82
+
83
+ def loaded?
84
+ !@records.nil?
85
+ end
86
+
87
+ def first(count = nil)
88
+ records = loaded? ? @records.first(count || 1) : limit(count || 1).to_a
89
+ count ? records : records.first
90
+ end
91
+
92
+ def last(count = nil)
93
+ count ? to_a.last(count) : to_a.last
94
+ end
95
+
96
+ def count
97
+ return @records.size if loaded?
98
+
99
+ total = resource.list(**request_params(start: offset_value || 0, limit: 1)).total.to_i
100
+ total -= offset_value.to_i
101
+ total = [total, limit_value].min if limit_value
102
+ [total, 0].max
103
+ end
104
+
105
+ alias size count
106
+
107
+ def empty?
108
+ count.zero?
109
+ end
110
+
111
+ def exists?(conditions = nil)
112
+ relation = case conditions
113
+ when nil then self
114
+ when Hash then where(conditions)
115
+ else where(id: conditions)
116
+ end
117
+
118
+ !relation.empty?
119
+ end
120
+
121
+ def pluck(*attributes)
122
+ map do |record|
123
+ values = attributes.map { |attribute| record[attribute] }
124
+ attributes.one? ? values.first : values
125
+ end
126
+ end
127
+
128
+ def find(*ids, &block)
129
+ return super(&block) if block
130
+
131
+ resource.find(*ids)
132
+ end
133
+
134
+ def find_by(conditions = {}, **)
135
+ where(conditions, **).first
136
+ end
137
+
138
+ def find_by!(conditions = {}, **kwargs)
139
+ find_by(conditions, **kwargs) || raise(NotFoundError, "#{resource.class.name} not found for #{conditions.merge(kwargs)}")
140
+ end
141
+
142
+ def in_batches(of: Resources::Base::PAGE_SIZE)
143
+ return enum_for(:in_batches, of: of) unless block_given?
144
+
145
+ start = offset_value || 0
146
+ remaining = limit_value
147
+
148
+ loop do
149
+ size = remaining ? [of, remaining].min : of
150
+ break if size <= 0
151
+
152
+ batch = resource.list(**request_params(start: start, limit: size))
153
+ yield preload(batch.to_a) unless batch.empty?
154
+
155
+ start += batch.size
156
+ remaining -= batch.size if remaining
157
+ break if batch.size < size || (batch.total && start >= batch.total.to_i)
158
+ end
159
+ end
160
+
161
+ alias find_in_batches in_batches
162
+
163
+ def find_each(batch_size: Resources::Base::PAGE_SIZE, &block)
164
+ return enum_for(:find_each, batch_size: batch_size) unless block
165
+
166
+ in_batches(of: batch_size) { |batch| batch.each(&block) }
167
+ end
168
+
169
+ def delete_all
170
+ ids = pluck(:id)
171
+ ids.empty? ? nil : resource.delete(ids)
172
+ end
173
+
174
+ def destroy_all
175
+ to_a.each(&:destroy)
176
+ end
177
+
178
+ def request_params(start: nil, limit: nil)
179
+ request = params.dup
180
+ request[:filter] = filters if filters.any?
181
+ request[:sort] = sort_value if sort_value
182
+ request[:dir] = dir_value if dir_value
183
+ request[:start] = start if start
184
+ request[:limit] = limit if limit
185
+ request
186
+ end
187
+
188
+ def inspect
189
+ "#<#{self.class.name} #{resource.class.name} #{request_params.inspect}>"
190
+ end
191
+
192
+ private
193
+
194
+ def spawn(**changes)
195
+ self.class.new(
196
+ resource,
197
+ params: changes.fetch(:params, params),
198
+ filters: changes.fetch(:filters, filters),
199
+ sort: changes.fetch(:sort, sort_value),
200
+ dir: changes.fetch(:dir, dir_value),
201
+ limit: changes.fetch(:limit, limit_value),
202
+ offset: changes.fetch(:offset, offset_value),
203
+ includes: changes.fetch(:includes, includes_values)
204
+ )
205
+ end
206
+
207
+ def fetch_records
208
+ return preload(resource.list(**request_params).to_a) unless resource.pageable?
209
+
210
+ records = []
211
+ in_batches { |batch| records.concat(batch) }
212
+ records
213
+ end
214
+
215
+ def preload(records)
216
+ lookups = {}
217
+
218
+ includes_values.each do |name|
219
+ options = resource.model_class.associations[name]
220
+ ids = records.map { |record| record[options[:foreign_key]].to_i }.reject(&:zero?).uniq
221
+ next if ids.empty?
222
+
223
+ found = associated_records(options[:resource], ids, lookups).to_h { |record| [record.id, record] }
224
+ records.each { |record| record.preload_association(name, found[record[options[:foreign_key]].to_i]) }
225
+ end
226
+
227
+ records
228
+ end
229
+
230
+ def associated_records(resource_name, ids, lookups)
231
+ target = Resources.const_get(resource_name).with_client(resource.client)
232
+
233
+ if lookup?(resource_name)
234
+ lookups[resource_name] ||= target.all.to_a
235
+ return lookups[resource_name].select { |record| ids.include?(record.id) }
236
+ end
237
+
238
+ ids.filter_map do |id|
239
+ target.find(id)
240
+ rescue NotFoundError
241
+ nil
242
+ end
243
+ end
244
+
245
+ def lookup?(resource_name)
246
+ LOOKUP_RESOURCES.include?(resource_name) || resource_name.end_with?("Category")
247
+ end
248
+
249
+ def resolve_associations(conditions)
250
+ associations = resource.model_class.associations
251
+
252
+ conditions.to_h do |key, value|
253
+ association = associations[key.to_sym]
254
+ key = association[:foreign_key] if association
255
+ [key, value.is_a?(Resource) ? value.id : value]
256
+ end
257
+ end
258
+
259
+ def filters_for(conditions)
260
+ case conditions
261
+ when nil then []
262
+ when Array then conditions
263
+ else conditions.map { |field, value| { comparison: "eq", field: camelize(field), value: value } }
264
+ end
265
+ end
266
+
267
+ def camelize(field)
268
+ field.to_s.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
269
+ end
270
+ end
271
+ end