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
- #cli_print(width: nil, position: nil, padding: nil) ⇒ Object
- #stringify_keys ⇒ Object
- #stringify_keys! ⇒ Object
- #symbolize_keys ⇒ Object
- #symbolize_keys! ⇒ Object
- #transform_keys ⇒ Object
- #transform_keys! ⇒ Object
- #vv_json ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/vv/hash_methods.rb', line 4 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#cli_print(width: nil, position: nil, padding: nil) ⇒ Object
53 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 |
# File 'lib/vv/hash_methods.rb', line 53 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 ⇒ Object
28 29 30 |
# File 'lib/vv/hash_methods.rb', line 28 def stringify_keys transform_keys{ |key| key.to_s } end |
#stringify_keys! ⇒ Object
32 33 34 |
# File 'lib/vv/hash_methods.rb', line 32 def stringify_keys! transform_keys!{ |key| key.to_s } end |
#symbolize_keys ⇒ Object
20 21 22 |
# File 'lib/vv/hash_methods.rb', line 20 def symbolize_keys transform_keys{ |key| key.to_sym } end |
#symbolize_keys! ⇒ Object
24 25 26 |
# File 'lib/vv/hash_methods.rb', line 24 def symbolize_keys! transform_keys!{ |key| key.to_sym } end |
#transform_keys ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/vv/hash_methods.rb', line 36 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
45 46 47 48 49 50 51 |
# File 'lib/vv/hash_methods.rb', line 45 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 |