Class: Amorail::Entity
- Inherits:
-
Object
show all
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Model, ActiveModel::Validations
- Defined in:
- lib/amorail/entity.rb,
lib/amorail/entity/params.rb,
lib/amorail/entity/finders.rb,
lib/amorail/entity/persistance.rb
Overview
Core class for all Amo entities (company, contact, etc)
Defined Under Namespace
Classes: InvalidRecord, NotPersisted, RecordNotFound
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Entity
Returns a new instance of Entity.
59
60
61
62
|
# File 'lib/amorail/entity.rb', line 59
def initialize(attributes = {})
super(attributes)
self.last_modified = Time.now.to_i if last_modified.nil?
end
|
Class Attribute Details
.amo_name ⇒ Object
Returns the value of attribute amo_name.
13
14
15
|
# File 'lib/amorail/entity.rb', line 13
def amo_name
@amo_name
end
|
.amo_response_name ⇒ Object
Returns the value of attribute amo_response_name.
13
14
15
|
# File 'lib/amorail/entity.rb', line 13
def amo_response_name
@amo_response_name
end
|
Class Method Details
.amo_field(*vars, **hargs) ⇒ Object
27
28
29
30
31
|
# File 'lib/amorail/entity.rb', line 27
def amo_field(*vars, **hargs)
vars.each { |v| attributes[v] = :default }
hargs.each { |k, v| attributes[k] = v }
attr_accessor(*(vars + hargs.keys))
end
|
.amo_names(name, response_name = nil) ⇒ Object
22
23
24
25
|
# File 'lib/amorail/entity.rb', line 22
def amo_names(name, response_name = nil)
@amo_name = @amo_response_name = name
@amo_response_name = response_name unless response_name.nil?
end
|
.amo_property(name, options = {}) ⇒ Object
33
34
35
36
|
# File 'lib/amorail/entity.rb', line 33
def amo_property(name, options = {})
properties[name] = options
attr_accessor(name)
end
|
.attributes ⇒ Object
38
39
40
41
|
# File 'lib/amorail/entity.rb', line 38
def attributes
@attributes ||=
superclass.respond_to?(:attributes) ? superclass.attributes.dup : {}
end
|
.find(id) ⇒ Object
5
6
7
|
# File 'lib/amorail/entity/finders.rb', line 5
def find(id)
new.load_record(id)
end
|
.find!(id) ⇒ Object
Find AMO entity by id and raise RecordNotFound if nothing was found
11
12
13
14
15
|
# File 'lib/amorail/entity/finders.rb', line 11
def find!(id)
rec = find(id)
fail RecordNotFound unless rec
rec
end
|
.find_all(*ids) ⇒ Object
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/amorail/entity/finders.rb', line 17
def find_all(*ids)
ids = ids.first if ids.size == 1 && ids.first.is_a?(Array)
response = client.safe_request(
:get,
remote_url('list'),
id: ids
)
load_many(response)
end
|
.find_by_query(q) ⇒ Object
Find AMO entities by query Returns array of matching entities.
30
31
32
33
34
35
36
37
|
# File 'lib/amorail/entity/finders.rb', line 30
def find_by_query(q)
response = client.safe_request(
:get,
remote_url('list'),
query: q
)
load_many(response)
end
|
.inherited(subclass) ⇒ Object
18
19
20
|
# File 'lib/amorail/entity.rb', line 18
def inherited(subclass)
subclass.amo_names amo_name, amo_response_name
end
|
.properties ⇒ Object
43
44
45
46
|
# File 'lib/amorail/entity.rb', line 43
def properties
@properties ||=
superclass.respond_to?(:properties) ? superclass.properties.dup : {}
end
|
.remote_url(action) ⇒ Object
48
49
50
|
# File 'lib/amorail/entity.rb', line 48
def remote_url(action)
File.join(Amorail.config.api_path, amo_name, action)
end
|
Instance Method Details
#load_record(id) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/amorail/entity/finders.rb', line 49
def load_record(id)
response = client.safe_request(
:get,
remote_url('list'),
id: id
)
handle_response(response, 'load')
end
|
#new_record? ⇒ Boolean
6
7
8
|
# File 'lib/amorail/entity/persistance.rb', line 6
def new_record?
id.blank?
end
|
#params ⇒ Object
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/amorail/entity/params.rb', line 3
def params
data = {}
self.class.attributes.each do |k, v|
data[k] = send("to_#{v}", send(k))
end
data[:custom_fields] = custom_fields if properties.respond_to?(amo_name)
normalize_params(data)
end
|
#persisted? ⇒ Boolean
10
11
12
|
# File 'lib/amorail/entity/persistance.rb', line 10
def persisted?
!new_record?
end
|
#reload ⇒ Object
41
42
43
44
|
# File 'lib/amorail/entity/persistance.rb', line 41
def reload
fail NotPersisted if id.nil?
load_record(id)
end
|
#reload_model(info) ⇒ Object
68
69
70
71
72
|
# File 'lib/amorail/entity.rb', line 68
def reload_model(info)
merge_params(info)
merge_custom_fields(info['custom_fields'])
self
end
|
#save ⇒ Object
14
15
16
17
|
# File 'lib/amorail/entity/persistance.rb', line 14
def save
return false unless valid?
new_record? ? push('add') : push('update')
end
|
#save! ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/amorail/entity/persistance.rb', line 19
def save!
if save
true
else
fail InvalidRecord
end
end
|
#update(attrs = {}) ⇒ Object
27
28
29
30
31
|
# File 'lib/amorail/entity/persistance.rb', line 27
def update(attrs = {})
return false if new_record?
merge_params(attrs)
push('update')
end
|
#update!(attrs = {}) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/amorail/entity/persistance.rb', line 33
def update!(attrs = {})
if update(attrs)
true
else
fail NotPersisted
end
end
|