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
Constructor Details
#initialize(user_set) ⇒ ItemRelation
Returns a new instance of ItemRelation.
24 25 26 27 28 29 30 |
# File 'lib/supplejack/item_relation.rb', line 24 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
71 72 73 |
# File 'lib/supplejack/item_relation.rb', line 71 def method_missing(method, *args, &block) @items.send(method, *args, &block) end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
22 23 24 |
# File 'lib/supplejack/item_relation.rb', line 22 def items @items end |
#user_set ⇒ Object (readonly)
Returns the value of attribute user_set.
22 23 24 |
# File 'lib/supplejack/item_relation.rb', line 22 def user_set @user_set end |
Instance Method Details
#all ⇒ Object
Returns an Array with all items for the current UserSet
34 35 36 |
# File 'lib/supplejack/item_relation.rb', line 34 def all @items end |
#build(attributes = {}) ⇒ Supplejack::Item
Initializes a new Item with the provided attributes
48 49 50 51 52 53 |
# File 'lib/supplejack/item_relation.rb', line 48 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
59 60 61 62 |
# File 'lib/supplejack/item_relation.rb', line 59 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.
40 41 42 |
# File 'lib/supplejack/item_relation.rb', line 40 def find(record_id) @items.detect {|i| i.record_id == record_id.to_i} end |