immosquare-extensions 0.1.9 → 0.1.11

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: 412295bead514cf0bafce1822bce7b868ffbd4a6c35f223f709bc52aff05d0f1
4
- data.tar.gz: 3401f2707ede1e6957234f98fb7a2adaccbb0bfe489085eacd58ace9ac82d374
3
+ metadata.gz: 68efb98fcb8dec7eaaa6edce1b9e2ca2546b860e98c1a91135e72d4fa1bb209b
4
+ data.tar.gz: 37700c0efc9a770fda32f0a2632ab21bca072abe9398b40cfaffa836013a00d8
5
5
  SHA512:
6
- metadata.gz: c6e38a318a14fd702ed5df9dc2969e5b89781208ac159fdf760db07d2b42f5df15c6751dfd515305174ee10e9bd0d8e4c4d3906719be96cbb6285a7c91b148b4
7
- data.tar.gz: 871f2fcae41dd41f3b118c25ca0177328d6a4e2e77b71df3fedd43a847fe6b7234a79a63400944f3fe08edc9a4747882b8bae262dcec47cc81d66a9f3298da23
6
+ metadata.gz: 2baa3798ab56b49857821603cf3eb84c58013de1e61b4ace02114e29ef02472ec09b504fac55ee3de4f19a15e94c6ffa7f059ff8ebcc764a05a50d05e750a375
7
+ data.tar.gz: 56e9077a3f2326b5a6faf15e25a663e60cd27521679cfde5a68963684f0854c2a23e485b2465586567f69e80c1a4bfddabfe465acf7f3f1318dac4aa645ce845
@@ -1,11 +1,9 @@
1
- require_relative "../immosquare-extensions"
2
-
3
1
  ##============================================================##
4
2
  ## This extension adds utility methods to the Array class.
5
3
  ##============================================================##
6
4
  class Array
7
5
 
8
- include ImmosquareExtensions
6
+ include ImmosquareExtensions::SharedMethods
9
7
 
10
8
  ##============================================================##
11
9
  ## Calculate the average (mean) of an array of numbers.
@@ -1,5 +1,3 @@
1
- require_relative "../immosquare-extensions"
2
-
3
1
  ##============================================================##
4
2
  ## This extension adds utility methods to the Hash class.
5
3
  ## It includes methods for handling and manipulating the keys
@@ -7,7 +5,7 @@ require_relative "../immosquare-extensions"
7
5
  ##============================================================##
8
6
  class Hash
9
7
 
10
- include ImmosquareExtensions
8
+ include ImmosquareExtensions::SharedMethods
11
9
 
12
10
  ##============================================================##
13
11
  ## Remove multiple keys from a hash in a single command.
@@ -0,0 +1,71 @@
1
+ module ImmosquareExtensions
2
+ module SharedMethods
3
+ private
4
+
5
+ ##============================================================##
6
+ ## Helper method to convert value based on its type
7
+ ##============================================================##
8
+ def json_representation(value, align, indent_size, indent)
9
+ case value
10
+ when Hash, Array then dump_beautify_json(value, align, indent_size, indent + indent_size)
11
+ when String then "\"#{value}\""
12
+ when NilClass then "null"
13
+ when TrueClass, FalseClass then value.to_s
14
+ else value
15
+ end
16
+ end
17
+
18
+ ##============================================================##
19
+ ## Helper method to recursively convert a hash or an array to
20
+ ## a beautifully formatted JSON string.
21
+ ##
22
+ ## It takes into consideration the alignment of key-value pairs
23
+ ## and the indentation for nested structures.
24
+ ##
25
+ ## Usage:
26
+ ## dump_beautify_json(input_hash, align, indent_size, indent)
27
+ ##============================================================##
28
+ def dump_beautify_json(hash, align, indent_size, indent = 0)
29
+ space = " "
30
+
31
+ if hash.is_a?(Hash)
32
+ return "{}" if hash.empty?
33
+
34
+ if hash.keys.count == 1 && indent > 0
35
+ key, value = hash.first
36
+ if !value.is_a?(Array)
37
+ value_str = json_representation(value, align, indent_size, indent)
38
+ return "{\"#{key}\": #{value_str}}"
39
+ end
40
+ end
41
+
42
+ max_key_length = align ? hash.keys.map(&:to_s).map(&:length).max : 0
43
+
44
+ json_parts = hash.map do |key, value|
45
+ value_str = json_representation(value, align, indent_size, indent)
46
+ spacing =
47
+ if value.is_a?(Hash)
48
+ space
49
+ else
50
+ align ? space * (max_key_length - key.to_s.length + 1) : space
51
+ end
52
+ "#{space * (indent + indent_size)}\"#{key}\":#{spacing}#{value_str}"
53
+ end
54
+
55
+ "{\n#{json_parts.join(",\n")}\n#{space * indent}}"
56
+ elsif hash.is_a?(Array)
57
+ return "[]" if hash.empty?
58
+
59
+ if hash.length == 1 && !hash.first.is_a?(Hash)
60
+ value_str = json_representation(hash.first, align, indent_size, indent)
61
+ return "[#{value_str}]"
62
+ end
63
+
64
+ array_parts = hash.map do |value|
65
+ "#{space * (indent + indent_size)}#{json_representation(value, align, indent_size, indent)}"
66
+ end
67
+ "[\n#{array_parts.join(",\n")}\n#{space * indent}]"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,3 +1,3 @@
1
1
  module ImmosquareExtensions
2
- VERSION = "0.1.9".freeze
2
+ VERSION = "0.1.11".freeze
3
3
  end
@@ -1,71 +1,7 @@
1
+ require_relative "immosquare-extensions/shared_methods"
1
2
  require_relative "immosquare-extensions/array"
2
3
  require_relative "immosquare-extensions/hash"
3
4
  require_relative "immosquare-extensions/string"
4
5
 
5
6
  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
71
7
  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.9
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - IMMO SQUARE
@@ -37,6 +37,7 @@ files:
37
37
  - lib/immosquare-extensions.rb
38
38
  - lib/immosquare-extensions/array.rb
39
39
  - lib/immosquare-extensions/hash.rb
40
+ - lib/immosquare-extensions/shared_methods.rb
40
41
  - lib/immosquare-extensions/string.rb
41
42
  - lib/immosquare-extensions/version.rb
42
43
  homepage: https://github.com/IMMOSQUARE/immosquare-extensions