Class: ZendeskAPI::Association
- Inherits:
-
Object
- Object
- ZendeskAPI::Association
- Defined in:
- lib/zendesk_api/association.rb
Overview
Represents an association between two resources
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Options passed into the association.
Class Method Summary collapse
- .class_from_namespace(klass_as_string) ⇒ Object
-
.module_defines_class?(mod, klass_as_string) ⇒ Boolean
1.9+ changed default to search ancestors, added flag to disable behavior.
- .namespaces ⇒ Object
Instance Method Summary collapse
-
#generate_path(*args) ⇒ Object
Generate a path to the resource.
-
#initialize(options = {}) ⇒ Association
constructor
Options to pass in * class - Required * parent - Parent instance * path - Optional path instead of resource name.
-
#side_load(resources, side_loads) ⇒ Object
Tries to place side loads onto given resources.
Constructor Details
#initialize(options = {}) ⇒ Association
Options to pass in
-
class - Required
-
parent - Parent instance
-
path - Optional path instead of resource name
39 40 41 |
# File 'lib/zendesk_api/association.rb', line 39 def initialize( = {}) @options = SilentMash.new() end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns Options passed into the association.
33 34 35 |
# File 'lib/zendesk_api/association.rb', line 33 def @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
1.9+ changed default to search ancestors, added flag to disable behavior.
23 24 25 26 27 28 29 |
# File 'lib/zendesk_api/association.rb', line 23 def module_defines_class?(mod, klass_as_string) if RUBY_VERSION < '1.9' mod.const_defined?(klass_as_string) else mod.const_defined?(klass_as_string, false) end end |
.namespaces ⇒ Object
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)
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 75 76 77 78 79 80 |
# File 'lib/zendesk_api/association.rb', line 50 def generate_path(*args) = SilentMash.new(:with_id => true) if args.last.is_a?(Hash) = args.pop .merge!() 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 || ([: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, , ) 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, , ) namespace << id end namespace.join("/") end |
#side_load(resources, side_loads) ⇒ Object
Tries to place side loads onto given resources.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/zendesk_api/association.rb', line 83 def side_load(resources, side_loads) key = "#{.name}_id" plural_key = "#{Inflection.singular .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) || .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 |