Class: ActiveSupport::OrderedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/active_support/ordered_hash.rb

Instance Method Summary collapse

Instance Method Details

#camelize_keysObject

Covert snake_case keys to camelCase



89
90
91
92
93
94
95
96
97
# File 'lib/active_support/ordered_hash.rb', line 89

def camelize_keys
  self.keys.each_with_index do |key, i|
    if key != key.camelize(:lower)
      self.insert(i, key.camelize(:lower), self[key])
      self.delete(key)
    end
  end
  self
end

#insert(index, key, value) ⇒ Object

Insert a new key and value at the suppplied index.

Note that this is slightly different from Array#insert in that new entries must be added one at a time, i.e. insert(n, k, v, k, v…) is not supported.

Parameters:

  • index (Integer)
  • key (Object)
  • value (Object)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_support/ordered_hash.rb', line 16

def insert(index, key, value)
  tmp = ActiveSupport::OrderedHash.new
  index = self.length + 1 + index if index < 0
  if index < 0
    m = "Index #{index} is too small for current length (#{length})"
    raise IndexError, m
  end
  if index > 0
    i=0
    self.each do |k,v|
      tmp[k] = v
      self.delete(k)
      i+=1
      break if i == index
    end
  end
  tmp[key] = value
  tmp.merge!(self) # copy the remaining to tmp
  self.clear       # start over...
  self.merge!(tmp) # now put them all back
  self
end

#insert_after(hsh, &block) ⇒ Object

Insert a key and value after an existing key or the first entry for’ which the supplied block evaluates to true. The block takes precendence over the supplied key. Options:‘

* :existing_key (default: nil). If nil or not supplied then a block is required.
* :new_key (required)
* :value (required)

key exists, or the block never evaluates to true.

Raises:

  • KeyError if the supplied existing key is not found, the new



68
69
70
71
72
73
74
75
76
77
# File 'lib/active_support/ordered_hash.rb', line 68

def insert_after(hsh, &block)
  existing_key = hsh.fetch(:existing_key, nil)
  new_key = hsh[:new_key]
  value = hsh[:value]
  if block_given?
    self.insert_here(1, new_key, value, &block)
  else
    self.insert_here(1, new_key, value, existing_key)
  end
end

#insert_before(hsh, &block) ⇒ Object

Insert a key and value before an existing key or the first entry for’ which the supplied block evaluates to true. The block takes precendence over the supplied key. Options:‘

* :existing_key (default: nil). If nil or not supplied then a block is required.
* :new_key (required)
* :value (required)

key exists, or the block never evaluates to true.

Raises:

  • KeyError if the supplied existing key is not found, the new



48
49
50
51
52
53
54
55
56
57
# File 'lib/active_support/ordered_hash.rb', line 48

def insert_before(hsh, &block)
  existing_key = hsh.fetch(:existing_key, nil)
  new_key = hsh[:new_key]
  value = hsh[:value]
  if block_given?
    self.insert_here(0, new_key, value, &block)
  else
    self.insert_here(0, new_key, value, existing_key)
  end
end

#remove_emptiesObject

Delete any keys that are empty arrays



80
81
82
83
84
85
86
# File 'lib/active_support/ordered_hash.rb', line 80

def remove_empties
  self.keys.each do |key|
    if (self[key].kind_of?(Array) && self[key].empty?) || self[key].nil?
      self.delete(key)
    end
  end
end

#snakeize_keysObject

Covert camelCase keys to snake_case



100
101
102
103
104
105
106
107
108
# File 'lib/active_support/ordered_hash.rb', line 100

def snakeize_keys
  self.keys.each_with_index do |key, i|
    if key != key.underscore
      self.insert(i, key.underscore, self[key])
      self.delete(key)
    end
  end
  self
end

#unshift(k, v) ⇒ Object

Prepends an entry to the front of the object. Note that this is slightly different from Array#unshift in that new entries must be added one at a time, i.e. unshift(,[k,v],…) is not currently supported.



115
116
117
118
# File 'lib/active_support/ordered_hash.rb', line 115

def unshift k,v
  self.insert(0, k, v)
  self
end