Class: Insertion
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/yard/core_ext/insertion.rb |
Overview
The Insertion class inserts a value before or after another value in a list.
Class Method Summary
-
.new(list, value) ⇒ Insertion
constructor
Creates an insertion object on a list with a value to be inserted.
Instance Method Summary
-
#after(val, recursive = false)
Inserts the value after
val
. -
#after_any(val)
Alias for #after with
recursive
set to true. -
#before(val, recursive = false)
Inserts the value before
val
-
#before_any(val)
Alias for #before with
recursive
set to true. -
#insertion(val, rel, recursive = false, list = @list)
private
This method performs the actual insertion.
Constructor Details
.new(list, value) ⇒ Insertion
# File 'lib/yard/core_ext/insertion.rb', line 14
def initialize(list, value) @list = list @values = (Array === value ? value : [value]) end
Instance Method Details
#after(val, recursive = false)
Inserts the value after val
.
# File 'lib/yard/core_ext/insertion.rb', line 30
def after(val, recursive = false) insertion(val, 1, recursive) end
#after_any(val)
Alias for #after with recursive
set to true
# File 'lib/yard/core_ext/insertion.rb', line 38
def after_any(val) insertion(val, 1, true) end
#before(val, recursive = false)
Inserts the value before val
# File 'lib/yard/core_ext/insertion.rb', line 22
def before(val, recursive = false) insertion(val, 0, recursive) end
#before_any(val)
Alias for #before with recursive
set to true
# File 'lib/yard/core_ext/insertion.rb', line 34
def before_any(val) insertion(val, 0, true) end
#insertion(val, rel, recursive = false, list = @list) (private)
This method performs the actual insertion
# File 'lib/yard/core_ext/insertion.rb', line 49
def insertion(val, rel, recursive = false, list = @list) if recursive list.each do |item| next unless item.is_a?(Array) tmp = item.dup insertion(val, rel, recursive, item) return(list) unless item == tmp end end index = list.index(val) list[index + rel, 0] = @values if index list end