ledger_sync 1.0.10 → 1.1.1
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +36 -20
- data/README.md +105 -66
- data/ledger_sync.gemspec +1 -0
- data/lib/ledger_sync.rb +23 -4
- data/lib/ledger_sync/adaptors/operation.rb +28 -28
- data/lib/ledger_sync/adaptors/quickbooks_online/account/mapping.rb +325 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/account/operations/create.rb +54 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/account/operations/find.rb +38 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/account/operations/update.rb +61 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/account/searcher.rb +67 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb +18 -16
- data/lib/ledger_sync/adaptors/quickbooks_online/bill/operations/create.rb +65 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/bill/operations/find.rb +37 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/bill/operations/update.rb +69 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/bill/searcher.rb +28 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/customer/operations/create.rb +6 -6
- data/lib/ledger_sync/adaptors/quickbooks_online/customer/operations/find.rb +1 -3
- data/lib/ledger_sync/adaptors/quickbooks_online/customer/operations/update.rb +6 -9
- data/lib/ledger_sync/adaptors/quickbooks_online/customer/searcher.rb +2 -38
- data/lib/ledger_sync/adaptors/quickbooks_online/deposit/operations/create.rb +61 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/deposit/operations/find.rb +36 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/deposit/operations/update.rb +65 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/deposit/searcher.rb +28 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/expense/mapping.rb +15 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/expense/operations/create.rb +68 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/expense/operations/find.rb +39 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/expense/operations/update.rb +72 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/expense/searcher.rb +28 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/mapping.rb +14 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/operations/create.rb +56 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/operations/find.rb +34 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/operations/update.rb +60 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/searcher.rb +28 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/operation.rb +30 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/payment/operations/create.rb +8 -22
- data/lib/ledger_sync/adaptors/quickbooks_online/payment/operations/find.rb +0 -2
- data/lib/ledger_sync/adaptors/quickbooks_online/payment/operations/update.rb +6 -22
- data/lib/ledger_sync/adaptors/quickbooks_online/searcher.rb +45 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/transfer/operations/create.rb +52 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/transfer/operations/find.rb +36 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/transfer/operations/update.rb +57 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/vendor/operations/create.rb +46 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/vendor/operations/find.rb +36 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/vendor/operations/update.rb +51 -0
- data/lib/ledger_sync/adaptors/quickbooks_online/vendor/searcher.rb +64 -0
- data/lib/ledger_sync/adaptors/searcher.rb +3 -1
- data/lib/ledger_sync/adaptors/test/account/operations/create.rb +53 -0
- data/lib/ledger_sync/adaptors/test/account/operations/find.rb +38 -0
- data/lib/ledger_sync/adaptors/test/account/operations/invalid.rb +25 -0
- data/lib/ledger_sync/adaptors/test/account/operations/update.rb +50 -0
- data/lib/ledger_sync/adaptors/test/account/operations/valid.rb +31 -0
- data/lib/ledger_sync/adaptors/test/account/searcher.rb +40 -0
- data/lib/ledger_sync/adaptors/test/adaptor.rb +4 -4
- data/lib/ledger_sync/adaptors/test/customer/operations/create.rb +4 -6
- data/lib/ledger_sync/adaptors/test/customer/operations/find.rb +0 -2
- data/lib/ledger_sync/adaptors/test/customer/operations/update.rb +4 -6
- data/lib/ledger_sync/adaptors/test/customer/searcher.rb +2 -2
- data/lib/ledger_sync/adaptors/test/expense/operations/create.rb +54 -0
- data/lib/ledger_sync/adaptors/test/expense/operations/find.rb +39 -0
- data/lib/ledger_sync/adaptors/test/expense/operations/update.rb +57 -0
- data/lib/ledger_sync/adaptors/test/payment/operations/create.rb +4 -20
- data/lib/ledger_sync/adaptors/test/payment/operations/find.rb +0 -2
- data/lib/ledger_sync/adaptors/test/payment/operations/update.rb +4 -20
- data/lib/ledger_sync/adaptors/test/transfer/operations/create.rb +45 -0
- data/lib/ledger_sync/adaptors/test/transfer/operations/find.rb +36 -0
- data/lib/ledger_sync/adaptors/test/transfer/operations/update.rb +48 -0
- data/lib/ledger_sync/adaptors/test/vendor/operations/create.rb +47 -0
- data/lib/ledger_sync/adaptors/test/vendor/operations/find.rb +34 -0
- data/lib/ledger_sync/adaptors/test/vendor/operations/invalid.rb +21 -0
- data/lib/ledger_sync/adaptors/test/vendor/operations/update.rb +44 -0
- data/lib/ledger_sync/adaptors/test/vendor/operations/valid.rb +27 -0
- data/lib/ledger_sync/adaptors/test/vendor/searcher.rb +41 -0
- data/lib/ledger_sync/error/resource_errors.rb +24 -0
- data/lib/ledger_sync/resource.rb +24 -64
- data/lib/ledger_sync/resource_attribute.rb +50 -0
- data/lib/ledger_sync/resource_attribute/dirty_mixin.rb +49 -0
- data/lib/ledger_sync/resource_attribute/mixin.rb +90 -0
- data/lib/ledger_sync/resource_attribute/reference.rb +9 -0
- data/lib/ledger_sync/resource_attribute/reference/many.rb +34 -0
- data/lib/ledger_sync/resource_attribute/reference/one.rb +33 -0
- data/lib/ledger_sync/resource_attribute_set.rb +58 -0
- data/lib/ledger_sync/resources/account.rb +14 -0
- data/lib/ledger_sync/resources/bill.rb +17 -0
- data/lib/ledger_sync/resources/bill_line_item.rb +11 -0
- data/lib/ledger_sync/resources/customer.rb +5 -4
- data/lib/ledger_sync/resources/deposit.rb +16 -0
- data/lib/ledger_sync/resources/deposit_line_item.rb +11 -0
- data/lib/ledger_sync/resources/expense.rb +19 -0
- data/lib/ledger_sync/resources/expense_line_item.rb +11 -0
- data/lib/ledger_sync/resources/journal_entry.rb +13 -0
- data/lib/ledger_sync/resources/journal_entry_line_item.rb +12 -0
- data/lib/ledger_sync/resources/payment.rb +5 -3
- data/lib/ledger_sync/resources/transfer.rb +15 -0
- data/lib/ledger_sync/resources/vendor.rb +12 -0
- data/lib/ledger_sync/result.rb +0 -24
- data/lib/ledger_sync/type/boolean.rb +17 -0
- data/lib/ledger_sync/type/date.rb +17 -0
- data/lib/ledger_sync/type/float.rb +17 -0
- data/lib/ledger_sync/type/integer.rb +17 -0
- data/lib/ledger_sync/type/reference_many.rb +27 -0
- data/lib/ledger_sync/type/reference_one.rb +26 -0
- data/lib/ledger_sync/type/string.rb +17 -0
- data/lib/ledger_sync/type/value.rb +12 -0
- data/lib/ledger_sync/type/value_mixin.rb +19 -0
- data/lib/ledger_sync/util/hash_helpers.rb +16 -1
- data/lib/ledger_sync/util/resources_builder.rb +36 -9
- data/lib/ledger_sync/version.rb +1 -1
- metadata +92 -7
- data/lib/ledger_sync/adaptors/quickbooks_online/customer/operations/upsert.rb +0 -42
- data/lib/ledger_sync/adaptors/quickbooks_online/payment/operations/upsert.rb +0 -53
- data/lib/ledger_sync/adaptors/test/customer/operations/upsert.rb +0 -42
- data/lib/ledger_sync/adaptors/test/payment/operations/upsert.rb +0 -53
- data/lib/ledger_sync/sync.rb +0 -108
- data/lib/ledger_sync/util/coordinator.rb +0 -72
@@ -18,40 +18,24 @@ module LedgerSync
|
|
18
18
|
|
19
19
|
private
|
20
20
|
|
21
|
-
def build
|
22
|
-
build_customer_operation
|
23
|
-
add_root_operation(self)
|
24
|
-
end
|
25
|
-
|
26
21
|
def operate
|
27
22
|
ledger_resource_data = adaptor.find(
|
28
23
|
resource: 'payment',
|
29
24
|
id: resource.ledger_id
|
30
25
|
)
|
31
|
-
response = adaptor.
|
26
|
+
response = adaptor.post(
|
32
27
|
resource: 'payment',
|
33
28
|
payload: merge_into(from: local_resource_data, to: ledger_resource_data)
|
34
29
|
)
|
35
30
|
|
36
31
|
success(response: response)
|
37
|
-
rescue OAuth2::Error => e
|
38
|
-
failure(e)
|
39
|
-
end
|
40
|
-
|
41
|
-
def build_customer_operation
|
42
|
-
customer = Customer::Operations::Upsert.new(
|
43
|
-
adaptor: adaptor,
|
44
|
-
resource: resource.customer
|
45
|
-
)
|
46
|
-
|
47
|
-
add_before_operation(customer)
|
48
32
|
end
|
49
33
|
|
50
34
|
def local_resource_data
|
51
35
|
{
|
52
|
-
'amount'
|
53
|
-
'currency'
|
54
|
-
'customer_id'
|
36
|
+
'amount' => resource.amount,
|
37
|
+
'currency' => resource.currency,
|
38
|
+
'customer_id' => resource.customer.ledger_id
|
55
39
|
}
|
56
40
|
end
|
57
41
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module LedgerSync
|
2
|
+
module Adaptors
|
3
|
+
module Test
|
4
|
+
module Transfer
|
5
|
+
module Operations
|
6
|
+
class Create < Operation::Create
|
7
|
+
class Contract < LedgerSync::Adaptors::Contract
|
8
|
+
schema do
|
9
|
+
required(:ledger_id).value(:nil)
|
10
|
+
required(:from_account).hash(Types::Reference)
|
11
|
+
required(:to_account).hash(Types::Reference)
|
12
|
+
required(:amount).filled(:integer)
|
13
|
+
required(:currency).filled(:string)
|
14
|
+
required(:memo).filled(:string)
|
15
|
+
required(:transaction_date).filled(:date?)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def operate
|
22
|
+
response = adaptor.post(
|
23
|
+
resource: 'transfer',
|
24
|
+
payload: local_resource_data
|
25
|
+
)
|
26
|
+
|
27
|
+
resource.ledger_id = response.dig('id')
|
28
|
+
success(response: response)
|
29
|
+
end
|
30
|
+
|
31
|
+
def local_resource_data
|
32
|
+
{
|
33
|
+
'amount' => resource.amount,
|
34
|
+
'currency' => resource.currency,
|
35
|
+
'from_account_id' => resource.from_account.ledger_id,
|
36
|
+
'to_account_id' => resource.to_account.ledger_id,
|
37
|
+
'date' => resource.transaction_date
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module LedgerSync
|
2
|
+
module Adaptors
|
3
|
+
module Test
|
4
|
+
module Transfer
|
5
|
+
module Operations
|
6
|
+
class Find < Operation::Find
|
7
|
+
class Contract < LedgerSync::Adaptors::Contract
|
8
|
+
schema do
|
9
|
+
required(:ledger_id).filled(:string)
|
10
|
+
required(:from_account).hash(Types::Reference)
|
11
|
+
required(:to_account).hash(Types::Reference)
|
12
|
+
required(:amount).maybe(:integer)
|
13
|
+
required(:currency).maybe(:string)
|
14
|
+
required(:memo).maybe(:string)
|
15
|
+
required(:transaction_date).maybe(:date?)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def operate
|
22
|
+
return failure(nil) if resource.ledger_id.nil?
|
23
|
+
|
24
|
+
response = adaptor.find(
|
25
|
+
resource: 'transfer',
|
26
|
+
id: resource.ledger_id
|
27
|
+
)
|
28
|
+
|
29
|
+
success(response: response)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module LedgerSync
|
2
|
+
module Adaptors
|
3
|
+
module Test
|
4
|
+
module Transfer
|
5
|
+
module Operations
|
6
|
+
class Update < Operation::Update
|
7
|
+
class Contract < LedgerSync::Adaptors::Contract
|
8
|
+
params do
|
9
|
+
required(:ledger_id).filled(:string)
|
10
|
+
required(:from_account).hash(Types::Reference)
|
11
|
+
required(:to_account).hash(Types::Reference)
|
12
|
+
required(:amount).filled(:integer)
|
13
|
+
required(:currency).filled(:string)
|
14
|
+
required(:memo).filled(:string)
|
15
|
+
required(:transaction_date).filled(:date?)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def operate
|
22
|
+
ledger_resource_data = adaptor.find(
|
23
|
+
resource: 'transfer',
|
24
|
+
id: resource.ledger_id
|
25
|
+
)
|
26
|
+
response = adaptor.post(
|
27
|
+
resource: 'transfer',
|
28
|
+
payload: merge_into(from: local_resource_data, to: ledger_resource_data)
|
29
|
+
)
|
30
|
+
|
31
|
+
success(response: response)
|
32
|
+
end
|
33
|
+
|
34
|
+
def local_resource_data
|
35
|
+
{
|
36
|
+
'amount' => resource.amount,
|
37
|
+
'currency' => resource.currency,
|
38
|
+
'from_account_id' => resource.from_account.ledger_id,
|
39
|
+
'to_account_id' => resource.to_account.ledger_id,
|
40
|
+
'date' => resource.transaction_date
|
41
|
+
}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module LedgerSync
|
2
|
+
module Adaptors
|
3
|
+
module Test
|
4
|
+
module Vendor
|
5
|
+
module Operations
|
6
|
+
class Create < Operation::Create
|
7
|
+
class Contract < LedgerSync::Adaptors::Contract
|
8
|
+
schema do
|
9
|
+
required(:ledger_id).value(:nil)
|
10
|
+
required(:display_name).maybe(:string)
|
11
|
+
required(:first_name).maybe(:string)
|
12
|
+
required(:last_name).maybe(:string)
|
13
|
+
required(:email).maybe(:string)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def id
|
20
|
+
SecureRandom.uuid
|
21
|
+
end
|
22
|
+
|
23
|
+
def local_resource_data
|
24
|
+
{
|
25
|
+
'name' => resource.display_name,
|
26
|
+
'email' => resource.email
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def operate
|
31
|
+
response = adaptor.post(
|
32
|
+
resource: 'vendor',
|
33
|
+
payload: local_resource_data.merge(
|
34
|
+
'id' => id
|
35
|
+
)
|
36
|
+
)
|
37
|
+
|
38
|
+
resource.ledger_id = response.dig('id')
|
39
|
+
|
40
|
+
success(response: response)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module LedgerSync
|
2
|
+
module Adaptors
|
3
|
+
module Test
|
4
|
+
module Vendor
|
5
|
+
module Operations
|
6
|
+
class Find < Operation::Find
|
7
|
+
class Contract < LedgerSync::Adaptors::Contract
|
8
|
+
params do
|
9
|
+
required(:ledger_id).filled(:string)
|
10
|
+
optional(:display_name).maybe(:string)
|
11
|
+
optional(:first_name).maybe(:string)
|
12
|
+
optional(:last_name).maybe(:string)
|
13
|
+
optional(:email).maybe(:string)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def operate
|
20
|
+
return failure(nil) if resource.ledger_id.nil?
|
21
|
+
|
22
|
+
response = adaptor.find(
|
23
|
+
resource: 'vendor',
|
24
|
+
id: resource.ledger_id
|
25
|
+
)
|
26
|
+
|
27
|
+
success(response: response)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module LedgerSync
|
2
|
+
module Adaptors
|
3
|
+
module Test
|
4
|
+
module Vendor
|
5
|
+
module Operations
|
6
|
+
class Invalid < Operation::Create
|
7
|
+
class Contract < LedgerSync::Adaptors::Contract
|
8
|
+
schema do
|
9
|
+
required(:ledger_id).filled(:string)
|
10
|
+
required(:display_name).filled(:string)
|
11
|
+
required(:first_name).filled(:string)
|
12
|
+
required(:last_name).filled(:string)
|
13
|
+
required(:email).filled(:string)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module LedgerSync
|
2
|
+
module Adaptors
|
3
|
+
module Test
|
4
|
+
module Vendor
|
5
|
+
module Operations
|
6
|
+
class Update < Operation::Update
|
7
|
+
class Contract < LedgerSync::Adaptors::Contract
|
8
|
+
schema do
|
9
|
+
required(:ledger_id).filled(:string)
|
10
|
+
required(:display_name).maybe(:string)
|
11
|
+
required(:first_name).maybe(:string)
|
12
|
+
required(:last_name).maybe(:string)
|
13
|
+
optional(:email).maybe(:string)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def operate
|
20
|
+
ledger_resource_data = adaptor.find(
|
21
|
+
resource: 'vendor',
|
22
|
+
id: resource.ledger_id
|
23
|
+
)
|
24
|
+
|
25
|
+
response = adaptor.post(
|
26
|
+
resource: 'vendor',
|
27
|
+
payload: merge_into(from: local_resource_data, to: ledger_resource_data)
|
28
|
+
)
|
29
|
+
|
30
|
+
success(response: response)
|
31
|
+
end
|
32
|
+
|
33
|
+
def local_resource_data
|
34
|
+
{
|
35
|
+
'name' => resource.name,
|
36
|
+
'email' => resource.email
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module LedgerSync
|
2
|
+
module Adaptors
|
3
|
+
module Test
|
4
|
+
module Vendor
|
5
|
+
module Operations
|
6
|
+
class Valid < Operation::Create
|
7
|
+
class Contract < LedgerSync::Adaptors::Contract
|
8
|
+
schema do
|
9
|
+
optional(:ledger_id).maybe(:string)
|
10
|
+
optional(:display_name).maybe(:string)
|
11
|
+
optional(:first_name).maybe(:string)
|
12
|
+
optional(:last_name).maybe(:string)
|
13
|
+
optional(:email).maybe(:string)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def operate
|
20
|
+
success(response: :foo)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module LedgerSync
|
2
|
+
module Adaptors
|
3
|
+
module Test
|
4
|
+
module Vendor
|
5
|
+
class Searcher < LedgerSync::Adaptors::Searcher
|
6
|
+
def next_searcher
|
7
|
+
paginate(cursor: 'asdf')
|
8
|
+
end
|
9
|
+
|
10
|
+
def previous_searcher
|
11
|
+
paginate(cursor: 'asdf')
|
12
|
+
end
|
13
|
+
|
14
|
+
def resources
|
15
|
+
@resources ||= begin
|
16
|
+
adaptor
|
17
|
+
.query(
|
18
|
+
resource: 'vendor',
|
19
|
+
query: "name LIKE '#{query}%'"
|
20
|
+
)
|
21
|
+
.map do |v|
|
22
|
+
LedgerSync::Vendor.new(
|
23
|
+
ledger_id: v.fetch('id'),
|
24
|
+
display_name: v.fetch('display_name', ''),
|
25
|
+
first_name: v.fetch('first_name', ''),
|
26
|
+
last_name: v.fetch('last_name', '')
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def search
|
33
|
+
super
|
34
|
+
rescue OAuth2::Error => e
|
35
|
+
failure(e)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -6,6 +6,30 @@ module LedgerSync
|
|
6
6
|
@resource = resource
|
7
7
|
super(message: message)
|
8
8
|
end
|
9
|
+
class AttributeTypeError < self
|
10
|
+
attr_reader :attribute, :resource, :value
|
11
|
+
|
12
|
+
def initialize(attribute:, resource:, value:)
|
13
|
+
@attribute = attribute
|
14
|
+
@resource = resource
|
15
|
+
@value = value
|
16
|
+
|
17
|
+
resource_class = resource.class
|
18
|
+
|
19
|
+
message = "Attribute #{attribute.name} for #{resource_class.name} should be a class supported by #{attribute.type.class.name}. Given: #{value.class}"
|
20
|
+
|
21
|
+
super(message: message, resource: nil)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class ReferenceAssignmentError < self
|
26
|
+
def initialize(attribute:, resource:, value:)
|
27
|
+
resource_class = resource.class
|
28
|
+
message = "Attribute #{attribute} value for #{resource_class.name} should be a #{asdf}. Given: #{value.class}"
|
29
|
+
|
30
|
+
super(message: message, resource: resource)
|
31
|
+
end
|
32
|
+
end
|
9
33
|
|
10
34
|
class MissingResourceError < self
|
11
35
|
attr_reader :resource_type, :resource_external_id
|
data/lib/ledger_sync/resource.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'resource_attribute'
|
4
|
+
|
3
5
|
# Template class for named resources such as
|
4
6
|
# LedgerSync::Invoice, LedgerSync::Contact, etc.
|
5
7
|
module LedgerSync
|
@@ -7,80 +9,38 @@ module LedgerSync
|
|
7
9
|
include SimplySerializable::Mixin
|
8
10
|
include Validatable
|
9
11
|
include Fingerprintable::Mixin
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
include ResourceAttribute::Mixin
|
13
|
+
include ResourceAttribute::Reference::One::Mixin
|
14
|
+
include ResourceAttribute::Reference::Many::Mixin
|
15
|
+
|
16
|
+
PRIMITIVES = [
|
17
|
+
Date,
|
18
|
+
DateTime,
|
19
|
+
FalseClass,
|
20
|
+
Float,
|
21
|
+
Integer,
|
22
|
+
NilClass,
|
23
|
+
String,
|
24
|
+
Time,
|
25
|
+
TrueClass
|
26
|
+
].freeze
|
27
|
+
|
28
|
+
serialize except: %i[resource_attributes references]
|
29
|
+
|
30
|
+
dirty_attribute :external_id, :ledger_id, :sync_token
|
19
31
|
|
20
32
|
def initialize(external_id: nil, ledger_id: nil, sync_token: nil, **data)
|
21
|
-
@external_id = external_id.
|
33
|
+
@external_id = external_id.try(:to_sym)
|
22
34
|
@ledger_id = ledger_id
|
23
35
|
@sync_token = sync_token
|
24
36
|
|
25
|
-
data
|
26
|
-
if (self.class.references || {}).key?(attr_key)
|
27
|
-
raise "#{val} must be of type #{self.class.references[attr_key]}" if self.class.references[attr_key] != val.class
|
28
|
-
end
|
29
|
-
|
30
|
-
raise "#{attr_key} is not an attribute of #{self.class}" unless self.class.attributes.include?(attr_key)
|
31
|
-
end
|
32
|
-
|
33
|
-
self.class.attributes.each do |attribute|
|
34
|
-
instance_variable_set("@#{attribute}", data.dig(attribute))
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def attributes
|
39
|
-
self.class.attributes
|
40
|
-
end
|
41
|
-
|
42
|
-
def references
|
43
|
-
self.class.references
|
44
|
-
end
|
45
|
-
|
46
|
-
def serialize_attributes
|
47
|
-
Hash[self.class.attributes.map { |a| [a, send(a)] }]
|
48
|
-
end
|
49
|
-
|
50
|
-
# def serializable_type
|
51
|
-
# self.class.resource_type
|
52
|
-
# end
|
53
|
-
|
54
|
-
def self.attribute(name)
|
55
|
-
attributes << name.to_sym
|
56
|
-
class_eval { attr_accessor name }
|
57
|
-
end
|
58
|
-
|
59
|
-
def self.attributes
|
60
|
-
@attributes ||= []
|
37
|
+
super(data)
|
61
38
|
end
|
62
39
|
|
63
|
-
def
|
40
|
+
def klass_from_resource_type(obj)
|
64
41
|
LedgerSync.const_get(LedgerSync::Util::StringHelpers.camelcase(obj))
|
65
42
|
end
|
66
43
|
|
67
|
-
def self.reference(name, type)
|
68
|
-
attribute(name)
|
69
|
-
references[name.to_sym] = type
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.references
|
73
|
-
@references ||= {}
|
74
|
-
end
|
75
|
-
|
76
|
-
def self.reference_klass(name)
|
77
|
-
references[name.to_sym]
|
78
|
-
end
|
79
|
-
|
80
|
-
def self.reference_resource_type(name)
|
81
|
-
reference_klass(name).resource_type
|
82
|
-
end
|
83
|
-
|
84
44
|
def self.resource_type
|
85
45
|
@resource_type ||= LedgerSync::Util::StringHelpers.underscore(name.split('::').last).to_sym
|
86
46
|
end
|