module Connectwise::Model::ClassMethods

Public Instance Methods

attrs_to_query(attrs) click to toggle source
# 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_name() click to toggle source
# File lib/connectwise/model.rb, line 32
def cw_api_name
  base_class_name.downcase.to_sym
end
cw_model_name() click to toggle source
# File lib/connectwise/model.rb, line 36
def cw_model_name
  base_class_name.downcase.to_sym
end
find(connection, id) click to toggle source
# File lib/connectwise/model.rb, line 10
def find(connection, id)
  if (attrs = connection.call(cw_api_name, "get_#{cw_model_name}".to_sym, {id: id}))
    self.new(connection, find_transform(attrs))
  else
    fail RecordNotFound
  end
rescue ConnectionError
  raise RecordNotFound
end
find_transform(attrs) click to toggle source
# File lib/connectwise/model.rb, line 49
def find_transform(attrs)
  attrs
end
model_name(model_name = self.name) click to toggle source
# File lib/connectwise/model.rb, line 61
def model_name(model_name = self.name)
  @model_name ||= model_name
end
plural(plural) click to toggle source
# File lib/connectwise/model.rb, line 20
def plural(plural)
  @plural_form = plural
end
plural_class_name() click to toggle source
# File lib/connectwise/model.rb, line 40
def plural_class_name
  ending = base_class_name[/[aeiou]$/] ? 'es' : 's'
  @plural_form ||= "#{base_class_name.downcase}#{ending}"
end
save_transform(attrs) click to toggle source
# File lib/connectwise/model.rb, line 53
def save_transform(attrs)
  attrs
end
transform(attrs) click to toggle source
# File lib/connectwise/model.rb, line 57
def transform(attrs)
  attrs
end
where(connection, *args, **attrs) click to toggle source
# File lib/connectwise/model.rb, line 4
def where(connection, *args, **attrs)
  conditions = attrs.empty? ? args.join(' ') : attrs_to_query(where_transform(attrs))
  resp = connection.call cw_api_name, "find_#{plural_class_name}".to_sym, {conditions: conditions}
  normalize_find_response(resp).map {|attrs| self.new(connection, find_transform(attrs)) }
end
where_transform(attrs) click to toggle source
# File lib/connectwise/model.rb, line 45
def where_transform(attrs)
  attrs
end

Private Instance Methods

base_class_name() click to toggle source
# File lib/connectwise/model.rb, line 66
def base_class_name
  model_name.split('::').last
end
normalize_find_response(resp) click to toggle source
# File lib/connectwise/model.rb, line 74
def normalize_find_response(resp)
  resp = resp.nil? ? [] : remove_root_node(resp)
  resp.respond_to?(:to_ary) ? resp : [resp]
end
remove_root_node(resp) click to toggle source
# File lib/connectwise/model.rb, line 70
def remove_root_node(resp)
  resp.values.first
end