Module: Connectwise::Model

Included in:
Client, Company, Contact, Member, Opportunity, ServiceTicket, Ticket, TicketNote
Defined in:
lib/connectwise/model.rb,
lib/connectwise/connectwise.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



80
81
82
# File 'lib/connectwise/model.rb', line 80

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#defined_attributesObject



113
114
115
116
# File 'lib/connectwise/model.rb', line 113

def defined_attributes
  instance_vars = instance_variables.map {|name| name.to_s.gsub(/@/, '').to_sym}
  public_methods.select{|method| instance_vars.include?(method) }
end

#destroyObject



96
97
98
99
# File 'lib/connectwise/model.rb', line 96

def destroy
  connection.call self.class.cw_api_name, "delete_#{self.class.cw_model_name}".to_sym, {id: id}
  self
end

#initialize(**attributes) ⇒ Object



84
85
86
87
88
89
# File 'lib/connectwise/model.rb', line 84

def initialize(connection, **attributes)
  @connection = connection
  self.class.transform(attributes).each do |attr, value|
    public_send("#{attr}=", value) if respond_to?("#{attr}=")
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/connectwise/model.rb', line 101

def persisted?
  !!id
end

#saveObject



91
92
93
94
# File 'lib/connectwise/model.rb', line 91

def save
  attrs = connection.call self.class.cw_api_name, "add_or_update_#{self.class.cw_model_name}".to_sym, {self.class.cw_model_name => to_cw_h}
  self.class.new(connection, self.class.save_transform(attrs))
end

#to_cw_hObject



109
110
111
# File 'lib/connectwise/model.rb', line 109

def to_cw_h
  defined_attributes.each_with_object({}) {|name, memo| key = name.to_s.extend(Extensions::String); memo[key.camelize] = public_send(name)}
end

#to_hObject



105
106
107
# File 'lib/connectwise/model.rb', line 105

def to_h
  defined_attributes.each_with_object({}) {|name, memo| memo[name] = public_send(name)}
end