immosquare-extensions 0.1.8 → 0.1.10

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: aa20b8d8ac9120127993e62017fd26e15fc4f0b811ed56a133ade0a2194a5b2f
4
- data.tar.gz: b4afa253b87387d516168aee16f16dbcbc6921e9a3de38a2b9479f03f9477415
3
+ metadata.gz: 937a618cbe172bd91a19e35f4a66981a388886fdddd6453e5a76673feaea0497
4
+ data.tar.gz: eeb0e955c984536eec9dbcd7c4a3f3515ee8022250713f932d1874f0f8ef5bcd
5
5
  SHA512:
6
- metadata.gz: 8a3c480a122cea61574ebdf9e98bc0a280ade6abca3d38e4daaeef6cce1f11f09a1e933dd850f90ce610789a3621aab4e543f6a607da58bf28b59754f26d8c2a
7
- data.tar.gz: 651147c063cbb00249bb0c461689b4eb76c6b694a2ace01fb6e4994d1b9379e122ca46bad241afd043c2152db959c021f26ece4164adeddc7f2ee0981256e95b
6
+ metadata.gz: bf79dda7ea083a80aa83e5e04a56684b2fb99779ab7ed551de09a8491feaaff41d55e5f4faa9cb94837280e1622334a23ccafa508aab27d9b645a68b1b43f176
7
+ data.tar.gz: e2372301df5d13e3802bacca96d104651b20fd3fcb48ea330183c7bc81887aa85805a6b098e8dec31e2ebda32023496c4853e755a3b1a961e9066bd7e263fd5d
@@ -3,7 +3,7 @@
3
3
  ##============================================================##
4
4
  class Array
5
5
 
6
- include ImmosquareExtensions
6
+ include ImmosquareExtensions::SharedMethods
7
7
 
8
8
  ##============================================================##
9
9
  ## Calculate the average (mean) of an array of numbers.
@@ -5,7 +5,7 @@
5
5
  ##============================================================##
6
6
  class Hash
7
7
 
8
- include ImmosquareExtensions
8
+ include ImmosquareExtensions::SharedMethods
9
9
 
10
10
  ##============================================================##
11
11
  ## Remove multiple keys from a hash in a single command.
@@ -0,0 +1,69 @@
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
+ value_str = json_representation(value, align, indent_size, indent)
37
+ return "{\"#{key}\": #{value_str}}"
38
+ end
39
+
40
+ max_key_length = align ? hash.keys.map(&:to_s).map(&:length).max : 0
41
+
42
+ json_parts = hash.map do |key, value|
43
+ value_str = json_representation(value, align, indent_size, indent)
44
+ spacing =
45
+ if value.is_a?(Hash)
46
+ space
47
+ else
48
+ align ? space * (max_key_length - key.to_s.length + 1) : space
49
+ end
50
+ "#{space * (indent + indent_size)}\"#{key}\":#{spacing}#{value_str}"
51
+ end
52
+
53
+ "{\n#{json_parts.join(",\n")}\n#{space * indent}}"
54
+ elsif hash.is_a?(Array)
55
+ return "[]" if hash.empty?
56
+
57
+ if hash.length == 1 && !hash.first.is_a?(Hash)
58
+ value_str = json_representation(hash.first, align, indent_size, indent)
59
+ return "[#{value_str}]"
60
+ end
61
+
62
+ array_parts = hash.map do |value|
63
+ "#{space * (indent + indent_size)}#{json_representation(value, align, indent_size, indent)}"
64
+ end
65
+ "[\n#{array_parts.join(",\n")}\n#{space * indent}]"
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,3 +1,3 @@
1
1
  module ImmosquareExtensions
2
- VERSION = "0.1.8".freeze
2
+ VERSION = "0.1.10".freeze
3
3
  end
@@ -1,71 +1,7 @@
1
- require "immosquare-extensions/array"
2
- require "immosquare-extensions/hash"
3
- require "immosquare-extensions/string"
1
+ require_relative "immosquare-extensions/shared_methods"
2
+ require_relative "immosquare-extensions/array"
3
+ require_relative "immosquare-extensions/hash"
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.8
4
+ version: 0.1.10
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