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

Class Method 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



34
35
36
# File 'lib/zendesk_api/association.rb', line 34

def initialize(options = {})
  @options = SilentMash.new(options)
end

Instance Attribute Details

#optionsHash (readonly)

Returns Options passed into the association.

Returns:

  • (Hash)

    Options passed into the association



28
29
30
# File 'lib/zendesk_api/association.rb', line 28

def options
  @options
end

Class Method Details

.class_from_namespace(klass_as_string) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/zendesk_api/association.rb', line 12

def class_from_namespace(klass_as_string)
  namespaces.each do |ns|
    if module_defines_class?(ns, klass_as_string)
      return ns.const_get(klass_as_string)
    end
  end

  nil
end

.module_defines_class?(mod, klass_as_string) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/zendesk_api/association.rb', line 22

def module_defines_class?(mod, klass_as_string)
  mod.const_defined?(klass_as_string, false)
end

.namespacesObject



8
9
10
# File 'lib/zendesk_api/association.rb', line 8

def namespaces
  [ZendeskAPI] + ZendeskAPI::DataNamespace.descendants
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)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zendesk_api/association.rb', line 45

def generate_path(*args)
  options = SilentMash.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[-1] = @options[:class].resource_path
  # Remove components without path information
  ignorable_namespace_strings.each { |ns| namespace.delete(ns) }
  has_parent = namespace.size > 1 || (options[:with_parent] && @options.parent)

  if has_parent
    parent_class = @options.parent ? @options.parent.class : Association.class_from_namespace(ZendeskAPI::Helpers.modulize_string(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_path
  else
    namespace[0] = @options.path || @options[:class].resource_path
  end

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

  namespace.join("/")
end

#side_load(resources, side_loads) ⇒ Object

Tries to place side loads onto given resources.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/zendesk_api/association.rb', line 77

def side_load(resources, side_loads)
  key = "#{options.name}_id"
  plural_key = "#{Inflection.singular options.name.to_s}_ids"

  resources.each do |resource|
    if resource.key?(plural_key) # Grab associations from child_ids field on resource
      side_load_from_child_ids(resource, side_loads, plural_key)
    elsif resource.key?(key) || options.singular
      side_load_from_child_or_parent_id(resource, side_loads, key)
    else # Grab associations from parent_id field from multiple child resources
      side_load_from_parent_id(resource, side_loads, key)
    end
  end
end