Class: Ketchup::NoteArray
- Inherits:
-
Array
- Object
- Array
- Ketchup::NoteArray
- 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
-
#build(params = {}) ⇒ Ketchup::Note
Create a new unsaved note object.
-
#create(params = {}) ⇒ Ketchup::Note
Create a new (saved) note object.
-
#initialize(api, item, notes = []) ⇒ NoteArray
constructor
Create a new NoteArray for a given api, item, and set of notes.
-
#reorder(*notes) ⇒ Object
Re-order the notes in this array, by passing them through in the order you would prefer.
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.
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.
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.
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.
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 |