Class: ZendeskAPI::Data
- Inherits:
-
Object
- Object
- ZendeskAPI::Data
- Includes:
- Associations
- Defined in:
- lib/zendesk_api/resource.rb
Overview
Represents a resource that only holds data.
Direct Known Subclasses
App::Upload, Attachment, DataResource, RuleExecution, Search::Result, Ticket::Audit::Event, TicketEvent::Event, TopicComment, Upload
Instance Attribute Summary collapse
-
#association ⇒ ZendeskAPI::Association
The association.
-
#attributes ⇒ Hash
(also: #to_param)
readonly
The resource’s attributes.
-
#errors ⇒ Array
The last received errors.
-
#response ⇒ Object
Place to dump the last response.
Class Method Summary collapse
- .inherited(klass) ⇒ Object
- .namespace(namespace) ⇒ Object
- .new_from_response(client, response, includes = nil) ⇒ Object
-
.resource_name ⇒ Object
(also: model_key)
The resource name taken from the class name (e.g. ZendeskAPI::Ticket -> tickets).
- .resource_path ⇒ Object
-
.singular_resource_name ⇒ Object
The singular resource name taken from the class name (e.g. ZendeskAPI::Ticket -> ticket).
- .subclasses ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql)
Compares resources by class and id.
-
#id ⇒ Object
Returns the resource id of the object or nil.
-
#initialize(client, attributes = {}) ⇒ Data
constructor
Create a new resource instance.
- #loaded_associations ⇒ Object
-
#method_missing(*args, &block) ⇒ Object
Passes the method onto the attributes hash.
-
#new_record? ⇒ Boolean
Has this been object been created server-side? Does this by checking for an id.
-
#path(options = {}) ⇒ Object
Returns the path to the resource.
-
#to_json(*args) ⇒ Object
Passes #to_json to the underlying attributes hash.
- #to_s ⇒ Object (also: #inspect)
Methods included from Associations
Constructor Details
#initialize(client, attributes = {}) ⇒ Data
Create a new resource instance.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/zendesk_api/resource.rb', line 55 def initialize(client, attributes = {}) raise "Expected a Hash for attributes, got #{attributes.inspect}" unless attributes.is_a?(Hash) @association = attributes.delete(:association) || Association.new(:class => self.class) @global_params = attributes.delete(:global) || {} @client = client @attributes = ZendeskAPI::Trackie.new(attributes) if self.class.associations.none? {|a| a[:name] == self.class.singular_resource_name} ZendeskAPI::Client.check_deprecated_namespace_usage @attributes, self.class.singular_resource_name end @attributes.clear_changes unless new_record? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
Passes the method onto the attributes hash. If the attributes are nested (e.g. { :tickets => { :id => 1 } }), passes the method onto the nested hash.
79 80 81 82 |
# File 'lib/zendesk_api/resource.rb', line 79 def method_missing(*args, &block) raise NoMethodError, ":save is not defined" if args.first.to_sym == :save @attributes.send(*args, &block) end |
Instance Attribute Details
#association ⇒ ZendeskAPI::Association
Returns The association.
46 47 48 |
# File 'lib/zendesk_api/resource.rb', line 46 def association @association end |
#attributes ⇒ Hash (readonly) Also known as: to_param
Returns The resource’s attributes.
44 45 46 |
# File 'lib/zendesk_api/resource.rb', line 44 def attributes @attributes end |
#errors ⇒ Array
Returns The last received errors.
48 49 50 |
# File 'lib/zendesk_api/resource.rb', line 48 def errors @errors end |
#response ⇒ Object
Place to dump the last response
50 51 52 |
# File 'lib/zendesk_api/resource.rb', line 50 def response @response end |
Class Method Details
.inherited(klass) ⇒ Object
14 15 16 |
# File 'lib/zendesk_api/resource.rb', line 14 def inherited(klass) subclasses.push(klass) end |
.namespace(namespace) ⇒ Object
38 39 40 |
# File 'lib/zendesk_api/resource.rb', line 38 def namespace(namespace) @namespace = namespace end |
.new_from_response(client, response, includes = nil) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/zendesk_api/resource.rb', line 69 def self.new_from_response(client, response, includes = nil) new(client).tap do |resource| resource.handle_response(response) resource.set_includes(resource, includes, response.body) if includes resource.attributes.clear_changes end end |
.resource_name ⇒ Object Also known as: model_key
The resource name taken from the class name (e.g. ZendeskAPI::Ticket -> tickets)
28 29 30 |
# File 'lib/zendesk_api/resource.rb', line 28 def resource_name @resource_name ||= Inflection.plural(singular_resource_name) end |
.resource_path ⇒ Object
32 33 34 |
# File 'lib/zendesk_api/resource.rb', line 32 def resource_path [@namespace, resource_name].compact.join("/") end |
.singular_resource_name ⇒ Object
The singular resource name taken from the class name (e.g. ZendeskAPI::Ticket -> ticket)
23 24 25 |
# File 'lib/zendesk_api/resource.rb', line 23 def singular_resource_name @singular_resource_name ||= ZendeskAPI::Helpers.snakecase_string(to_s.split("::").last) end |
.subclasses ⇒ Object
18 19 20 |
# File 'lib/zendesk_api/resource.rb', line 18 def subclasses @subclasses ||= [] end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql
Compares resources by class and id. If id is nil, then by object_id
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/zendesk_api/resource.rb', line 119 def ==(other) return true if other.object_id == self.object_id if other && !(other.is_a?(Data) || other.is_a?(Integer)) warn "Trying to compare #{other.class} to a Resource from #{caller.first}" end if other.is_a?(Data) other.id && other.id == id elsif other.is_a?(Integer) id == other else false end end |
#id ⇒ Object
Returns the resource id of the object or nil
85 86 87 |
# File 'lib/zendesk_api/resource.rb', line 85 def id key?(:id) ? method_missing(:id) : nil end |
#loaded_associations ⇒ Object
95 96 97 98 99 100 |
# File 'lib/zendesk_api/resource.rb', line 95 def loaded_associations self.class.associations.select do |association| loaded = @attributes.method_missing(association[:name]) loaded && !(loaded.respond_to?(:empty?) && loaded.empty?) end end |
#new_record? ⇒ Boolean
Has this been object been created server-side? Does this by checking for an id.
90 91 92 |
# File 'lib/zendesk_api/resource.rb', line 90 def new_record? id.nil? end |
#path(options = {}) ⇒ Object
Returns the path to the resource
103 104 105 |
# File 'lib/zendesk_api/resource.rb', line 103 def path( = {}) @association.generate_path(self, ) end |
#to_json(*args) ⇒ Object
Passes #to_json to the underlying attributes hash
108 109 110 |
# File 'lib/zendesk_api/resource.rb', line 108 def to_json(*args) method_missing(:to_json, *args) end |
#to_s ⇒ Object Also known as: inspect
113 114 115 |
# File 'lib/zendesk_api/resource.rb', line 113 def to_s "#{self.class.singular_resource_name}: #{attributes.inspect}" end |