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.
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
Constructor Details
#initialize(options = {}) ⇒ Association
Options to pass in
-
class - Required
-
parent - Parent instance
-
path - Optional path instead of resource name
13 14 15 |
# File 'lib/zendesk_api/association.rb', line 13 def initialize( = {}) @options = Hashie::Mash.new() end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns Options passed into the association.
7 8 9 |
# File 'lib/zendesk_api/association.rb', line 7 def @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)
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 50 51 |
# File 'lib/zendesk_api/association.rb', line 24 def generate_path(*args) = Hashie::Mash.new(:with_id => true) if args.last.is_a?(Hash) = args.pop .merge!() end instance = args.first namespace = @options[:class].to_s.split("::") namespace.delete("ZendeskAPI") has_parent = namespace.size > 1 || ([: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, , ) 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, , ) namespace << id end namespace.join("/") end |
#side_load(resources, side_loads) ⇒ Object
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 81 82 83 84 85 86 87 88 89 |
# File 'lib/zendesk_api/association.rb', line 53 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 ids = resource.send(plural_key) resource.send("#{.name}=", _side_load(resource, side_loads.select {|side_load| ids.include?(side_load[.include_key]) })) elsif resource.key?(key) || .singular # Either grab association from child_id field on resource or parent_id on child resource if resource.key?(key) id = resource.send(key) key = .include_key else id = resource.id key = "#{resource.class.singular_resource_name}_id" end next unless id side_load = side_loads.detect do |side_load| id == side_load[key] end resource.send("#{.name}=", side_load) if side_load else # Grab associations from parent_id field from multiple child resources key = "#{resource.class.singular_resource_name}_id" resource.send("#{.name}=", _side_load(resource, side_loads.select {|side_load| side_load[key] == resource.id })) end end end |