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
Constructor Details
#initialize(user) ⇒ UserSetRelation
Returns a new instance of UserSetRelation.
25 26 27 |
# File 'lib/supplejack/user_set_relation.rb', line 25 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
138 139 140 |
# File 'lib/supplejack/user_set_relation.rb', line 138 def method_missing(method, *args, &block) sets.send(method, *args, &block) end |
Instance Attribute Details
#user ⇒ Object (readonly)
Returns the value of attribute user.
23 24 25 |
# File 'lib/supplejack/user_set_relation.rb', line 23 def user @user end |
Instance Method Details
#all ⇒ Object
Returns an array of all UserSet objects for the current User
127 128 129 |
# File 'lib/supplejack/user_set_relation.rb', line 127 def all self.sets end |
#build(attributes = {}) ⇒ Supplejack::UserSet
Initializes a new UserSet object and links it to the current User
87 88 89 90 91 |
# File 'lib/supplejack/user_set_relation.rb', line 87 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
99 100 101 102 103 |
# File 'lib/supplejack/user_set_relation.rb', line 99 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.
39 40 41 42 43 |
# File 'lib/supplejack/user_set_relation.rb', line 39 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
77 78 79 |
# File 'lib/supplejack/user_set_relation.rb', line 77 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.
115 116 117 118 119 120 121 122 123 |
# File 'lib/supplejack/user_set_relation.rb', line 115 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
31 32 33 |
# File 'lib/supplejack/user_set_relation.rb', line 31 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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/supplejack/user_set_relation.rb', line 51 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 |