module Mycrm::Model::ClassMethods

provides method at class level

Public Class Methods

extended(descendant) click to toggle source
# File lib/mycrm/model.rb, line 67
def self.extended(descendant)
  descendant.singleton_class.class_eval do
    attr_accessor :class_endpoint, :alowed_methods, :attribute_to
  end
end

Public Instance Methods

attribute(name, klazz, options = {}) click to toggle source

macro method to create a new attribute of type Object and relative accessors

Calls superclass method
# File lib/mycrm/model.rb, line 131
def attribute(name, klazz, options = {})
  if options[:embedded]
    options.merge!(default: {})
    define_embedded_methods(name, klazz)
  end

  define_formatted_methods name

  super

  alias_method(options[:as], name) if options[:as]
end
define_embedded_method(template, field, attribute) click to toggle source
# File lib/mycrm/model.rb, line 169
def define_embedded_method(template, field, attribute)
  accessor = format(template, field: field.to_s, attribute: attribute)

  define_method "#{accessor}=" do |val|
    instance_variable_get("@#{field}").send("#{attribute}=", val)
  end

  define_method accessor do
    instance_variable_get("@#{field}").send(attribute)
  end
end
define_embedded_methods(field, klazz) click to toggle source

Defines accessors for embetted attributes

class Phone

attribute :home
attribute :work
attribute :mobile

end

class Contact

attribute :phone, embedded:true

end

contact = Contact.new contact.phone_home = 'xxx' contact.home_phone = 'xxx'

private

# File lib/mycrm/model.rb, line 161
def define_embedded_methods(field, klazz)
  klazz.allowed_writer_methods.each do |att|
    ['%{field}_%{attribute}', '%{attribute}_%{field}'].each do |template|
      define_embedded_method template, field, att.to_s.chomp('=')
    end
  end
end
define_formatted_methods(field) click to toggle source
# File lib/mycrm/model.rb, line 181
def define_formatted_methods(field)
  return unless attribute_to
  alias_name = field.send("to_#{attribute_to}")

  define_method "#{alias_name}=" do |val|
    send("#{field}=", val)
  end

  define_method alias_name do
    send(field)
  end
end
endpoint(endpoint) click to toggle source

macro method to setup the endpoint that will be used to connect the API

# File lib/mycrm/model.rb, line 115
def endpoint(endpoint)
  self.class_endpoint = endpoint
end
execute(action, path, body = {}, query = {}) click to toggle source
# File lib/mycrm/model.rb, line 91
      def execute(action, path, body = {}, query = {})
        raise ApiError, 'Method not allowed' if !alowed_methods.nil? && !alowed_methods.include?(action)
        Mycrm.log(:info, %{
--------- #{self.name} ---------
method:   #{action}
path:     #{uri(path, body)}
body:     #{body}
query:    #{query}
-------------------------------})
        send(action, uri(path, body), body, query).tap { |response|  Mycrm.log(:info, "response: #{response}") }
      end
find(id) click to toggle source
# File lib/mycrm/model.rb, line 82
def find(id)
  new(execute(:get, id))
end
find_by(key, value, use_key_as_path = false) click to toggle source
# File lib/mycrm/model.rb, line 86
def find_by(key, value, use_key_as_path = false)
  path = use_key_as_path ? key : nil
  new(execute(:get, path, {}, key => value))
end
list(id = nil) click to toggle source
# File lib/mycrm/model.rb, line 73
def list(id = nil)
  to_list(execute(:get, id))
end
list_by(key, value, use_key_as_path = false) click to toggle source
# File lib/mycrm/model.rb, line 77
def list_by(key, value, use_key_as_path = false)
  path = use_key_as_path ? key : nil
  to_list(execute(:get, path, {}, key => value))
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/mycrm/model.rb, line 194
def method_missing(name, *args, &block)
  name =~ /^(find|list)_by_(.*)$/ ? send("#{Regexp.last_match[1]}_by", Regexp.last_match[2], *args) : super
end
only(*methods) click to toggle source

macro method to limit the method type

# File lib/mycrm/model.rb, line 120
def only(*methods)
  self.alowed_methods = methods.collect(&:to_sym)
end
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/mycrm/model.rb, line 198
def respond_to_missing?(name, include_private = false)
  name !~ /^(find|list)_by_(.*)$/ ? super : true
end
to(mode) click to toggle source
# File lib/mycrm/model.rb, line 124
def to(mode)
  raise ApiError, "The conversion type '#{mode}' is not compatible." unless [:underscore, :camelized].include?(mode)
  self.attribute_to = mode
end
to_list(res) click to toggle source
# File lib/mycrm/model.rb, line 109
def to_list(res)
  raise ApiError, 'The output is not compatible.' unless res.is_a?(Array)
  res.each_with_object([]) { |j, list| list << new(j) }
end
uri(relative = nil, parameters = {}) click to toggle source
# File lib/mycrm/model.rb, line 103
def uri(relative = nil, parameters = {})
  format([class_endpoint, relative.to_s].compact.join('/').chomp('/'), parameters)
rescue KeyError => e
  raise ApiError, e
end