Class: Ketchup::ItemArray

Inherits:
Array
  • Object
show all
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

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.

Examples:

Ketchup::ItemArray.new api, meeting, [{'content' => 'foo', #... }]

Parameters:

  • api (Ketchup::API)

    A connected API instance

  • meeting (Ketchup::Meeting)

    The meeting all items are attached to.

  • items (Array) (defaults to: [])

    An array of hashes that map to attributes of items.



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.

Examples:

meeting.items.build 'content' => 'Petty Cash'

Parameters:

  • params (Hash) (defaults to: {})

    The item’s details

Returns:



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.

Examples:

meeting.items.create 'content' => 'Petty Cash'

Parameters:

  • params (Hash) (defaults to: {})

    The item’s details

Returns:



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.

Examples:

meeting.items.reorder second_item, fourth_item, first_item, third_item

Parameters:

  • items (Ketchup::Item)

    The items of the array, in a new order.

Raises:

  • ArgumentError if you don’t provide the exact same set of 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