immosquare-extensions 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/immosquare-extensions/hash.rb +69 -3
- data/lib/immosquare-extensions/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e47304c7813f19b582e9efeda7f3d661574a60ee3c6b353ca2e2e9cadca44d93
|
4
|
+
data.tar.gz: e0ad30775947fb184fb589856d64fcbc7ddc2d2a66dbda3c5f7a1e8f62d53ec5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a32a0d02cb5d2bfc687f5af9ccda6391713ebcf7f0090c2b79992819e377dc1df539e95e5b9a4ff050757b2e018c9340a56faba2d93a0c7a7111919890c2361
|
7
|
+
data.tar.gz: 7e7cbb7a609cbff971fcb2fabf40674df2ecb6c2488256293f387ab54b5e5b007e34b968eb9e0612db6a4868737f4a9c272b64db7852fd76993974f8c7d35da0
|
@@ -41,13 +41,13 @@ class Hash
|
|
41
41
|
## http://dan.doezema.com/2012/04/recursively-sort-ruby-hash-by-key/
|
42
42
|
##
|
43
43
|
## Example:
|
44
|
-
## {b: 1, a: {d: 4, c: 3}}.sort_by_key
|
44
|
+
## {b: 1, a: {d: 4, c: 3}}.sort_by_key => {:a=>{:c=>3, :d=>4}, :b=>1}
|
45
45
|
##============================================================##
|
46
|
-
def sort_by_key(recursive
|
46
|
+
def sort_by_key(recursive: true, &block)
|
47
47
|
block ||= proc {|a, b| a.to_s.downcase.gsub("\"", "") <=> b.to_s.downcase.gsub("\"", "") }
|
48
48
|
keys.sort(&block).each_with_object({}) do |key, seed|
|
49
49
|
seed[key] = self[key]
|
50
|
-
seed[key] = seed[key].sort_by_key(
|
50
|
+
seed[key] = seed[key].sort_by_key(:recursive => recursive, &block) if recursive && seed[key].is_a?(Hash)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -73,8 +73,74 @@ class Hash
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
+
##============================================================##
|
77
|
+
## Returns a beautifully formatted JSON string representation
|
78
|
+
## of the hash.
|
79
|
+
##
|
80
|
+
## Options:
|
81
|
+
## - :align => true | Whether to align the values in the output.
|
82
|
+
## - :indent_size => 2 | The number of spaces to indent.
|
83
|
+
##
|
84
|
+
## Usage:
|
85
|
+
## hash.to_beautiful_json(:align => true, :indent_size => 2)
|
86
|
+
##============================================================##
|
87
|
+
def to_beautiful_json(**options)
|
88
|
+
options = {}.merge(options)
|
89
|
+
options[:align] = true if ![true, false].include?(options[:align])
|
90
|
+
options[:indent_size] = 2 if options[:indent_size].to_i == 0 || options[:indent_size].to_i > 10
|
91
|
+
|
92
|
+
dump_beautify_json(self, options[:align], options[:indent_size])
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
##============================================================##
|
99
|
+
## Helper method to convert value based on its type
|
100
|
+
##============================================================##
|
101
|
+
def json_representation(value, align, indent_size, indent)
|
102
|
+
case value
|
103
|
+
when Hash, Array then dump_beautify_json(value, align, indent_size, indent + indent_size)
|
104
|
+
when String then "\"#{value}\""
|
105
|
+
when NilClass then "null"
|
106
|
+
when TrueClass, FalseClass then value.to_s
|
107
|
+
else value
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
##============================================================##
|
112
|
+
## Helper method to recursively convert a hash or an array to
|
113
|
+
## a beautifully formatted JSON string.
|
114
|
+
##
|
115
|
+
## It takes into consideration the alignment of key-value pairs
|
116
|
+
## and the indentation for nested structures.
|
117
|
+
##
|
118
|
+
## Usage:
|
119
|
+
## dump_beautify_json(input_hash, align, indent_size, indent)
|
120
|
+
##============================================================##
|
121
|
+
def dump_beautify_json(hash, align, indent_size, indent = 0)
|
122
|
+
space = " "
|
76
123
|
|
124
|
+
if hash.is_a?(Hash)
|
125
|
+
return "{}" if hash.empty?
|
77
126
|
|
127
|
+
max_key_length = align ? hash.keys.map(&:to_s).map(&:length).max : 0
|
78
128
|
|
129
|
+
json_parts = hash.map do |key, value|
|
130
|
+
value_str = json_representation(value, align, indent_size, indent)
|
131
|
+
spacing = align ? space * (max_key_length - key.to_s.length + 1) : space
|
132
|
+
"#{space * (indent + indent_size)}\"#{key}\":#{spacing}#{value_str}"
|
133
|
+
end
|
134
|
+
|
135
|
+
"{\n#{json_parts.join(",\n")}\n#{space * indent}}"
|
136
|
+
elsif hash.is_a?(Array)
|
137
|
+
return "[]" if hash.empty?
|
138
|
+
|
139
|
+
array_parts = hash.map do |value|
|
140
|
+
"#{space * (indent + indent_size)}#{json_representation(value, align, indent_size, indent)}"
|
141
|
+
end
|
142
|
+
"[\n#{array_parts.join(",\n")}\n#{space * indent}]"
|
143
|
+
end
|
144
|
+
end
|
79
145
|
|
80
146
|
end
|