Class: Supplejack::UserSetRelation
- Inherits:
-
Object
- Object
- Supplejack::UserSetRelation
- Includes:
- Request
- Defined in:
- lib/supplejack/user_set_relation.rb
Overview
The UserSetRelation
class provides ActiveRecord like functionality to the relationship between a User object and it’s UserSet objects.
Instance Attribute Summary collapse
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#all ⇒ Object
Returns an array of all UserSet objects for the current User.
-
#build(attributes = {}) ⇒ Supplejack::UserSet
Initializes a new UserSet object and links it to the current User.
-
#create(attributes = {}) ⇒ Supplejack::UserSet
Build and persist a UserSet object with the provided attributes.
-
#fetch_sets ⇒ Array
Initialize an array of UserSet objects and orders them by priority.
-
#find(user_set_id) ⇒ Supplejack::UserSet
Finds a UserSet object with the provided ID that belongs to the current User.
-
#initialize(user) ⇒ UserSetRelation
constructor
A new instance of UserSetRelation.
-
#method_missing(method, *args, &block) ⇒ Object
Any method missing on this class is delegated to the UserSet objects array so that the developer can easily execute any Array method on the UserSetRelation.
-
#order(attribute) ⇒ Array
Return a array of UserSet objects ordered by the priority first and then in ascending order by the specified attribute.
-
#sets ⇒ Object
Returns an array of UserSet objects and memoizes the array.
-
#sets_response ⇒ Hash
Execute a GET request to the API to retrieve the user_sets from a User.
Methods included from Request
#delete, #get, #patch, #post, #put
Constructor Details
#initialize(user) ⇒ UserSetRelation
Returns a new instance of UserSetRelation.
27 28 29 |
# File 'lib/supplejack/user_set_relation.rb', line 27 def initialize(user) @user = user end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Any method missing on this class is delegated to the UserSet objects array so that the developer can easily execute any Array method on the UserSetRelation
140 141 142 |
# File 'lib/supplejack/user_set_relation.rb', line 140 def method_missing(method, *args, &block) sets.send(method, *args, &block) end |
Instance Attribute Details
#user ⇒ Object (readonly)
Returns the value of attribute user.
25 26 27 |
# File 'lib/supplejack/user_set_relation.rb', line 25 def user @user end |
Instance Method Details
#all ⇒ Object
Returns an array of all UserSet objects for the current User
129 130 131 |
# File 'lib/supplejack/user_set_relation.rb', line 129 def all self.sets end |
#build(attributes = {}) ⇒ Supplejack::UserSet
Initializes a new UserSet object and links it to the current User
89 90 91 92 93 |
# File 'lib/supplejack/user_set_relation.rb', line 89 def build(attributes={}) user_set = Supplejack::UserSet.new(attributes) user_set.api_key = user.api_key user_set end |
#create(attributes = {}) ⇒ Supplejack::UserSet
Build and persist a UserSet object with the provided attributes
101 102 103 104 105 |
# File 'lib/supplejack/user_set_relation.rb', line 101 def create(attributes={}) user_set = self.build(attributes) user_set.save user_set end |
#fetch_sets ⇒ Array
Initialize an array of UserSet objects and orders them by priority.
41 42 43 44 45 |
# File 'lib/supplejack/user_set_relation.rb', line 41 def fetch_sets response = sets_response sets_array = response["sets"] || [] @sets = sets_array.map {|attributes| Supplejack::UserSet.new(attributes) }.sort_by { |set| set.priority } end |
#find(user_set_id) ⇒ Supplejack::UserSet
Finds a UserSet object with the provided ID that belongs to the current User
79 80 81 |
# File 'lib/supplejack/user_set_relation.rb', line 79 def find(user_set_id) Supplejack::UserSet.find(user_set_id, self.user.api_key) end |
#order(attribute) ⇒ Array
Return a array of UserSet objects ordered by the priority first and then in ascending order by the specified attribute.
The only exception is for the “updated_at” attribute, for which ignores the priority and orders on descending order.
117 118 119 120 121 122 123 124 125 |
# File 'lib/supplejack/user_set_relation.rb', line 117 def order(attribute) @sets = sets.sort_by do |set| value = set.send(attribute) value = value.downcase if value.is_a?(String) attribute == :updated_at ? value : [set.priority, value] end @sets = @sets.reverse if attribute == :updated_at @sets end |
#sets ⇒ Object
Returns an array of UserSet objects and memoizes the array
33 34 35 |
# File 'lib/supplejack/user_set_relation.rb', line 33 def sets @sets ||= self.fetch_sets end |
#sets_response ⇒ Hash
Execute a GET request to the API to retrieve the user_sets from a User. It caches the response from the API for a day. The cache is invalidated every time any set for the user changes.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/supplejack/user_set_relation.rb', line 53 def sets_response params = {} if user.use_own_api_key? path = "/sets" params[:api_key] = user.api_key else path = "/users/#{user.api_key}/sets" end if Supplejack.enable_caching Rails.cache.fetch(path, expires_in: 1.day) do get(path, params) end else get(path, params) end end |