immosquare-extensions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a1d5cf291f00c33bf864785c2bf1b72db5ce0c682292da329aa3bef2cd909df2
4
+ data.tar.gz: 0d443a7bdb4304e42c7d065085a2a57c84ca57f6ef16078d6ae318953cd80a42
5
+ SHA512:
6
+ metadata.gz: 8866d06150921a47bd3b23d3b02f65acb3bc51793f55ff1f0097fe29c969a064ed0f61908b792cd1069366a02c6d3312ca5aecee18ace47321a2f357b964ba92
7
+ data.tar.gz: 5ee4d9ef0ecbb4374cd2dab28baa8e22bb9909e45c19499194d34698056f3133de6382c2e095273bc6b6c6c63b7960b185a260ec14de913b0ac6dc94cc4fa2cb
@@ -0,0 +1,21 @@
1
+ ##============================================================##
2
+ ## This extension adds utility methods to the Array class.
3
+ ##============================================================##
4
+ class Array
5
+
6
+ ##============================================================##
7
+ ## Calculate the average (mean) of an array of numbers.
8
+ ## The mean is the sum of the numbers divided by the count.
9
+ ##
10
+ ## Reference:
11
+ ## https://www.chrisjmendez.com/2018/10/14/mean-median-mode-standard-deviation/
12
+ ##
13
+ ## Examples:
14
+ ## [1, 2, 3, 4, 5].mean => 3.0
15
+ ## [2, 4, 6].mean => 4.0
16
+ ##============================================================##
17
+ def mean
18
+ sum(0.0) / size
19
+ end
20
+
21
+ end
@@ -0,0 +1,87 @@
1
+ ##============================================================##
2
+ ## This extension adds utility methods to the Hash class.
3
+ ## It includes methods for handling and manipulating the keys
4
+ ## and values of a hash in various ways.
5
+ ##============================================================##
6
+ class Hash
7
+
8
+ ##============================================================##
9
+ ## Remove multiple keys from a hash in a single command.
10
+ ##
11
+ ## Reference:
12
+ ## https://apidock.com/ruby/Hash/delete
13
+ ##
14
+ ## Examples:
15
+ ## {a: 1, b: 2, c: 3}.without(:a, :b) => {:c=>3}
16
+ ##============================================================##
17
+ def without(*keys)
18
+ cpy = dup
19
+ keys.each {|key| cpy.delete(key) }
20
+ cpy
21
+ end
22
+
23
+ ##============================================================##
24
+ ## Convert all keys of the hash to lowercase.
25
+ ## If a value is an array, it recursively processes the
26
+ ## nested hash elements.
27
+ ##
28
+ ## Example:
29
+ ## { "A" => { "B" => "value" } }.downcase_key => {"a"=>{"b"=>"value"}}
30
+ ##============================================================##
31
+ def downcase_key
32
+ keys.each do |k|
33
+ store(k.downcase, (v = delete(k)).is_a?(Array) ? v.map(&:downcase_key) : v)
34
+ end
35
+ self
36
+ end
37
+
38
+ ##============================================================##
39
+ ## Calculate the depth (or level) of nesting within a hash.
40
+ ##
41
+ ## Example:
42
+ ## {a: {b: {c: 1}}}.depth => 3
43
+ ##============================================================##
44
+ def depth
45
+ 1 + values.map {|v| v.is_a?(Hash) ? v.depth : 1 }.max
46
+ end
47
+
48
+ ##============================================================##
49
+ ## Sort a hash by its keys. If the recursive flag is true,
50
+ ## it will sort nested hashes as well.
51
+ ##
52
+ ## Reference:
53
+ ## http://dan.doezema.com/2012/04/recursively-sort-ruby-hash-by-key/
54
+ ##
55
+ ## Example:
56
+ ## {b: 1, a: {d: 4, c: 3}}.sort_by_key(true) => {:a=>{:c=>3, :d=>4}, :b=>1}
57
+ ##============================================================##
58
+ def sort_by_key(recursive = false, &block)
59
+ keys.sort(&block).each_with_object({}) do |key, seed|
60
+ seed[key] = self[key]
61
+ seed[key] = seed[key].sort_by_key(true, &block) if recursive && seed[key].is_a?(Hash)
62
+ end
63
+ end
64
+
65
+ ##============================================================##
66
+ ## Flatten a nested hash into a single-level hash. Nested keys
67
+ ## are represented with dot notation.
68
+ ##
69
+ ## Reference:
70
+ ## https://stackoverflow.com/questions/23521230/flattening-nested-hash-to-a-single-hash-with-ruby-rails
71
+ ##
72
+ ## Example:
73
+ ## {a: {b: {c: 1}}}.flatten_hash => {:a.b.c=>1}
74
+ ##============================================================##
75
+ def flatten_hash
76
+ each_with_object({}) do |(k, v), h|
77
+ if v.is_a?(Hash)
78
+ v.flatten_hash.map do |h_k, h_v|
79
+ h["#{k}.#{h_k}".to_sym] = h_v
80
+ end
81
+ else
82
+ h[k] = v
83
+ end
84
+ end
85
+ end
86
+
87
+ end
@@ -0,0 +1,53 @@
1
+ require "unicode_utils/upcase"
2
+
3
+ ##============================================================##
4
+ ## This extension adds utility methods to the String class.
5
+ ##============================================================##
6
+ class String
7
+
8
+ ##============================================================##
9
+ ## Convert the string 'true' to true and 'false' to false.
10
+ ## Any other value will return nil.
11
+ ##
12
+ ## Examples:
13
+ ## "true".to_boolean => true
14
+ ## "false".to_boolean => false
15
+ ## "random".to_boolean => nil
16
+ ##============================================================##
17
+ def to_boolean
18
+ case downcase
19
+ when "true"
20
+ true
21
+ when "false"
22
+ false
23
+ end
24
+ end
25
+
26
+ ##============================================================##
27
+ ## Provide a custom titleize method to handle strings
28
+ ## especially ones with hyphens more appropriately.
29
+ ## Standard titleize does not preserve hyphens in the desired manner.
30
+ ##
31
+ ## Reference:
32
+ ## https://stackoverflow.com/questions/29784873/titleize-a-hyphenated-name
33
+ ##
34
+ ## Examples:
35
+ ## "SANT-ANDREA-D'ORCINO".titleize_custom => Sant-Andrea-D'orcino
36
+ ## "SANT-ANDREA-D'ORCINO".titleize => Sant Andrea D'orcino
37
+ ##============================================================##
38
+ def titleize_custom
39
+ humanize.gsub(/\b('?[a-z])/) { ::Regexp.last_match(1).capitalize }
40
+ end
41
+
42
+ ##============================================================##
43
+ ## Overriding the standard upcase method to provide a more
44
+ ## comprehensive version that correctly handles Unicode characters.
45
+ ##
46
+ ## Example:
47
+ ## "José".upcase => "JOSÉ"
48
+ ##============================================================##
49
+ def upcase
50
+ UnicodeUtils.upcase(self)
51
+ end
52
+
53
+ end
@@ -0,0 +1,3 @@
1
+ module ImmosquareExtensions
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,7 @@
1
+ require "immosquare-extensions/array"
2
+ require "immosquare-extensions/hash"
3
+ require "immosquare-extensions/string"
4
+
5
+ module ImmosquareExentions
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: immosquare-extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - IMMO SQUARE
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: unicode_utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.0
27
+ description: The immosquare-extensions gem provides a set of utility extensions for
28
+ various Ruby core classes such as String, Hash, and Array. These extensions aim
29
+ to enhance the standard functionality, offering more convenience and efficiency
30
+ for Ruby developers.
31
+ email:
32
+ - jules@immosquare.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/immosquare-extensions.rb
38
+ - lib/immosquare-extensions/array.rb
39
+ - lib/immosquare-extensions/hash.rb
40
+ - lib/immosquare-extensions/string.rb
41
+ - lib/immosquare-extensions/version.rb
42
+ homepage: https://github.com/IMMOSQUARE/immosquare-extensions
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.7.2
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.4.13
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Utility extensions for Ruby core classes
65
+ test_files: []