immosquare-extensions 0.1.1 → 0.1.4

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: 0e6c1b345c314a734220646b1b777b87eb9dc03c5e83b7523d1315f49ef51334
4
- data.tar.gz: e90b46d14c40a00fa121e336fc839b9f9d11c6a866f31337400ca14f5ef37b08
3
+ metadata.gz: e47304c7813f19b582e9efeda7f3d661574a60ee3c6b353ca2e2e9cadca44d93
4
+ data.tar.gz: e0ad30775947fb184fb589856d64fcbc7ddc2d2a66dbda3c5f7a1e8f62d53ec5
5
5
  SHA512:
6
- metadata.gz: 6690804267f30d222a482bbffd3c47dac59d8d5b21e32f5f375f2e1b4d41938ff8acb80900675e0cf963efca0e931cf2d8851424e2e9378ddfe4b84d159ac628
7
- data.tar.gz: bcd8843d0423781c3161035bf3c9f8ebcfa5b7946e69e728e3a5099f4049eb41655e785452d0c7877d9dda73c51c089e063de7c5ef00070d5d8170429ffc029a
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(true) => {:a=>{:c=>3, :d=>4}, :b=>1}
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 = false, &block)
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(true, &block) if recursive && seed[key].is_a?(Hash)
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
@@ -1,3 +1,3 @@
1
1
  module ImmosquareExtensions
2
- VERSION = "0.1.1".freeze
2
+ VERSION = "0.1.4".freeze
3
3
  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.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - IMMO SQUARE