Module: Zendesk2::Attributes

Instance Method Summary collapse

Instance Method Details

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



27
28
29
30
# File 'lib/zendesk2/attributes.rb', line 27

def assoc_accessor(name, options = {})
  assoc_reader(name, options)
  assoc_writer(name, options)
end

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



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/zendesk2/attributes.rb', line 3

def assoc_reader(name, options = {})
  assoc_key  = options[:key] || "#{name}_id"
  collection = options[:collection] || "#{name}s"
  define_method(name) do
    assoc_id = send(assoc_key)
    if assoc_id
      cistern.send(collection).get(assoc_id)
    else
      instance_variable_get("@#{name}")
    end
  end
end

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



16
17
18
19
20
21
22
23
24
25
# File 'lib/zendesk2/attributes.rb', line 16

def assoc_writer(name, options = {})
  assoc_key = options[:key] || "#{name}_id"
  define_method("#{name}=") do |assoc|
    if assoc.is_a?(Cistern::Model)
      send("#{assoc_key}=", assoc.identity)
    else
      instance_variable_set("@#{name}", assoc)
    end
  end
end