Class: ZendeskAPI::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/zendesk_api/association.rb

Overview

Represents an association between two resources

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Association

Options to pass in

  • class - Required

  • parent - Parent instance

  • path - Optional path instead of resource name



11
12
13
# File 'lib/zendesk_api/association.rb', line 11

def initialize(options = {})
  @options = Hashie::Mash.new(options)
end

Instance Attribute Details

#optionsHash (readonly)

Returns Options passed into the association.

Returns:

  • (Hash)

    Options passed into the association



5
6
7
# File 'lib/zendesk_api/association.rb', line 5

def options
  @options
end

Instance Method Details

#generate_path(*args) ⇒ Object

Generate a path to the resource. id and <parent>_id attributes will be deleted from passed in options hash if they are used in the built path. Arguments that can be passed in: An instance, any resource instance Hash Options:

  • with_parent - Include the parent path (false by default)

  • with_id - Include the instance id, if possible (true)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zendesk_api/association.rb', line 22

def generate_path(*args)
  options = Hashie::Mash.new(:with_id => true)
  if args.last.is_a?(Hash)
    original_options = args.pop
    options.merge!(original_options)
  end

  instance = args.first

  namespace = @options[:class].to_s.split("::")
  namespace.delete("ZendeskAPI")
  has_parent = namespace.size > 1 || (options[:with_parent] && @options.parent)

  if has_parent
    parent_class = @options.parent ? @options.parent.class : ZendeskAPI.get_class(namespace[0])
    parent_namespace = build_parent_namespace(parent_class, instance, options, original_options)
    namespace[1..1] = parent_namespace if parent_namespace
    namespace[0] = parent_class.resource_name
  else
    namespace[0] = @options.path || @options[:class].resource_name
  end

  if id = extract_id(instance, options, original_options)
    namespace << id
  end

  namespace.join("/")
end