Module: VV::HashMethods

Included in:
Hash
Defined in:
lib/vv/hash_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
# File 'lib/vv/hash_methods.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
  base.alias_method :includes?, :include?
end

Instance Method Details

#cli_print(width: nil, position: nil, padding: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vv/hash_methods.rb', line 54

def cli_print width: nil,
              position: nil,
              padding: nil

  width    ||= 80
  position ||= 0
  padding  ||= 0

  key_padding = nil

  String.capture_stdout {
    key_padding = self.keys.map { | key |
      key.cli_print width: width,
                    position: position,
                    padding: padding
    }.max
  }

  key_padding += 1

  self.each do | key, value |
    position = key.cli_print width: width,
                             position: position,
                             padding: padding

    print ( key_padding - position ).spaces

    value_padding = position = key_padding

    position = value.cli_print width: width,
                               position: position,
                               padding: value_padding

    puts
    position = 0
  end

  position
end

#stringify_keysObject



29
30
31
# File 'lib/vv/hash_methods.rb', line 29

def stringify_keys
  transform_keys{ |key| key.to_s }
end

#stringify_keys!Object



33
34
35
# File 'lib/vv/hash_methods.rb', line 33

def stringify_keys!
  transform_keys!{ |key| key.to_s }
end

#symbolize_keysObject



21
22
23
# File 'lib/vv/hash_methods.rb', line 21

def symbolize_keys
  transform_keys{ |key| key.to_sym }
end

#symbolize_keys!Object



25
26
27
# File 'lib/vv/hash_methods.rb', line 25

def symbolize_keys!
  transform_keys!{ |key| key.to_sym }
end

#transform_keysObject



37
38
39
40
41
42
43
44
# File 'lib/vv/hash_methods.rb', line 37

def transform_keys
  return enum_for(:transform_keys) unless block_given?
  result = self.class.new
  self.each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end

#transform_keys!Object



46
47
48
49
50
51
52
# File 'lib/vv/hash_methods.rb', line 46

def transform_keys!
  return self.enum_for(:transform_keys!) unless block_given?
  self.keys.each do |key|
    self[yield(key)] = self.delete(key)
  end
  self
end

#vv_jsonObject



17
18
19
# File 'lib/vv/hash_methods.rb', line 17

def vv_json
  VV::JSON.generate self
end