Module: Connectwise::Model::ClassMethods

Defined in:
lib/connectwise/model.rb

Instance Method Summary collapse

Instance Method Details

#attrs_to_query(attrs) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/connectwise/model.rb', line 24

def attrs_to_query(attrs)
  attrs.map do |k,v|
    str = k.to_s
    str.extend(Connectwise::Extensions::String)
    "#{str.camelize} like '#{v}'"
  end.join(' and ')
end

#cw_api_nameObject



32
33
34
# File 'lib/connectwise/model.rb', line 32

def cw_api_name
  base_class_name.downcase.to_sym
end

#find(connection, id) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/connectwise/model.rb', line 10

def find(connection, id)
  if (attrs = connection.call(cw_api_name, "get_#{cw_api_name}".to_sym, {id: id}))
    self.new(connection, find_transform(attrs))
  else
    fail RecordNotFound
  end
rescue ConnectionError
  raise RecordNotFound
end

#find_transform(attrs) ⇒ Object



41
42
43
# File 'lib/connectwise/model.rb', line 41

def find_transform(attrs)
  attrs
end

#model_name(model_name = self.name) ⇒ Object



53
54
55
# File 'lib/connectwise/model.rb', line 53

def model_name(model_name = self.name)
  @model_name ||= model_name
end

#plural(plural) ⇒ Object



20
21
22
# File 'lib/connectwise/model.rb', line 20

def plural(plural)
  @plural_form = plural
end

#plural_class_nameObject



36
37
38
39
# File 'lib/connectwise/model.rb', line 36

def plural_class_name
  ending = base_class_name[/[aeiou]$/] ? 'es' : 's'
  @plural_form ||= "#{base_class_name.downcase}#{ending}"
end

#save_transform(attrs) ⇒ Object



45
46
47
# File 'lib/connectwise/model.rb', line 45

def save_transform(attrs)
  attrs
end

#transform(attrs) ⇒ Object



49
50
51
# File 'lib/connectwise/model.rb', line 49

def transform(attrs)
  attrs
end

#where(connection, *args, **attrs) ⇒ Object



4
5
6
7
8
# File 'lib/connectwise/model.rb', line 4

def where(connection, *args, **attrs)
  conditions = attrs ? attrs_to_query(transform(attrs)) : args.join(' ')
  resp = connection.call cw_api_name, "find_#{plural_class_name}".to_sym, {conditions: conditions}
  resp ? Array(remove_root_node(resp)).map {|attrs| self.new(connection, find_transform(attrs)) } : []
end