Module: Nylas::Model
- Included in:
- Account, Calendar, Component, Contact, ContactGroup, CurrentAccount, Deltas, Draft, Event, File, Folder, JobStatus, Label, Message, NeuralCategorizer, NeuralCleanConversation, NeuralOcr, NeuralSentimentAnalysis, NeuralSignatureExtraction, NewMessage, OutboxJobStatus, OutboxMessage, RoomResource, Rsvp, Scheduler, Thread, Webhook
- Defined in:
- lib/nylas/model.rb,
lib/nylas/model/attributes.rb,
lib/nylas/model/attributable.rb,
lib/nylas/model/transferable.rb,
lib/nylas/model/attribute_definition.rb,
lib/nylas/model/list_attribute_definition.rb
Overview
Include this to define a class to represent an object returned from the API
Defined Under Namespace
Modules: Attributable, ClassMethods, Transferable
Classes: AttributeDefinition, Attributes, ListAttributeDefinition
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
11
12
13
|
# File 'lib/nylas/model.rb', line 11
def api
@api
end
|
Class Method Details
.included(model) ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'lib/nylas/model.rb', line 17
def self.included(model)
model.include(Attributable)
model.include(Transferable)
model.extend(ClassMethods)
model.extend(Forwardable)
model.def_delegators :model_class, :creatable?, :filterable?, :listable?, :searchable?, :showable?,
:updatable?, :destroyable?
model.allows_operations
end
|
Instance Method Details
#auth_method ⇒ Object
110
111
112
|
# File 'lib/nylas/model.rb', line 110
def auth_method
self.class.auth_method(api: api)
end
|
#create ⇒ Object
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/nylas/model.rb', line 46
def create
raise ModelNotCreatableError, self unless creatable?
execute(
method: :post,
payload: attributes.serialize_for_api,
path: resources_path,
query: query_params
)
end
|
#destroy ⇒ Object
114
115
116
117
118
|
# File 'lib/nylas/model.rb', line 114
def destroy
raise ModelNotDestroyableError, self unless destroyable?
execute(method: :delete, path: resource_path, query: query_params)
end
|
#execute(method:, payload: nil, path:, query: {}, auth_method: self.auth_method) ⇒ Object
42
43
44
|
# File 'lib/nylas/model.rb', line 42
def execute(method:, payload: nil, path:, query: {}, auth_method: self.auth_method)
api.execute(method: method, payload: payload, path: path, query: query, auth_method: auth_method)
end
|
#model_class ⇒ Object
13
14
15
|
# File 'lib/nylas/model.rb', line 13
def model_class
self.class
end
|
#persisted? ⇒ Boolean
38
39
40
|
# File 'lib/nylas/model.rb', line 38
def persisted?
!id.nil?
end
|
#reload ⇒ Object
97
98
99
100
|
# File 'lib/nylas/model.rb', line 97
def reload
assign(**execute(method: :get, path: resource_path))
true
end
|
#resource_path ⇒ Object
102
103
104
|
# File 'lib/nylas/model.rb', line 102
def resource_path
"#{resources_path}/#{id}"
end
|
#resources_path ⇒ Object
106
107
108
|
# File 'lib/nylas/model.rb', line 106
def resources_path
self.class.resources_path(api: api)
end
|
#save ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/nylas/model.rb', line 27
def save
result = if persisted?
raise ModelNotUpdatableError, self unless updatable?
update_call(attributes.serialize_for_api)
else
create
end
attributes.merge(result)
end
|
#save_all_attributes ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/nylas/model.rb', line 69
def save_all_attributes
result = if persisted?
raise ModelNotUpdatableError, self unless updatable?
execute(
method: :put,
payload: attributes.serialize_all_for_api,
path: resource_path
)
else
create
end
attributes.merge(result)
end
|
#to_json(_opts = {}) ⇒ String
Returns JSON String of the model.
121
122
123
|
# File 'lib/nylas/model.rb', line 121
def to_json(_opts = {})
JSON.dump(to_h)
end
|
#update_all_attributes(**data) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/nylas/model.rb', line 85
def update_all_attributes(**data)
raise ModelNotUpdatableError, model_class unless updatable?
attributes.merge(**data)
payload = attributes.serialize_all_for_api(keys: data.keys)
update_call(payload)
true
rescue Registry::MissingKeyError => e
raise ModelMissingFieldError.new(e.key, self)
end
|