Sha256: 9e3e6b4ac3403fdca504e5e7a0cb23f557d3963892082d4469035756bcd9c089

Contents?: true

Size: 830 Bytes

Versions: 6

Compression:

Stored size: 830 Bytes

Contents

# frozen_string_literal: true
module Zendesk2::Attributes
  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

  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

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

  # TODO: collection
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
zendesk2-1.15.0 lib/zendesk2/attributes.rb
zendesk2-1.13.1 lib/zendesk2/attributes.rb
zendesk2-1.13.0 lib/zendesk2/attributes.rb
zendesk2-1.12.0 lib/zendesk2/attributes.rb
zendesk2-1.11.0 lib/zendesk2/attributes.rb
zendesk2-1.10.0 lib/zendesk2/attributes.rb