module VV::HashMethods

Public Class Methods

included(base) click to toggle source
# File lib/vv/hash_methods.rb, line 4
def self.included(base)
  base.extend(ClassMethods)
  base.alias_method :includes?, :include?
end

Public Instance Methods

cli_print(width: nil, position: nil, padding: nil) click to toggle source
# 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_keys() click to toggle source
# File lib/vv/hash_methods.rb, line 29
def stringify_keys
  transform_keys{ |key| key.to_s }
end
stringify_keys!() click to toggle source
# File lib/vv/hash_methods.rb, line 33
def stringify_keys!
  transform_keys!{ |key| key.to_s }
end
symbolize_keys() click to toggle source
# File lib/vv/hash_methods.rb, line 21
def symbolize_keys
  transform_keys{ |key| key.to_sym }
end
symbolize_keys!() click to toggle source
# File lib/vv/hash_methods.rb, line 25
def symbolize_keys!
  transform_keys!{ |key| key.to_sym }
end
transform_keys() { |key| ... } click to toggle source
# 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!() { |key| ... } click to toggle source
# 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_json() click to toggle source
# File lib/vv/hash_methods.rb, line 17
def vv_json
  VV::JSON.generate self
end