def insert_at(target, index = :last, scope = {})
level_nodes = target.children(scope)
current_index = level_nodes.index(self)
last_index = level_nodes.length - 1
as_first = (index == :first)
as_last = (index == :last || (index.is_a?(Fixnum) && index > last_index))
index = 0 if as_first
index = last_index if as_last
if last_index < 0
move_to_child_of(target)
elsif index >= 0 && index <= last_index && level_nodes[index]
if as_last && index != current_index
move_to_right_of(level_nodes[index])
elsif (as_first || index == 0) && index != current_index
move_to_left_of(level_nodes[index])
elsif !current_index.nil? && index > current_index
move_to_right_of(level_nodes[index])
elsif !current_index.nil? && index < current_index
move_to_left_of(level_nodes[index])
elsif current_index.nil?
move_to_left_of(level_nodes[index])
end
end
end