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.
- #inspect ⇒ Object
-
#new_record? ⇒ Boolean
Has this been object been created server-side? Does this by checking for an id.
- #to_s ⇒ Object
Constructor Details
#initialize(client, attributes = {}) ⇒ Data
Create a new resource instance.
29 30 31 32 33 |
# File 'lib/freshdesk_api/resource.rb', line 29 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.
9 10 11 |
# File 'lib/freshdesk_api/resource.rb', line 9 def attributes @attributes end |
#errors ⇒ Array
Returns The last received errors.
12 13 14 |
# File 'lib/freshdesk_api/resource.rb', line 12 def errors @errors end |
Class Method Details
.resource_name ⇒ Object
The resource name taken from the class name (e.g. FreshdeskAPI::SolutionCatogory -> solution_categories)
21 22 23 |
# File 'lib/freshdesk_api/resource.rb', line 21 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)
16 17 18 |
# File 'lib/freshdesk_api/resource.rb', line 16 def singular_resource_name @singular_resource_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 == object_id if other && !other.is_a?(Data) warn "Trying to compare #{other.class} to a Resource from #{caller(1..1).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
36 37 38 |
# File 'lib/freshdesk_api/resource.rb', line 36 def id attributes.key?(:id) ? attributes[:id] : nil end |
#inspect ⇒ Object
51 52 53 |
# File 'lib/freshdesk_api/resource.rb', line 51 def inspect "#<#{self.class.name} #{@attributes.to_hash.inspect}>" end |
#new_record? ⇒ Boolean
Has this been object been created server-side? Does this by checking for an id.
41 42 43 |
# File 'lib/freshdesk_api/resource.rb', line 41 def new_record? id.nil? end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/freshdesk_api/resource.rb', line 46 def to_s "#{self.class.singular_resource_name}: #{attributes.inspect}" end |