Class: Ketchup::ItemArray
- Inherits:
-
Array
- Object
- Array
- Ketchup::ItemArray
- Defined in:
- lib/ketchup/item_array.rb
Overview
A collection of items for a specific meeting, and allows for the creation of new items, and re-ordering of the full set of items. It’s really just a slightly special subclass of Array.
Instance Method Summary collapse
-
#build(params = {}) ⇒ Ketchup::Item
Create a new unsaved item object.
-
#create(params = {}) ⇒ Ketchup::Item
Create a new (saved) item object.
-
#initialize(api, meeting, items = []) ⇒ ItemArray
constructor
Create a new ItemArray for a given api, meeting, and set of items.
-
#reorder(*items) ⇒ Object
Re-order the items in this array, by passing them through in the order you would prefer.
Constructor Details
#initialize(api, meeting, items = []) ⇒ ItemArray
Create a new ItemArray for a given api, meeting, and set of items. The set of items should be hashes taken from the API server, not actual item objects.
Like many of the methods in this library, you really don’t need to call this one yourself. A meeting object will create its own ItemArray without any help.
21 22 23 24 25 26 27 28 |
# File 'lib/ketchup/item_array.rb', line 21 def initialize(api, meeting, items = []) @api = api @meeting = meeting replace items.collect { |hash| Ketchup::Item.new(@api, @meeting, hash) } end |
Instance Method Details
#build(params = {}) ⇒ Ketchup::Item
Create a new unsaved item object. The only parameter you really need to worry about is the content - the rest is all internal values, managed by the API server.
40 41 42 43 44 |
# File 'lib/ketchup/item_array.rb', line 40 def build(params = {}) item = Ketchup::Item.new @api, @meeting, params push item item end |
#create(params = {}) ⇒ Ketchup::Item
Create a new (saved) item object. The only parameter you really need to worry about is the content - the rest is all internal values, managed by the API server.
56 57 58 59 60 |
# File 'lib/ketchup/item_array.rb', line 56 def create(params = {}) item = build(params) item.save item end |
#reorder(*items) ⇒ Object
Re-order the items in this array, by passing them through in the order you would prefer. This makes the change directly to the API server, as well as within this array.
Make sure you’re passing in all the items that are in this array, and no others. This can’t be used as a shortcut to add new items.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ketchup/item_array.rb', line 75 def reorder(*items) if items.collect(&:shortcode_url).sort != collect(&:shortcode_url).sort raise ArgumentError, 'cannot sort a different set of items' end @api.put "/meetings/#{@meeting.shortcode_url}/sort_items.json", { 'items' => items.collect(&:shortcode_url) } replace items end |