Class: ZendeskAPI::Data
- Inherits:
-
Object
- Object
- ZendeskAPI::Data
- Includes:
- Associations, Rescue
- Defined in:
- lib/zendesk_api/resource.rb
Overview
Represents a resource that only holds data.
Direct Known Subclasses
Attachment, DataResource, TicketComment, Upload, ViewExecution
Instance Attribute Summary collapse
-
#association ⇒ ZendeskAPI::Association
The association.
-
#attributes ⇒ Hash
(also: #to_param)
readonly
The resource’s attributes.
Class Method Summary collapse
- .only_send_unnested_params ⇒ Object
-
.resource_name ⇒ Object
(also: model_key)
The resource name taken from the class name (e.g. ZendeskAPI::Ticket -> tickets).
-
.singular_resource_name ⇒ Object
The singular resource name taken from the class name (e.g. ZendeskAPI::Ticket -> ticket).
- .unnested_params ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql)
-
#id ⇒ Object
(also: #hash)
Returns the resource id of the object or nil.
-
#initialize(client, attributes = {}) ⇒ Data
constructor
Create a new resource instance.
-
#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(*args) ⇒ Object
Returns the path to the resource.
- #to_s ⇒ Object (also: #inspect)
Methods included from Associations
Constructor Details
#initialize(client, attributes = {}) ⇒ Data
Create a new resource instance.
47 48 49 50 51 52 53 54 55 |
# File 'lib/zendesk_api/resource.rb', line 47 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) @client = client @attributes = ZendeskAPI::Trackie.new(attributes) ZendeskAPI::Client.check_deprecated_namespace_usage @attributes, self.class.singular_resource_name @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.
59 60 61 62 |
# File 'lib/zendesk_api/resource.rb', line 59 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.
42 43 44 |
# File 'lib/zendesk_api/resource.rb', line 42 def association @association end |
#attributes ⇒ Hash (readonly) Also known as: to_param
Returns The resource’s attributes.
40 41 42 |
# File 'lib/zendesk_api/resource.rb', line 40 def attributes @attributes end |
Class Method Details
.only_send_unnested_params ⇒ Object
30 31 32 |
# File 'lib/zendesk_api/resource.rb', line 30 def only_send_unnested_params @unnested_params = true end |
.resource_name ⇒ Object Also known as: model_key
The resource name taken from the class name (e.g. ZendeskAPI::Ticket -> tickets)
19 20 21 |
# File 'lib/zendesk_api/resource.rb', line 19 def resource_name @resource_name ||= singular_resource_name.plural end |
.singular_resource_name ⇒ Object
The singular resource name taken from the class name (e.g. ZendeskAPI::Ticket -> ticket)
14 15 16 |
# File 'lib/zendesk_api/resource.rb', line 14 def singular_resource_name @singular_resource_name ||= to_s.split("::").last.snakecase end |
.unnested_params ⇒ Object
34 35 36 |
# File 'lib/zendesk_api/resource.rb', line 34 def unnested_params @unnested_params ||= false end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql
84 85 86 87 |
# File 'lib/zendesk_api/resource.rb', line 84 def ==(other) warn "Trying to compare #{other.class} to a Resource" if other && !other.is_a?(Data) other.is_a?(self.class) && ((other.id && other.id == id) || (other.object_id == self.object_id)) end |
#id ⇒ Object Also known as: hash
Returns the resource id of the object or nil
65 66 67 |
# File 'lib/zendesk_api/resource.rb', line 65 def id key?(:id) ? method_missing(:id) : nil end |
#new_record? ⇒ Boolean
Has this been object been created server-side? Does this by checking for an id.
70 71 72 |
# File 'lib/zendesk_api/resource.rb', line 70 def new_record? id.nil? end |
#path(*args) ⇒ Object
Returns the path to the resource
75 76 77 |
# File 'lib/zendesk_api/resource.rb', line 75 def path(*args) @association.generate_path(self, *args) end |
#to_s ⇒ Object Also known as: inspect
79 80 81 |
# File 'lib/zendesk_api/resource.rb', line 79 def to_s "#{self.class.singular_resource_name}: #{attributes.inspect}" end |