Class: IIIF::OrderedHash
- Inherits:
-
Hash
- Object
- Hash
- IIIF::OrderedHash
- Defined in:
- lib/iiif/ordered_hash.rb
Instance Method Summary collapse
-
#camelize_keys ⇒ Object
Covert snake_case keys to camelCase.
-
#insert(index, key, value) ⇒ Object
Insert a new key and value at the suppplied index.
-
#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.
-
#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.
-
#remove_empties ⇒ Object
Delete any keys that are empty arrays.
-
#snakeize_keys ⇒ Object
Covert camelCase keys to snake_case.
-
#unshift(k, v) ⇒ Object
Prepends an entry to the front of the object.
Instance Method Details
#camelize_keys ⇒ Object
Covert snake_case keys to camelCase
88 89 90 91 92 93 94 95 96 |
# File 'lib/iiif/ordered_hash.rb', line 88 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.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/iiif/ordered_hash.rb', line 15 def insert(index, key, value) tmp = IIIF::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.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/iiif/ordered_hash.rb', line 67 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.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/iiif/ordered_hash.rb', line 47 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_empties ⇒ Object
Delete any keys that are empty arrays
79 80 81 82 83 84 85 |
# File 'lib/iiif/ordered_hash.rb', line 79 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_keys ⇒ Object
Covert camelCase keys to snake_case
99 100 101 102 103 104 105 106 107 |
# File 'lib/iiif/ordered_hash.rb', line 99 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.
114 115 116 117 |
# File 'lib/iiif/ordered_hash.rb', line 114 def unshift k,v self.insert(0, k, v) self end |