immosquare-extensions 0.1.11 → 0.1.13

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: 68efb98fcb8dec7eaaa6edce1b9e2ca2546b860e98c1a91135e72d4fa1bb209b
4
- data.tar.gz: 37700c0efc9a770fda32f0a2632ab21bca072abe9398b40cfaffa836013a00d8
3
+ metadata.gz: 5b4cad039db2d2b1ee44eb735d1c6c710893b4aca057782689e060eb065275bb
4
+ data.tar.gz: 4a87efbeca74628edbc47da54e24a9287dd765e248e7900a269c93f5ccad9dcc
5
5
  SHA512:
6
- metadata.gz: 2baa3798ab56b49857821603cf3eb84c58013de1e61b4ace02114e29ef02472ec09b504fac55ee3de4f19a15e94c6ffa7f059ff8ebcc764a05a50d05e750a375
7
- data.tar.gz: 56e9077a3f2326b5a6faf15e25a663e60cd27521679cfde5a68963684f0854c2a23e485b2465586567f69e80c1a4bfddabfe465acf7f3f1318dac4aa645ce845
6
+ metadata.gz: 5c448e5db2ad0358f3e5bf5ea27cd184a5a8bfc159269a1dc7b93bc4b497f7555021c2c2a82071b925da25456839272f7b875060846f2f4c5e5e1707df983232
7
+ data.tar.gz: 5437907a34109b7edfb622eed9fa081ba878ef9c207261a7ed389b8b7276a8ea0bcedff2b065663b5f2c42cb976be0f229cefcafc3aae3666f3b1903e5e6e167
@@ -0,0 +1,50 @@
1
+ class File
2
+
3
+ ##===========================================================================##
4
+ ## This method ensures the file ends with a single newline, facilitating
5
+ ## cleaner multi-line blocks. It operates by reading all lines of the file,
6
+ ## removing any empty lines at the end, and then appending a newline.
7
+ ## This guarantees the presence of a newline at the end, and also prevents
8
+ ## multiple newlines from being present at the end.
9
+ ##
10
+ ## Params:
11
+ ## +file_path+:: The path to the file to be normalized.
12
+ ##
13
+ ## Returns:
14
+ ## The total number of lines in the normalized file.
15
+ ##===========================================================================##
16
+ def self.normalize_last_line(file_path)
17
+ end_of_line = $INPUT_RECORD_SEPARATOR || "\n"
18
+ ##============================================================##
19
+ ## Read all lines from the file
20
+ ## https://gist.github.com/guilhermesimoes/d69e547884e556c3dc95
21
+ ## Detect the encoding of the file using uchardet
22
+ ## Read the content of the file with the detected encoding,
23
+ ## falling back to UTF-8 if the detected encoding is empty or invalid.
24
+ ##============================================================##
25
+ detected_encoding = `uchardet #{file_path}`.strip
26
+ encoding_to_use = detected_encoding.empty? ? "UTF-8" : "#{detected_encoding}:UTF-8"
27
+ content = File.read(file_path, :encoding => encoding_to_use)
28
+
29
+ ##===========================================================================##
30
+ ## Remove all trailing empty lines at the end of the file
31
+ ##===========================================================================##
32
+ content.gsub!(/#{Regexp.escape(end_of_line)}+\z/, "")
33
+
34
+ ##===========================================================================##
35
+ ## Append an EOL at the end to maintain the file structure
36
+ ##===========================================================================##
37
+ content << end_of_line
38
+
39
+ ##===========================================================================##
40
+ ## Write the modified lines back to the file
41
+ ##===========================================================================##
42
+ File.write(file_path, content, :encoding => encoding_to_use)
43
+
44
+ ##===========================================================================##
45
+ ## Return the total number of lines in the modified file
46
+ ##===========================================================================##
47
+ content.lines.size
48
+ end
49
+
50
+ end
@@ -1,3 +1,3 @@
1
1
  module ImmosquareExtensions
2
- VERSION = "0.1.11".freeze
2
+ VERSION = "0.1.13".freeze
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require_relative "immosquare-extensions/shared_methods"
2
2
  require_relative "immosquare-extensions/array"
3
+ require_relative "immosquare-extensions/file"
3
4
  require_relative "immosquare-extensions/hash"
4
5
  require_relative "immosquare-extensions/string"
5
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - IMMO SQUARE
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-01 00:00:00.000000000 Z
11
+ date: 2024-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unicode_utils
@@ -36,6 +36,7 @@ extra_rdoc_files: []
36
36
  files:
37
37
  - lib/immosquare-extensions.rb
38
38
  - lib/immosquare-extensions/array.rb
39
+ - lib/immosquare-extensions/file.rb
39
40
  - lib/immosquare-extensions/hash.rb
40
41
  - lib/immosquare-extensions/shared_methods.rb
41
42
  - lib/immosquare-extensions/string.rb