Class: Hash

Inherits:
Object show all
Defined in:
lib/active_object/hash.rb

Instance Method Summary collapse

Instance Method Details

#assert_valid_keys(*valid_keys) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/active_object/hash.rb', line 4

def assert_valid_keys(*valid_keys)
  valid_keys.flatten!
  each_key do |k|
    unless valid_keys.include?(k)
      raise ArgumentError,
        "Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}"
    end
  end
end

#compactObject



16
17
18
# File 'lib/active_object/hash.rb', line 16

def compact
  select { |k, v| !v.nil? }
end

#compact!Object



22
23
24
# File 'lib/active_object/hash.rb', line 22

def compact!
  reject! { |k, v| v.nil? }
end

#deep_merge(other_hash, &block) ⇒ Object



28
29
30
# File 'lib/active_object/hash.rb', line 28

def deep_merge(other_hash, &block)
  dup.deep_merge!(other_hash, &block)
end

#deep_merge!(other_hash, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_object/hash.rb', line 34

def deep_merge!(other_hash, &block)
  other_hash.each_pair do |current_key, other_value|
    this_value = self[current_key]

    self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
      this_value.deep_merge(other_value, &block)
    else
      block_given? && key?(current_key) ? block.call(current_key, this_value, other_value) : other_value
    end
  end

  self
end

#except(*keys) ⇒ Object



49
50
51
# File 'lib/active_object/hash.rb', line 49

def except(*keys)
  dup.except!(*keys)
end

#except!(*keys) ⇒ Object



53
54
55
56
# File 'lib/active_object/hash.rb', line 53

def except!(*keys)
  keys.flatten.each { |k| delete(k) }
  self
end

#nillifyObject



58
59
60
# File 'lib/active_object/hash.rb', line 58

def nillify
  dup.nillify!
end

#nillify!Object



62
63
64
# File 'lib/active_object/hash.rb', line 62

def nillify!
  each { |k, v| self[k] = nil if !v.nil? && (v.try(:blank?) || v.try(:to_s).blank?) }
end

#only(*keys) ⇒ Object



66
67
68
# File 'lib/active_object/hash.rb', line 66

def only(*keys)
  dup.only!(*keys)
end

#only!(*keys) ⇒ Object



70
71
72
73
74
# File 'lib/active_object/hash.rb', line 70

def only!(*keys)
  hash = {}
  keys.flatten.each { |k| hash[k] = self[k] if self.has_key?(k) }
  replace(hash)
end

#rename_keys(*keys) ⇒ Object



76
77
78
# File 'lib/active_object/hash.rb', line 76

def rename_keys(*keys)
  dup.rename_keys!(*keys)
end

#rename_keys!(*keys) ⇒ Object



80
81
82
83
84
# File 'lib/active_object/hash.rb', line 80

def rename_keys!(*keys)
  keys = Hash[*keys.flatten]
  keys.each { |k, v| self[v] = delete(k) if self[k] }
  self
end

#reverse_merge(other_hash) ⇒ Object



87
88
89
# File 'lib/active_object/hash.rb', line 87

def reverse_merge(other_hash)
  other_hash.merge(self)
end

#reverse_merge!(other_hash) ⇒ Object



93
94
95
# File 'lib/active_object/hash.rb', line 93

def reverse_merge!(other_hash)
  replace(reverse_merge(other_hash))
end

#sampleObject



98
99
100
101
# File 'lib/active_object/hash.rb', line 98

def sample
  key = sample_key
  [key, fetch(key)]
end

#sample!Object



103
104
105
106
107
# File 'lib/active_object/hash.rb', line 103

def sample!
  key, value = sample
  delete(key)
  [key, value]
end

#sample_keyObject



109
110
111
112
# File 'lib/active_object/hash.rb', line 109

def sample_key
  hash_keys = keys
  hash_keys.at(Random.rand(hash_keys.length - 1))
end

#sample_key!Object



114
115
116
117
118
# File 'lib/active_object/hash.rb', line 114

def sample_key!
  key, value = sample
  delete(key)
  key
end

#sample_valueObject



120
121
122
# File 'lib/active_object/hash.rb', line 120

def sample_value
  fetch(sample_key)
end

#sample_value!Object



124
125
126
127
128
# File 'lib/active_object/hash.rb', line 124

def sample_value!
  key, value = sample
  delete(key)
  value
end

#shuffleObject



130
131
132
# File 'lib/active_object/hash.rb', line 130

def shuffle
  Hash[to_a.sample(length)]
end

#shuffle!Object



134
135
136
# File 'lib/active_object/hash.rb', line 134

def shuffle!
  replace(shuffle)
end

#slice(*keys) ⇒ Object



139
140
141
# File 'lib/active_object/hash.rb', line 139

def slice(*keys)
  keys.flatten.each_with_object(self.class.new) { |k, h| h[k] = self[k] if has_key?(k) }
end

#slice!(*keys) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/active_object/hash.rb', line 145

def slice!(*keys)
  omit = slice(*self.keys - keys)
  hash = slice(*keys)

  hash.default      = default
  hash.default_proc = default_proc if default_proc

  replace(hash)
  omit
end

#stringify_keysObject



158
159
160
# File 'lib/active_object/hash.rb', line 158

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object



164
165
166
167
168
169
# File 'lib/active_object/hash.rb', line 164

def stringify_keys!
  inject({}) do |options, (key, value)|
    options[key.to_s] = value
    options
  end
end

#stripObject



172
173
174
# File 'lib/active_object/hash.rb', line 172

def strip
  select { |k, v| !v.blank? }
end

#strip!Object



176
177
178
# File 'lib/active_object/hash.rb', line 176

def strip!
  reject! { |k, v| v.blank? }
end

#symbolize_and_underscore_keysObject



195
196
197
# File 'lib/active_object/hash.rb', line 195

def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end

#symbolize_and_underscore_keys!Object



199
200
201
202
203
204
# File 'lib/active_object/hash.rb', line 199

def symbolize_and_underscore_keys!
  inject({}) do |options, (key, value)|
    options[(key.to_s.gsub(' '.freeze, '_'.freeze).underscore.to_sym rescue key) || key] = value
    options
  end
end

#symbolize_keysObject



181
182
183
# File 'lib/active_object/hash.rb', line 181

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object



187
188
189
190
191
192
# File 'lib/active_object/hash.rb', line 187

def symbolize_keys!
  inject({}) do |options, (key, value)|
    options[(key.to_sym rescue key) || key] = value
    options
  end
end

#transform_keys(&block) ⇒ Object



207
208
209
# File 'lib/active_object/hash.rb', line 207

def transform_keys(&block)
  dup.transform_keys!(&block)
end

#transform_keys!(&block) ⇒ Object



213
214
215
216
217
218
# File 'lib/active_object/hash.rb', line 213

def transform_keys!(&block)
  return(enum_for(:transform_keys!)) unless block_given?

  keys.each { |k| self[yield(k)] = delete(k) }
  self
end

#transform_values(&block) ⇒ Object



222
223
224
# File 'lib/active_object/hash.rb', line 222

def transform_values(&block)
  dup.transform_values!(&block)
end

#transform_values!(&block) ⇒ Object



228
229
230
231
232
# File 'lib/active_object/hash.rb', line 228

def transform_values!(&block)
  return(enum_for(:transform_values!)) unless block_given?

  each { |k, v| self[k] = yield(v) }
end