module Zendesk2::Attributes

Public Instance Methods

assoc_accessor(name, options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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