Module: Mycrm::Model::InstanceMethods

Defined in:
lib/mycrm/model.rb

Overview

provides methods for the instance

Instance Method Summary collapse

Instance Method Details

#create!Object



27
28
29
# File 'lib/mycrm/model.rb', line 27

def create!
  mass_assign(self.class.execute(:post, nil, to_json))
end

#delete!Object



35
36
37
# File 'lib/mycrm/model.rb', line 35

def delete!
  self.class.execute(:delete, id, nil)
end

#initialize(attributes = nil) ⇒ Object



21
22
23
24
25
# File 'lib/mycrm/model.rb', line 21

def initialize(attributes = nil)
  partitions = (attributes || []).partition{ |field, _| attribute_set[field].nil? }
  super(partitions.last.to_h)
  partitions.first.each { |field, value| send("#{field}=", value) rescue nil }
end

#mass_assign(json) ⇒ Object



47
48
49
# File 'lib/mycrm/model.rb', line 47

def mass_assign(json)
  attribute_set.set(self, json)
end

#swoop(_, v) ⇒ Object



43
44
45
# File 'lib/mycrm/model.rb', line 43

def swoop(_, v)
  (v.is_a?(Hash) ? v.delete_if(&method(:swoop)) : v.to_s).empty?
end

#to_jsonObject



39
40
41
# File 'lib/mycrm/model.rb', line 39

def to_json
  VirtusConvert.new(self).to_hash.delete_if(&method(:swoop))
end

#update!Object



31
32
33
# File 'lib/mycrm/model.rb', line 31

def update!
  mass_assign(self.class.execute(:put, id, to_json))
end