Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/flumtter/app/core/hash.rb

Instance Method Summary collapse

Instance Method Details

#indent(t = 0, n = 2) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flumtter/app/core/hash.rb', line 18

def indent(t=0, n=2)
  self.map do |k,v|
    if self.nest_level == 1 || !v.is_a?(Hash)
      between = ": "
      char_len = self.keys.max_by{|key|key.size}.size + (n * t)
      k.to_s.shift(t*n).ljust(char_len) + ": " + v.to_s.nshift(char_len + between.size)
    else
      [k.to_s.shift(t*n), *v.is_a?(Hash) ? v.indent(t+1, n) : [v.to_s.shift(n*2)]].join("\n")
    end
  end.join("\n")
end

#nest_level(level = 0) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/flumtter/app/core/hash.rb', line 8

def nest_level(level=0)
  self.map do |k,v|
    if v.is_a?(Hash)
      v.nest_level(level+1)
    else
      level+1
    end
  end.max
end

#requires(*args) ⇒ Object



2
3
4
5
6
# File 'lib/flumtter/app/core/hash.rb', line 2

def requires(*args)
  args.each do |arg|
    raise ArgumentError, "Parameter is missing a value(#{arg})" if self[arg].nil?
  end
end