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.
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 = Hashie::Mash.new() end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns Options passed into the association.
5 6 7 |
# File 'lib/zendesk_api/association.rb', line 5 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)
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) = 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 |