Class: Supplejack::ItemRelation
- Inherits:
-
Object
- Object
- Supplejack::ItemRelation
- Includes:
- Request
- Defined in:
- lib/supplejack/item_relation.rb
Overview
The ItemRelation
class provides ActiveRecord like functionality to the relationship between a UserSet object and it’s Item objects.
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#user_set ⇒ Object
readonly
Returns the value of attribute user_set.
Instance Method Summary collapse
-
#all ⇒ Object
Returns an Array with all items for the current UserSet.
-
#build(attributes = {}) ⇒ Supplejack::Item
Initializes a new Item with the provided attributes.
-
#create(attributes = {}) ⇒ true, false
Initializes and persists the Item to the API.
-
#find(record_id) ⇒ Object
Finds the item based on the record_id and returns it.
-
#initialize(user_set) ⇒ ItemRelation
constructor
A new instance of ItemRelation.
-
#method_missing(method, *args, &block) ⇒ Object
Any method missing on this class is delegated to the Item objects array so that the developer can easily execute any Array method on the ItemRelation.
Methods included from Request
#delete, #get, #patch, #post, #put
Constructor Details
#initialize(user_set) ⇒ ItemRelation
Returns a new instance of ItemRelation.
26 27 28 29 30 31 32 |
# File 'lib/supplejack/item_relation.rb', line 26 def initialize(user_set) @user_set = user_set items_array = user_set.attributes[:records] || [] @items = items_array.map do |hash| Supplejack::Item.new(hash.merge(user_set_id: user_set.id, api_key: user_set.api_key)) end 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 Item objects array so that the developer can easily execute any Array method on the ItemRelation
74 75 76 |
# File 'lib/supplejack/item_relation.rb', line 74 def method_missing(method, *args, &block) @items.send(method, *args, &block) end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
24 25 26 |
# File 'lib/supplejack/item_relation.rb', line 24 def items @items end |
#user_set ⇒ Object (readonly)
Returns the value of attribute user_set.
24 25 26 |
# File 'lib/supplejack/item_relation.rb', line 24 def user_set @user_set end |
Instance Method Details
#all ⇒ Object
Returns an Array with all items for the current UserSet
36 37 38 |
# File 'lib/supplejack/item_relation.rb', line 36 def all @items end |
#build(attributes = {}) ⇒ Supplejack::Item
Initializes a new Item with the provided attributes
50 51 52 53 54 55 |
# File 'lib/supplejack/item_relation.rb', line 50 def build(attributes={}) attributes ||= {} attributes[:user_set_id] = self.user_set.id attributes[:api_key] = self.user_set.api_key Supplejack::Item.new(attributes) end |
#create(attributes = {}) ⇒ true, false
Initializes and persists the Item to the API
61 62 63 64 65 |
# File 'lib/supplejack/item_relation.rb', line 61 def create(attributes={}) item = self.build(attributes) item.save end |
#find(record_id) ⇒ Object
Finds the item based on the record_id and returns it.
42 43 44 |
# File 'lib/supplejack/item_relation.rb', line 42 def find(record_id) @items.detect {|i| i.record_id == record_id.to_i} end |