Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/avodeploy/core_ext/hash_insert_at.rb

Overview

AVOCADO

The flexible and easy to use deployment framework for web applications

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License Version 2
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Instance Method Summary collapse

Instance Method Details

#insert_at(position, key, kvpair) ⇒ Object

Inserts a Key/Value-Pair at a specific position of in a Hash

Parameters:

  • position (Symbol)

    the position to add the new pair, either ‘:before` or `:after`

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/avodeploy/core_ext/hash_insert_at.rb', line 24

def insert_at(position, key, kvpair)
  raise ArgumentError, 'position must be either :before or :after' unless [:before, :after].include?(position)

  arr = to_a
  pos = arr.index(arr.assoc(key))

  if pos && position == :after
    pos += 1
  end

  if pos
    arr.insert(pos, kvpair)
  else
    arr << kvpair
  end

  replace Hash[arr]
end