Module: Mycrm::Model::ClassMethods

Defined in:
lib/mycrm/model.rb

Overview

provides method at class level

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



194
195
196
# 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

Class Method Details

.extended(descendant) ⇒ Object



67
68
69
70
71
# 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

Instance Method Details

#attribute(name, klazz, options = {}) ⇒ Object

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



131
132
133
134
135
136
137
138
139
140
141
142
# 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) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
# 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) ⇒ Object

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



161
162
163
164
165
166
167
# 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) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# 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) ⇒ Object

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



115
116
117
# File 'lib/mycrm/model.rb', line 115

def endpoint(endpoint)
  self.class_endpoint = endpoint
end

#execute(action, path, body = {}, query = {}) ⇒ Object

Raises:



91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Object



82
83
84
# File 'lib/mycrm/model.rb', line 82

def find(id)
  new(execute(:get, id))
end

#find_by(key, value, use_key_as_path = false) ⇒ Object



86
87
88
89
# 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) ⇒ Object



73
74
75
# 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) ⇒ Object



77
78
79
80
# 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

#only(*methods) ⇒ Object

macro method to limit the method type



120
121
122
# 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) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/mycrm/model.rb', line 198

def respond_to_missing?(name, include_private = false)
  name !~ /^(find|list)_by_(.*)$/ ? super : true
end

#to(mode) ⇒ Object

Raises:



124
125
126
127
# 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) ⇒ Object

Raises:



109
110
111
112
# 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 = {}) ⇒ Object



103
104
105
106
107
# 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