Class: Exact::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/exact/models/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
6
7
8
|
# File 'lib/exact/models/base.rb', line 6
def client
@client
end
|
Class Method Details
.all(client:) ⇒ Object
48
49
50
51
52
|
# File 'lib/exact/models/base.rb', line 48
def self.all(client:)
client.send(to_s.demodulize.pluralize)
result = client.execute
result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
end
|
.create(attributes: {}, client:) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/exact/models/base.rb', line 54
def self.create(attributes: {}, client:)
exact_obj = Exactonline.const_get(exact_endpoint.singularize).new(attributes)
client.send("AddTo#{exact_endpoint}", exact_obj)
result = client.save_changes
result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
result.first
end
|
.create_client(access_token:, division:) ⇒ Object
38
39
40
|
# File 'lib/exact/models/base.rb', line 38
def self.create_client(access_token:, division:)
Exact::Client.new(access_token: access_token, division: division, service: exact_service, endpoint: exact_endpoint)
end
|
.destroy(id: nil, client:) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/exact/models/base.rb', line 115
def self.destroy(id: nil, client:)
client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{id}'")
result = client.execute
return false unless result.any?
client.delete_object(result.first)
client.save_changes
true
rescue OData::ServiceError => e
errors.add(:base, e.message)
false
end
|
.exact_endpoint ⇒ Object
28
29
30
31
32
|
# File 'lib/exact/models/base.rb', line 28
def self.exact_endpoint
name = Object.const_get("#{self}::EXACT_ENDPOINT") rescue nil
name = to_s.demodulize.pluralize.camelize unless name
name
end
|
.exact_guid_attribute ⇒ Object
12
13
14
|
# File 'lib/exact/models/base.rb', line 12
def self.exact_guid_attribute
Object.const_get("#{name}::EXACT_GUID")
end
|
.exact_service ⇒ Object
20
21
22
|
# File 'lib/exact/models/base.rb', line 20
def self.exact_service
Object.const_get("#{self}::EXACT_SERVICE")
end
|
.find(id:, client:) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/exact/models/base.rb', line 62
def self.find(id:, client:)
client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{id}'")
result = client.execute
result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
result.first
end
|
.find_by(field:, value:, guid: false, client:) ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/exact/models/base.rb', line 69
def self.find_by(field:, value:, guid: false, client:)
query = "#{field.capitalize} eq "
query << 'guid' if guid
query << "'#{value}'"
client.send(exact_endpoint).filter(query)
result = client.execute
result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
result.first
end
|
.update(id:, attributes: {}, client:) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/exact/models/base.rb', line 79
def self.update(id:, attributes: {}, client:)
client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{id}'")
result = client.execute
return false unless result.any?
exact_obj = result.first
attributes.each do |attribute, value|
exact_obj.send("#{attribute}=", value) if exact_obj.respond_to? attribute
end
client.update_object(exact_obj)
client.save_changes
true
end
|
Instance Method Details
#destroy(client:) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/exact/models/base.rb', line 127
def destroy(client:)
client.send(self.class.exact_endpoint).filter("#{self.class.exact_guid_attribute} eq guid'#{guid}'")
result = client.execute
return false unless result.any?
client.delete_object(result.first)
client.save_changes
true
rescue OData::ServiceError => e
errors.add(:base, e.message)
false
end
|
#exact_endpoint ⇒ Object
34
35
36
|
# File 'lib/exact/models/base.rb', line 34
def exact_endpoint
self.class.exact_endpoint
end
|
#exact_guid_attribute ⇒ Object
16
17
18
|
# File 'lib/exact/models/base.rb', line 16
def exact_guid_attribute
self.class.exact_guid_attribute
end
|
#exact_service ⇒ Object
24
25
26
|
# File 'lib/exact/models/base.rb', line 24
def exact_service
self.class.exact_service
end
|
#guid ⇒ Object
8
9
10
|
# File 'lib/exact/models/base.rb', line 8
def guid
send(self.class.exact_guid_attribute)
end
|
#save(client:) ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/exact/models/base.rb', line 106
def save(client:)
if !guid.blank?
update(client: client)
else
self.attributes = self.class.create(attributes: attributes, client: client).attributes
self
end
end
|
#setup_client(access_token: nil, division: nil, reload: false) ⇒ Object
42
43
44
45
46
|
# File 'lib/exact/models/base.rb', line 42
def setup_client(access_token: nil, division: nil, reload: false)
return @client if @client.present? && !reload
return @client = self.class.create_client(access_token: access_token, division: division) if @client.nil? || reload
self
end
|
#update(client:) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/exact/models/base.rb', line 92
def update(client:)
return false if guid.blank?
client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{guid}'")
result = client.execute
return false unless result.any?
exact_obj = result.first
attributes.each do |attribute, value|
exact_obj.send("#{attribute}=", value) if exact_obj.respond_to? attribute
end
client.update_object(exact_obj)
client.save_changes
true
end
|