Class: Ruber::OutputWidget::ActionList

Inherits:
Array show all
Defined in:
lib/ruber/output_widget.rb,
lib/ruber/output_widget.rb,
lib/ruber/output_widget.rb,
lib/ruber/output_widget.rb,
lib/ruber/output_widget.rb

Overview

Array of actions and separators (represented by nil) which allows to easily insert an entry before or after another one

Instance Method Summary (collapse)

Methods inherited from Array

#only?, #sample, #to_array, #to_h

Instance Method Details

- (self) insert_after(entry, *names)

Inserts one or more actions after a given one

Parameters:

  • entry (String, Integer)

    the entry after which the new action(s) should be inserted. If it’s a string, the actions will be inserted after the action with that name. If it’s an integer, the new actions will be inserted after the action at position entry. If the given entry doesn’t exist (or it’s a number larger than the size of the array), the new actions will be appended at the end

  • names (Array<String,nil>)

    the names of the actions to insert. nil entries represent separator

Returns:

  • (self)


1185
1186
1187
# File 'lib/ruber/output_widget.rb', line 1185

def insert_after entry, *names
  insert_after_or_before entry, :after, names
end

- (self) insert_after_or_before(entry, where, names) (private)

Helper method used by #insert_after and #insert_before

This is the method which performs the actual insertion of elements

Parameters:

  • where (Symbol)

    whether the new actions should be inserted before or after entry. It can be either :before or :after

  • entry (String, Integer)

    the entry after which the new action(s) should be inserted. If it’s a string, the actions will be inserted after the action with that name. If it’s an integer, the new actions will be inserted after the action at position entry. If the given entry doesn’t exist (or it’s a number larger than the size of the array), the new actions will be appended at the end

  • names (Array<String,nil>)

    the names of the actions to insert. nil entries represent separator

Returns:

  • (self)


1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/ruber/output_widget.rb', line 1202

def insert_after_or_before entry, where, names
  idx = if entry.is_a? Integer
  # The - 1 is needed because entry is the number of separators, but indexes start
  # at 0
    (self.each_with_index.find_all{|a, i| a.nil?}[entry - 1] || [])[1]
  else self.index(entry)
  end
  if idx and where == :after then idx += 1
  elsif !idx then idx = size
  end
  #Using reverse_each, we don't need to care increasing idx as we add items (the 
  #first item should be at position idx, the second at idx+1 and so on. With reverse_each)
  #this happens automatically. The +1 is needed to insert the items after idx
  names.reverse_each{|n| insert idx, n}
  self
end

- (self) insert_before(entry, *names)

Inserts one or more actions before a given one

Parameters:

  • entry (String, Integer)

    the entry before which the new action(s) should be inserted. If it’s a string, the actions will be inserted before the action with that name. If it’s an integer, the new actions will be inserted before the action at position entry. If the given entry doesn’t exist (or it’s a number larger than the size of the array), the new actions will be appended at the end

  • names (Array<String,nil>)

    the names of the actions to insert. nil entries represent separator

Returns:

  • (self)


1169
1170
1171
# File 'lib/ruber/output_widget.rb', line 1169

def insert_before entry, *names
  insert_after_or_before entry, :before, names
end