immosquare-extensions 0.1.2 → 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: 8344d04cc08c20ab66793e39c6db6ec84cadd1cd042b2a41542ae127994bc855
4
- data.tar.gz: 2f45a3da92e6b929d649f2753df7298343a748470ee74bb0923bd84ca8b6e59f
3
+ metadata.gz: e47304c7813f19b582e9efeda7f3d661574a60ee3c6b353ca2e2e9cadca44d93
4
+ data.tar.gz: e0ad30775947fb184fb589856d64fcbc7ddc2d2a66dbda3c5f7a1e8f62d53ec5
5
5
  SHA512:
6
- metadata.gz: f3150e1f7f1eab4c0e31d3887e6017e596813fa563329dbb5fa876ab307420ae320542fee0c85a4f3b0f316ad557eba8ed69221253d1753db09c01704ecc4e92
7
- data.tar.gz: 298f9e60514c83125f1383c087b151b4d9aec2a8a1210c49f9368afee66d6a0c3e4c7755c085971a6634b76469b9411a419797bfa7667d2e04d49d9fed8ea646
6
+ metadata.gz: 9a32a0d02cb5d2bfc687f5af9ccda6391713ebcf7f0090c2b79992819e377dc1df539e95e5b9a4ff050757b2e018c9340a56faba2d93a0c7a7111919890c2361
7
+ data.tar.gz: 7e7cbb7a609cbff971fcb2fabf40674df2ecb6c2488256293f387ab54b5e5b007e34b968eb9e0612db6a4868737f4a9c272b64db7852fd76993974f8c7d35da0
@@ -41,13 +41,13 @@ class Hash
41
41
  ## http://dan.doezema.com/2012/04/recursively-sort-ruby-hash-by-key/
42
42
  ##
43
43
  ## Example:
44
- ## {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}
45
45
  ##============================================================##
46
- def sort_by_key(recursive = false, &block)
46
+ def sort_by_key(recursive: true, &block)
47
47
  block ||= proc {|a, b| a.to_s.downcase.gsub("\"", "") <=> b.to_s.downcase.gsub("\"", "") }
48
48
  keys.sort(&block).each_with_object({}) do |key, seed|
49
49
  seed[key] = self[key]
50
- 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)
51
51
  end
52
52
  end
53
53
 
@@ -73,8 +73,74 @@ class Hash
73
73
  end
74
74
  end
75
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 = " "
76
123
 
124
+ if hash.is_a?(Hash)
125
+ return "{}" if hash.empty?
77
126
 
127
+ max_key_length = align ? hash.keys.map(&:to_s).map(&:length).max : 0
78
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
79
145
 
80
146
  end
@@ -1,3 +1,3 @@
1
1
  module ImmosquareExtensions
2
- VERSION = "0.1.2".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.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - IMMO SQUARE