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/persistence.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.
61
62
63
64
|
# File 'lib/amorail/entity.rb', line 61
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.
15
16
17
|
# File 'lib/amorail/entity.rb', line 15
def amo_name
@amo_name
end
|
.amo_response_name ⇒ Object
Returns the value of attribute amo_response_name.
15
16
17
|
# File 'lib/amorail/entity.rb', line 15
def amo_response_name
@amo_response_name
end
|
Class Method Details
.amo_field(*vars, **hargs) ⇒ Object
29
30
31
32
33
|
# File 'lib/amorail/entity.rb', line 29
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
24
25
26
27
|
# File 'lib/amorail/entity.rb', line 24
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
35
36
37
38
|
# File 'lib/amorail/entity.rb', line 35
def amo_property(name, options = {})
properties[name] = options
attr_accessor(options.fetch(:method_name, name))
end
|
.attributes ⇒ Object
40
41
42
43
|
# File 'lib/amorail/entity.rb', line 40
def attributes
@attributes ||=
superclass.respond_to?(:attributes) ? superclass.attributes.dup : {}
end
|
.find(id) ⇒ Object
7
8
9
|
# File 'lib/amorail/entity/finders.rb', line 7
def find(id)
new.load_record(id)
end
|
.find!(id) ⇒ Object
Find AMO entity by id and raise RecordNotFound if nothing was found
13
14
15
16
17
18
|
# File 'lib/amorail/entity/finders.rb', line 13
def find!(id)
rec = find(id)
fail RecordNotFound unless rec
rec
end
|
.find_all(*ids) ⇒ Object
30
31
32
33
34
|
# File 'lib/amorail/entity/finders.rb', line 30
def find_all(*ids)
ids = ids.first if ids.size == 1 && ids.first.is_a?(Array)
where(id: ids)
end
|
.find_by_query(query) ⇒ Object
Find AMO entities by query Returns array of matching entities.
38
39
40
|
# File 'lib/amorail/entity/finders.rb', line 38
def find_by_query(query)
where(query: query)
end
|
.inherited(subclass) ⇒ Object
20
21
22
|
# File 'lib/amorail/entity.rb', line 20
def inherited(subclass)
subclass.amo_names amo_name, amo_response_name
end
|
.properties ⇒ Object
45
46
47
48
|
# File 'lib/amorail/entity.rb', line 45
def properties
@properties ||=
superclass.respond_to?(:properties) ? superclass.properties.dup : {}
end
|
.remote_url(action) ⇒ Object
50
51
52
|
# File 'lib/amorail/entity.rb', line 50
def remote_url(action)
File.join(Amorail.config.api_path, amo_name, action)
end
|
.where(options) ⇒ Object
General method to load many records by proving some filters
21
22
23
24
25
26
27
28
|
# File 'lib/amorail/entity/finders.rb', line 21
def where(options)
response = client.safe_request(
:get,
remote_url('list'),
options
)
load_many(response)
end
|
Instance Method Details
#load_record(id) ⇒ Object
54
55
56
57
58
59
60
61
|
# File 'lib/amorail/entity/finders.rb', line 54
def load_record(id)
response = client.safe_request(
:get,
remote_url('list'),
id: id
)
handle_response(response, 'load') || nil
end
|
#new_record? ⇒ Boolean
8
9
10
|
# File 'lib/amorail/entity/persistence.rb', line 8
def new_record?
id.blank?
end
|
#params ⇒ Object
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/amorail/entity/params.rb', line 7
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
12
13
14
|
# File 'lib/amorail/entity/persistence.rb', line 12
def persisted?
!new_record?
end
|
#reload ⇒ Object
37
38
39
40
41
|
# File 'lib/amorail/entity/persistence.rb', line 37
def reload
fail NotPersisted if id.nil?
load_record(id)
end
|
#reload_model(info) ⇒ Object
70
71
72
73
74
|
# File 'lib/amorail/entity.rb', line 70
def reload_model(info)
merge_params(info)
merge_custom_fields(info['custom_fields'])
self
end
|
#save ⇒ Object
16
17
18
19
20
|
# File 'lib/amorail/entity/persistence.rb', line 16
def save
return false unless valid?
new_record? ? push('add') : push('update')
end
|
#save! ⇒ Object
22
23
24
|
# File 'lib/amorail/entity/persistence.rb', line 22
def save!
save || fail(InvalidRecord)
end
|
#update(attrs = {}) ⇒ Object
26
27
28
29
30
31
|
# File 'lib/amorail/entity/persistence.rb', line 26
def update(attrs = {})
return false if new_record?
merge_params(attrs)
push('update')
end
|
#update!(attrs = {}) ⇒ Object
33
34
35
|
# File 'lib/amorail/entity/persistence.rb', line 33
def update!(attrs = {})
update(attrs) || fail(NotPersisted)
end
|