immosquare-extensions 0.1.1 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/immosquare-extensions/hash.rb +71 -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
|
@@ -35,17 +35,19 @@ class Hash
|
|
35
35
|
##============================================================##
|
36
36
|
## Sort a hash by its keys. If the recursive flag is true,
|
37
37
|
## it will sort nested hashes as well.
|
38
|
+
## case-insensitive comparison and stripping of double quotes.
|
38
39
|
##
|
39
40
|
## Reference:
|
40
41
|
## http://dan.doezema.com/2012/04/recursively-sort-ruby-hash-by-key/
|
41
42
|
##
|
42
43
|
## Example:
|
43
|
-
## {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}
|
44
45
|
##============================================================##
|
45
|
-
def sort_by_key(recursive
|
46
|
+
def sort_by_key(recursive: true, &block)
|
47
|
+
block ||= proc {|a, b| a.to_s.downcase.gsub("\"", "") <=> b.to_s.downcase.gsub("\"", "") }
|
46
48
|
keys.sort(&block).each_with_object({}) do |key, seed|
|
47
49
|
seed[key] = self[key]
|
48
|
-
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)
|
49
51
|
end
|
50
52
|
end
|
51
53
|
|
@@ -71,8 +73,74 @@ class Hash
|
|
71
73
|
end
|
72
74
|
end
|
73
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 = " "
|
74
123
|
|
124
|
+
if hash.is_a?(Hash)
|
125
|
+
return "{}" if hash.empty?
|
75
126
|
|
127
|
+
max_key_length = align ? hash.keys.map(&:to_s).map(&:length).max : 0
|
76
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
|
77
145
|
|
78
146
|
end
|