Class: Ketchup::NoteArray

Inherits:
Array
  • Object
show all
Defined in:
lib/ketchup/note_array.rb

Overview

A collection of notes for a specific item, and allows for the creation of new notes, and re-ordering of the full set of notes. It’s really just a slightly special subclass of Array.

Instance Method Summary collapse

Constructor Details

#initialize(api, item, notes = []) ⇒ NoteArray

Create a new NoteArray for a given api, item, and set of notes. The set of notes should be hashes taken from the API server, not actual note objects.

Like many of the methods in this library, you really don’t need to call this one yourself. An item object will create its own NoteArray without any help.

Examples:

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

Parameters:

  • api (Ketchup::API)

    A connected API instance

  • item (Ketchup::Item)

    The item all notes are attached to.

  • items (Array)

    An array of hashes that map to attributes of notes.



21
22
23
24
25
26
27
28
# File 'lib/ketchup/note_array.rb', line 21

def initialize(api, item, notes = [])
  @api  = api
  @item = item
  
  replace notes.collect { |hash|
    Ketchup::Note.new(@api, @item, hash)
  }
end

Instance Method Details

#build(params = {}) ⇒ Ketchup::Note

Create a new unsaved note 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:

item.notes.build 'content' => 'Targets are being met.'

Parameters:

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

    The note’s details

Returns:



40
41
42
43
44
# File 'lib/ketchup/note_array.rb', line 40

def build(params = {})
  note = Ketchup::Note.new @api, @item, params
  push note
  note
end

#create(params = {}) ⇒ Ketchup::Note

Create a new (saved) note 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:

item.notes.create 'content' => 'Targets are being met.'

Parameters:

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

    The note’s details

Returns:



56
57
58
59
60
# File 'lib/ketchup/note_array.rb', line 56

def create(params = {})
  note = build(params)
  note.save
  note
end

#reorder(*notes) ⇒ Object

Re-order the notes 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 notes that are in this array, and no others. This can’t be used as a shortcut to add new notes.

Examples:

item.notes.reorder second_note, fourth_note, first_note, third_note

Parameters:

  • notes (Ketchup::Note)

    The notes of the array, in a new order.

Raises:

  • ArgumentError if you don’t provide the exact same set of notes.



75
76
77
78
79
80
81
82
83
84
# File 'lib/ketchup/note_array.rb', line 75

def reorder(*notes)
  if notes.collect(&:shortcode_url).sort != collect(&:shortcode_url).sort
    raise ArgumentError, 'cannot sort a different set of notes'
  end
  
  @api.put "/items/#{@item.shortcode_url}/sort_notes.json", {
    'notes' => notes.collect(&:shortcode_url)
  }
  replace notes
end