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: 80, position: 0, padding: 0) ⇒ Object
- #stringify_keys ⇒ Object
- #stringify_keys! ⇒ Object
- #symbolize_keys ⇒ Object
- #symbolize_keys! ⇒ Object
- #transform_keys ⇒ Object
- #transform_keys! ⇒ Object
-
#vv_json ⇒ Object
See Float#vv_json.
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: 80, position: 0, padding: 0) ⇒ Object
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 |
# File 'lib/vv/hash_methods.rb', line 56 def cli_print 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
31 32 33 |
# File 'lib/vv/hash_methods.rb', line 31 def stringify_keys transform_keys{ |key| key.to_s } end |
#stringify_keys! ⇒ Object
35 36 37 |
# File 'lib/vv/hash_methods.rb', line 35 def stringify_keys! transform_keys!{ |key| key.to_s } end |
#symbolize_keys ⇒ Object
23 24 25 |
# File 'lib/vv/hash_methods.rb', line 23 def symbolize_keys transform_keys{ |key| key.to_sym } end |
#symbolize_keys! ⇒ Object
27 28 29 |
# File 'lib/vv/hash_methods.rb', line 27 def symbolize_keys! transform_keys!{ |key| key.to_sym } end |
#transform_keys ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/vv/hash_methods.rb', line 39 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
48 49 50 51 52 53 54 |
# File 'lib/vv/hash_methods.rb', line 48 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 ⇒ Object
See Float#vv_json. Intended for common sense json doc generation with reasonable length, depth, and size limits.
19 20 21 |
# File 'lib/vv/hash_methods.rb', line 19 def vv_json raise NotImplementedError end |