immosquare-extensions 0.1.7 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 94018ffd0ddd54cf8ee8914f26b0724424698f94e0bbcd008dbdba480580bdca
4
- data.tar.gz: a3f004b8721b0e722ecd9b20c3aac54586ac0da7a44f289e6791169c4c40b754
3
+ metadata.gz: 412295bead514cf0bafce1822bce7b868ffbd4a6c35f223f709bc52aff05d0f1
4
+ data.tar.gz: 3401f2707ede1e6957234f98fb7a2adaccbb0bfe489085eacd58ace9ac82d374
5
5
  SHA512:
6
- metadata.gz: b919e5f6e13c1759ad5f694fc0db3c07cd38c89b98db90482a70fe5f672c758e1d0b004435689967ff382234a1cdfa562f36906355e5df02b8d1787eb162e4cd
7
- data.tar.gz: 8270ffd69a4cf5823996d9c58e8290bf82244e46dec58cd8ba4884d3ac4b8b6533508220cd58bfcb064948c3ec4b0abf30574677ee1d908f6eca9258d8c615db
6
+ metadata.gz: c6e38a318a14fd702ed5df9dc2969e5b89781208ac159fdf760db07d2b42f5df15c6751dfd515305174ee10e9bd0d8e4c4d3906719be96cbb6285a7c91b148b4
7
+ data.tar.gz: 871f2fcae41dd41f3b118c25ca0177328d6a4e2e77b71df3fedd43a847fe6b7234a79a63400944f3fe08edc9a4747882b8bae262dcec47cc81d66a9f3298da23
@@ -1,8 +1,12 @@
1
+ require_relative "../immosquare-extensions"
2
+
1
3
  ##============================================================##
2
4
  ## This extension adds utility methods to the Array class.
3
5
  ##============================================================##
4
6
  class Array
5
7
 
8
+ include ImmosquareExtensions
9
+
6
10
  ##============================================================##
7
11
  ## Calculate the average (mean) of an array of numbers.
8
12
  ## The mean is the sum of the numbers divided by the count.
@@ -18,4 +22,23 @@ class Array
18
22
  sum(0.0) / size
19
23
  end
20
24
 
25
+ ##============================================================##
26
+ ## A json file can be a hash or an array. This method will
27
+ ## test.json
28
+ ## [
29
+ ## {"name": "Alice"},
30
+ ## {"name": "Bob"},
31
+ ## 123,
32
+ ## "string"
33
+ ## ]
34
+ ##============================================================##
35
+ def to_beautiful_json(**options)
36
+ options = {}.merge(options)
37
+ options[:align] = true if ![true, false].include?(options[:align])
38
+ options[:indent_size] = 2 if options[:indent_size].to_i == 0 || options[:indent_size].to_i > 10
39
+
40
+ dump_beautify_json(self, options[:align], options[:indent_size])
41
+ end
42
+
43
+
21
44
  end
@@ -1,3 +1,5 @@
1
+ require_relative "../immosquare-extensions"
2
+
1
3
  ##============================================================##
2
4
  ## This extension adds utility methods to the Hash class.
3
5
  ## It includes methods for handling and manipulating the keys
@@ -5,6 +7,8 @@
5
7
  ##============================================================##
6
8
  class Hash
7
9
 
10
+ include ImmosquareExtensions
11
+
8
12
  ##============================================================##
9
13
  ## Remove multiple keys from a hash in a single command.
10
14
  ##
@@ -93,71 +97,5 @@ class Hash
93
97
  end
94
98
 
95
99
 
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 && indent > 0
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 && !hash.first.is_a?(Hash)
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
100
 
163
101
  end
@@ -1,3 +1,3 @@
1
1
  module ImmosquareExtensions
2
- VERSION = "0.1.7".freeze
2
+ VERSION = "0.1.9".freeze
3
3
  end
@@ -1,6 +1,71 @@
1
- require "immosquare-extensions/array"
2
- require "immosquare-extensions/hash"
3
- require "immosquare-extensions/string"
1
+ require_relative "immosquare-extensions/array"
2
+ require_relative "immosquare-extensions/hash"
3
+ require_relative "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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - IMMO SQUARE