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, 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

#apiObject

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

#createObject



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

#destroyObject



110
111
112
113
114
# File 'lib/nylas/model.rb', line 110

def destroy
  raise ModelNotDestroyableError, self unless destroyable?

  execute(method: :delete, path: resource_path, query: query_params)
end

#execute(method:, payload: nil, path:, query: {}) ⇒ Object



42
43
44
# File 'lib/nylas/model.rb', line 42

def execute(method:, payload: nil, path:, query: {})
  api.execute(method: method, payload: payload, path: path, query: query)
end

#model_classObject



13
14
15
# File 'lib/nylas/model.rb', line 13

def model_class
  self.class
end

#persisted?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/nylas/model.rb', line 38

def persisted?
  !id.nil?
end

#reloadObject



97
98
99
100
# File 'lib/nylas/model.rb', line 97

def reload
  assign(**execute(method: :get, path: resource_path))
  true
end

#resource_pathObject



102
103
104
# File 'lib/nylas/model.rb', line 102

def resource_path
  "#{resources_path}/#{id}"
end

#resources_pathObject



106
107
108
# File 'lib/nylas/model.rb', line 106

def resources_path
  self.class.resources_path(api: api)
end

#saveObject



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_attributesObject



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.

Returns:

  • (String)

    JSON String of the model.



117
118
119
# File 'lib/nylas/model.rb', line 117

def to_json(_opts = {})
  JSON.dump(to_h)
end

#update(**data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nylas/model.rb', line 57

def update(**data)
  raise ModelNotUpdatableError, model_class unless updatable?

  attributes.merge(**data)
  payload = attributes.serialize_for_api(keys: data.keys)
  update_call(payload)

  true
rescue Registry::MissingKeyError => e
  raise ModelMissingFieldError.new(e.key, self)
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