Module: AutotaskRuby::Association

Included in:
Entity
Defined in:
lib/autotask_ruby/association.rb

Overview

handles loading associated entities.

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, options = {}) ⇒ Object

loads parent entity. Thanks to scoop for this example.



8
9
10
11
12
13
14
15
# File 'lib/autotask_ruby/association.rb', line 8

def belongs_to(name, options = {})
  name = name.to_s
  klass = (options[:class_name] || name).to_s.classify.to_s
  foreign_key = name.foreign_key
  define_singleton_method(name) do
    find(klass, send(foreign_key))
  end
end

#has_many(name, options = {}) ⇒ Object

loads child entities. Example: A project can have many tasks. Thanks to scoop for this example.



31
32
33
34
35
36
37
38
# File 'lib/autotask_ruby/association.rb', line 31

def has_many(name, options = {})
  name = name.to_s
  options.reverse_merge! foreign_key: to_s.foreign_key.camelize
  klass = (options[:class_name] || name).to_s.classify.to_s
  define_singleton_method(name) do
    query(klass, options[:foreign_key], id)
  end
end

#has_one(name, options = {}) ⇒ Object

Example, an appointment can have one contact. Thanks to scoop for this example.



19
20
21
22
23
24
25
26
# File 'lib/autotask_ruby/association.rb', line 19

def has_one(name, options = {})
  name = name.to_s
  options.reverse_merge! foreign_key: to_s.foreign_key.camelize
  klass = (options[:class_name] || name).to_s.classify.to_s
  define_singleton_method(name) do
    find(klass, options[:foreign_key], id).first
  end
end