immosquare-extensions 0.1.6 → 0.1.8
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/array.rb +21 -0
- data/lib/immosquare-extensions/hash.rb +2 -66
- data/lib/immosquare-extensions/version.rb +1 -1
- data/lib/immosquare-extensions.rb +65 -0
- 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: aa20b8d8ac9120127993e62017fd26e15fc4f0b811ed56a133ade0a2194a5b2f
|
4
|
+
data.tar.gz: b4afa253b87387d516168aee16f16dbcbc6921e9a3de38a2b9479f03f9477415
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a3c480a122cea61574ebdf9e98bc0a280ade6abca3d38e4daaeef6cce1f11f09a1e933dd850f90ce610789a3621aab4e543f6a607da58bf28b59754f26d8c2a
|
7
|
+
data.tar.gz: 651147c063cbb00249bb0c461689b4eb76c6b694a2ace01fb6e4994d1b9379e122ca46bad241afd043c2152db959c021f26ece4164adeddc7f2ee0981256e95b
|
@@ -3,6 +3,8 @@
|
|
3
3
|
##============================================================##
|
4
4
|
class Array
|
5
5
|
|
6
|
+
include ImmosquareExtensions
|
7
|
+
|
6
8
|
##============================================================##
|
7
9
|
## Calculate the average (mean) of an array of numbers.
|
8
10
|
## The mean is the sum of the numbers divided by the count.
|
@@ -18,4 +20,23 @@ class Array
|
|
18
20
|
sum(0.0) / size
|
19
21
|
end
|
20
22
|
|
23
|
+
##============================================================##
|
24
|
+
## A json file can be a hash or an array. This method will
|
25
|
+
## test.json
|
26
|
+
## [
|
27
|
+
## {"name": "Alice"},
|
28
|
+
## {"name": "Bob"},
|
29
|
+
## 123,
|
30
|
+
## "string"
|
31
|
+
## ]
|
32
|
+
##============================================================##
|
33
|
+
def to_beautiful_json(**options)
|
34
|
+
options = {}.merge(options)
|
35
|
+
options[:align] = true if ![true, false].include?(options[:align])
|
36
|
+
options[:indent_size] = 2 if options[:indent_size].to_i == 0 || options[:indent_size].to_i > 10
|
37
|
+
|
38
|
+
dump_beautify_json(self, options[:align], options[:indent_size])
|
39
|
+
end
|
40
|
+
|
41
|
+
|
21
42
|
end
|
@@ -5,6 +5,8 @@
|
|
5
5
|
##============================================================##
|
6
6
|
class Hash
|
7
7
|
|
8
|
+
include ImmosquareExtensions
|
9
|
+
|
8
10
|
##============================================================##
|
9
11
|
## Remove multiple keys from a hash in a single command.
|
10
12
|
##
|
@@ -93,71 +95,5 @@ class Hash
|
|
93
95
|
end
|
94
96
|
|
95
97
|
|
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 = " "
|
123
|
-
|
124
|
-
if hash.is_a?(Hash)
|
125
|
-
return "{}" if hash.empty?
|
126
|
-
|
127
|
-
if hash.keys.count == 1
|
128
|
-
key, value = hash.first
|
129
|
-
value_str = json_representation(value, align, indent_size, indent)
|
130
|
-
return "{\"#{key}\": #{value_str}}"
|
131
|
-
end
|
132
|
-
|
133
|
-
max_key_length = align ? hash.keys.map(&:to_s).map(&:length).max : 0
|
134
|
-
|
135
|
-
json_parts = hash.map do |key, value|
|
136
|
-
value_str = json_representation(value, align, indent_size, indent)
|
137
|
-
spacing =
|
138
|
-
if value.is_a?(Hash)
|
139
|
-
space
|
140
|
-
else
|
141
|
-
align ? space * (max_key_length - key.to_s.length + 1) : space
|
142
|
-
end
|
143
|
-
"#{space * (indent + indent_size)}\"#{key}\":#{spacing}#{value_str}"
|
144
|
-
end
|
145
|
-
|
146
|
-
"{\n#{json_parts.join(",\n")}\n#{space * indent}}"
|
147
|
-
elsif hash.is_a?(Array)
|
148
|
-
return "[]" if hash.empty?
|
149
|
-
|
150
|
-
if hash.length == 1
|
151
|
-
value_str = json_representation(hash.first, align, indent_size, indent)
|
152
|
-
return "[#{value_str}]"
|
153
|
-
end
|
154
|
-
|
155
|
-
array_parts = hash.map do |value|
|
156
|
-
"#{space * (indent + indent_size)}#{json_representation(value, align, indent_size, indent)}"
|
157
|
-
end
|
158
|
-
"[\n#{array_parts.join(",\n")}\n#{space * indent}]"
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
98
|
|
163
99
|
end
|
@@ -3,4 +3,69 @@ require "immosquare-extensions/hash"
|
|
3
3
|
require "immosquare-extensions/string"
|
4
4
|
|
5
5
|
module ImmosquareExtensions
|
6
|
+
private
|
7
|
+
|
8
|
+
##============================================================##
|
9
|
+
## Helper method to convert value based on its type
|
10
|
+
##============================================================##
|
11
|
+
def json_representation(value, align, indent_size, indent)
|
12
|
+
case value
|
13
|
+
when Hash, Array then dump_beautify_json(value, align, indent_size, indent + indent_size)
|
14
|
+
when String then "\"#{value}\""
|
15
|
+
when NilClass then "null"
|
16
|
+
when TrueClass, FalseClass then value.to_s
|
17
|
+
else value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
##============================================================##
|
22
|
+
## Helper method to recursively convert a hash or an array to
|
23
|
+
## a beautifully formatted JSON string.
|
24
|
+
##
|
25
|
+
## It takes into consideration the alignment of key-value pairs
|
26
|
+
## and the indentation for nested structures.
|
27
|
+
##
|
28
|
+
## Usage:
|
29
|
+
## dump_beautify_json(input_hash, align, indent_size, indent)
|
30
|
+
##============================================================##
|
31
|
+
def dump_beautify_json(hash, align, indent_size, indent = 0)
|
32
|
+
space = " "
|
33
|
+
|
34
|
+
if hash.is_a?(Hash)
|
35
|
+
return "{}" if hash.empty?
|
36
|
+
|
37
|
+
if hash.keys.count == 1 && indent > 0
|
38
|
+
key, value = hash.first
|
39
|
+
value_str = json_representation(value, align, indent_size, indent)
|
40
|
+
return "{\"#{key}\": #{value_str}}"
|
41
|
+
end
|
42
|
+
|
43
|
+
max_key_length = align ? hash.keys.map(&:to_s).map(&:length).max : 0
|
44
|
+
|
45
|
+
json_parts = hash.map do |key, value|
|
46
|
+
value_str = json_representation(value, align, indent_size, indent)
|
47
|
+
spacing =
|
48
|
+
if value.is_a?(Hash)
|
49
|
+
space
|
50
|
+
else
|
51
|
+
align ? space * (max_key_length - key.to_s.length + 1) : space
|
52
|
+
end
|
53
|
+
"#{space * (indent + indent_size)}\"#{key}\":#{spacing}#{value_str}"
|
54
|
+
end
|
55
|
+
|
56
|
+
"{\n#{json_parts.join(",\n")}\n#{space * indent}}"
|
57
|
+
elsif hash.is_a?(Array)
|
58
|
+
return "[]" if hash.empty?
|
59
|
+
|
60
|
+
if hash.length == 1 && !hash.first.is_a?(Hash)
|
61
|
+
value_str = json_representation(hash.first, align, indent_size, indent)
|
62
|
+
return "[#{value_str}]"
|
63
|
+
end
|
64
|
+
|
65
|
+
array_parts = hash.map do |value|
|
66
|
+
"#{space * (indent + indent_size)}#{json_representation(value, align, indent_size, indent)}"
|
67
|
+
end
|
68
|
+
"[\n#{array_parts.join(",\n")}\n#{space * indent}]"
|
69
|
+
end
|
70
|
+
end
|
6
71
|
end
|