Class: FreshdeskAPI::Data
- Inherits:
-
Object
- Object
- FreshdeskAPI::Data
- Defined in:
- lib/freshdesk_api/resource.rb
Overview
Represents a resource that only holds data.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
The resource’s attributes.
-
#errors ⇒ Array
The last received errors.
Class Method Summary collapse
-
.resource_name ⇒ Object
The resource name taken from the class name (e.g. FreshdeskAPI::SolutionCatogory -> solution_categories).
-
.singular_resource_name ⇒ Object
The singular resource name taken from the class name (e.g. FreshdeskAPI::SoulutionCategory -> solution_category).
Instance Method Summary collapse
-
#==(other) ⇒ Object
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.
-
#new_record? ⇒ Boolean
Has this been object been created server-side? Does this by checking for an id.
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(client, attributes = {}) ⇒ Data
Create a new resource instance.
28 29 30 31 32 |
# File 'lib/freshdesk_api/resource.rb', line 28 def initialize(client, attributes = {}) raise "Expected a Hash for attributes, got #{attributes.inspect}" unless attributes.is_a?(Hash) @client = client @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns The resource’s attributes.
7 8 9 |
# File 'lib/freshdesk_api/resource.rb', line 7 def attributes @attributes end |
#errors ⇒ Array
Returns The last received errors.
10 11 12 |
# File 'lib/freshdesk_api/resource.rb', line 10 def errors @errors end |
Class Method Details
.resource_name ⇒ Object
The resource name taken from the class name (e.g. FreshdeskAPI::SolutionCatogory -> solution_categories)
20 21 22 |
# File 'lib/freshdesk_api/resource.rb', line 20 def resource_name @resource_name ||= singular_resource_name.pluralize end |
.singular_resource_name ⇒ Object
The singular resource name taken from the class name (e.g. FreshdeskAPI::SoulutionCategory -> solution_category)
14 15 16 17 |
# File 'lib/freshdesk_api/resource.rb', line 14 def singular_resource_name @singular_respurce_name ||= to_s.split('::').last.underscore end |
Instance Method Details
#==(other) ⇒ Object
Compares resources by class and id. If id is nil, then by object_id
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/freshdesk_api/resource.rb', line 56 def ==(other) return true if other.object_id == self.object_id if other && !other.is_a?(Data) warn "Trying to compare #{other.class} to a Resource from #{caller.first}" end if other.is_a?(Data) other.id && other.id == id else false end end |
#id ⇒ Object
Returns the resource id of the object or nil
35 36 37 |
# File 'lib/freshdesk_api/resource.rb', line 35 def id attributes.key?(:id) ? attributes[:id] : nil end |
#new_record? ⇒ Boolean
Has this been object been created server-side? Does this by checking for an id.
40 41 42 |
# File 'lib/freshdesk_api/resource.rb', line 40 def new_record? id.nil? end |
#to_s ⇒ Object Also known as: inspect
45 46 47 |
# File 'lib/freshdesk_api/resource.rb', line 45 def to_s "#{self.class.singular_resource_name}: #{attributes.inspect}" end |