Class: Supplejack::Item
- Inherits:
-
Object
- Object
- Supplejack::Item
- Includes:
- Request
- Defined in:
- lib/supplejack/item.rb
Overview
The Item
class represents a SetItem on the Supplejack API
An Item always belongs to a UserSet. In the API the SetItem class only has a record_id and position, but it gets augmented with some of the attributes of the Record that it represents.
An Item object can have the following values:
-
record_id
-
position
If more attributes of the Record are needed, they should be added to the SetItem::ATTRIBUTES array in the API SetItem model.
Constant Summary collapse
- ATTRIBUTES =
[:record_id, :date]
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#position ⇒ Object
Returns the value of attribute position.
-
#user_set_id ⇒ Object
readonly
Returns the value of attribute user_set_id.
Instance Method Summary collapse
-
#date ⇒ Object
Date getter to force the date attribute to be a Date object.
-
#destroy ⇒ true, false
Executes a DELETE request to the API to remove the current Item.
- #id ⇒ Object
-
#initialize(attributes = {}) ⇒ Item
constructor
A new instance of Item.
- #method_missing(symbol, *args, &block) ⇒ Object
-
#save ⇒ true, false
Executes a POST request to the API to persist the current Item.
Methods included from Request
Constructor Details
#initialize(attributes = {}) ⇒ Item
Returns a new instance of Item.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/supplejack/item.rb', line 32 def initialize(attributes={}) @attributes = attributes.try(:symbolize_keys) || {} @user_set_id = @attributes[:user_set_id] @api_key = @attributes[:api_key] @position = @attributes[:position] ATTRIBUTES.each do |attribute| self.instance_variable_set("@#{attribute}", @attributes[attribute]) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
90 91 92 |
# File 'lib/supplejack/item.rb', line 90 def method_missing(symbol, *args, &block) return nil end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
30 31 32 |
# File 'lib/supplejack/item.rb', line 30 def api_key @api_key end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
29 30 31 |
# File 'lib/supplejack/item.rb', line 29 def attributes @attributes end |
#errors ⇒ Object
Returns the value of attribute errors.
30 31 32 |
# File 'lib/supplejack/item.rb', line 30 def errors @errors end |
#position ⇒ Object
Returns the value of attribute position.
30 31 32 |
# File 'lib/supplejack/item.rb', line 30 def position @position end |
#user_set_id ⇒ Object (readonly)
Returns the value of attribute user_set_id.
29 30 31 |
# File 'lib/supplejack/item.rb', line 29 def user_set_id @user_set_id end |
Instance Method Details
#date ⇒ Object
Date getter to force the date attribute to be a Date object.
85 86 87 88 |
# File 'lib/supplejack/item.rb', line 85 def date @date = @date.first if @date.is_a?(Array) Time.parse(@date) rescue nil if @date end |
#destroy ⇒ true, false
Executes a DELETE request to the API to remove the current Item
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/supplejack/item.rb', line 72 def destroy begin delete("/sets/#{self.user_set_id}/records/#{self.record_id}", {api_key: self.api_key}) Rails.cache.delete("/users/#{self.api_key}/sets") if Supplejack.enable_caching return true rescue StandardError => e self.errors = e.inspect return false end end |
#id ⇒ Object
47 48 49 |
# File 'lib/supplejack/item.rb', line 47 def id self.record_id end |
#save ⇒ true, false
Executes a POST request to the API to persist the current Item
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/supplejack/item.rb', line 55 def save begin api_attributes = {record_id: self.record_id} api_attributes[:position] = self.position if self.position.present? post("/sets/#{self.user_set_id}/records", {api_key: self.api_key}, {record: api_attributes}) Rails.cache.delete("/users/#{self.api_key}/sets") if Supplejack.enable_caching return true rescue StandardError => e self.errors = e.inspect return false end end |