Class: Hash

Inherits:
Object show all
Defined in:
lib/ectoplasm.rb

Instance Method Summary collapse

Instance Method Details

#obfuscate!(secure_keys = DEFAULT_SECURE_KEYS) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/ectoplasm.rb', line 198

def obfuscate! secure_keys = DEFAULT_SECURE_KEYS
  self.each do |k, v|
    if v.is_a? Hash
      v.obfuscate!(secure_keys)
    elsif v.is_a? Array
      v.each { |_| v.obfuscate!(secure_keys) }
    elsif secure_keys.any? { |x| k.to_s.downcase.include? x.downcase }
      self[k] = '*****'
    end
  end
end

#pretty(indent: 0, width: 25) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ectoplasm.rb', line 157

def pretty indent: 0, width: 25
  s = ''

  self
    .select { |_, value| value != nil || value != '' }
    .map do |key, value|
      value = true if value == 'true'
      value = false if value == 'false'
      value = '********' if Regexp.new(DEFAULT_SECURE_KEYS.join '|') =~ key.to_s

      if value.is_a? Hash
        s += key.to_s.cyan.indent(indent) + "\n"
        s += value.pretty(width: width-indent-2).indent(indent+2)
        s += "\n"
        next
      end

      s += ' ' * indent

      if value.is_a? Array
        list = value.pretty(width: width)

        if list.split("\n").count > 1
          s += key.to_s.cyan + "\n"
          s += value.pretty(width: width).indent(indent)
          s += "\n"
          next
        end

        value = list
      end

      s += key.to_s.cyan
      s += ' ' + '.' * (width-key.to_s.length-indent) if width-key.to_s.length > indent
      s += ': '
      s += value.pretty + "\n"
    end

  s
end