immosquare-yaml 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: 233ee6e552c634b6fb640a401882a01deb01f114f63ad3abdd3822e00c1895a8
4
- data.tar.gz: 8e998672d8c72a2894e78d93eebf2dfdc8c6c4df892e0a1c4834399d4961d39f
3
+ metadata.gz: 8e9284e5c8a302af2d5c35d6a88ec0bbaea401be34629800f585347a9ad0de22
4
+ data.tar.gz: f9e2da6ee693fe3e001eaa6ec9d233bca746c805b2eaabacecc9152d4fe7e781
5
5
  SHA512:
6
- metadata.gz: 3d089b20cfa08b4f73c5bf0483a01aea667104a5745c4cc4e8fca96200c9453efa271b192b3f0b6701edd4c105e69b8620fa7a39cd7cd92e8eb590a98d81d101
7
- data.tar.gz: 07eaaf3c64d95808a28b8dc10f413ea18c18429878a980e5456b3e6779f6969b44d41770fbf413bdd51b9b131bc30a3c4c537f18737fcd9f9e89efb469021b44
6
+ metadata.gz: 523ee5214b2a8bef47ddad6f94c2035cc84eef36d2312a144b07757546f9487d7baeb2c88ecbd0e1558fc3571ec7c868f40518e214fd80b59320bf505c829293
7
+ data.tar.gz: 980155dbb44f8bba350c060b20d64f3cb0106f2d84d374b7aa6953ace6df467cdbb5db539830e9b93f1762b5d34c69ca28abecc7ba327dc3bb6c011056615439
@@ -28,18 +28,5 @@ module ImmosquareYaml
28
28
  end
29
29
  end
30
30
  end
31
-
32
- ##============================================================##
33
- ## sort_by_key Function
34
- ## Purpose: Sort a hash by its keys, optionally recursively, with
35
- ## case-insensitive comparison and stripping of double quotes.
36
- ## ============================================================ #
37
- def sort_by_key(hash, recursive = false, &block)
38
- block ||= proc {|a, b| a.to_s.downcase.gsub("\"", "") <=> b.to_s.downcase.gsub("\"", "") }
39
- hash.keys.sort(&block).each_with_object({}) do |key, seed|
40
- seed[key] = hash[key]
41
- seed[key] = sort_by_key(seed[key], true, &block) if recursive && seed[key].is_a?(Hash)
42
- end
43
- end
44
31
  end
45
32
  end
@@ -1,3 +1,3 @@
1
1
  module ImmosquareYaml
2
- VERSION = "0.1.11".freeze
2
+ VERSION = "0.1.13".freeze
3
3
  end
@@ -1,9 +1,16 @@
1
+ require "English"
2
+ require "immosquare-extensions"
1
3
  require_relative "immosquare-yaml/configuration"
2
4
  require_relative "immosquare-yaml/shared_methods"
3
5
  require_relative "immosquare-yaml/translate"
4
6
  require_relative "immosquare-yaml/railtie" if defined?(Rails)
5
7
 
6
-
8
+ ##===========================================================================##
9
+ ## Importing the 'English' library allows us to use more human-readable
10
+ ## global variables, such as $INPUT_RECORD_SEPARATOR instead of $/,
11
+ ## which enhances code clarity and makes it easier to understand
12
+ ## the purpose of these variables in our code.
13
+ ##===========================================================================##
7
14
  module ImmosquareYaml
8
15
  extend SharedMethods
9
16
 
@@ -48,6 +55,7 @@ module ImmosquareYaml
48
55
  }.merge(options)
49
56
 
50
57
  begin
58
+ output_file_path = nil
51
59
  raise("File not found") if !File.exist?(file_path)
52
60
 
53
61
  ##===========================================================================##
@@ -67,7 +75,7 @@ module ImmosquareYaml
67
75
  ##===========================================================================##
68
76
  clean_yml(file_path)
69
77
  parsed_yml = parse(file_path)
70
- parsed_yml = sort_by_key(parsed_yml, options[:sort])
78
+ parsed_yml = parsed_yml.sort_by_key(true)
71
79
  parsed_yml = dump(parsed_yml)
72
80
 
73
81
  ##===========================================================================##
@@ -82,7 +90,12 @@ module ImmosquareYaml
82
90
  File.write(output_file_path, parsed_yml)
83
91
  true
84
92
  rescue StandardError => e
93
+ ##===========================================================================##
94
+ ## Restore original content if necessary
95
+ ##===========================================================================##
96
+ File.write(file_path, original_content) if output_file_path != file_path && !original_content.nil?
85
97
  puts(e.message)
98
+ puts(e.backtrace)
86
99
  false
87
100
  end
88
101
  end
@@ -104,6 +117,7 @@ module ImmosquareYaml
104
117
  options = {:sort => true}.merge(options)
105
118
 
106
119
  begin
120
+ original_content = nil
107
121
  raise("File not found") if !File.exist?(file_path)
108
122
 
109
123
  ##===========================================================================##
@@ -112,22 +126,30 @@ module ImmosquareYaml
112
126
  original_content = File.read(file_path)
113
127
 
114
128
  ##===========================================================================##
115
- ## clean, parse & Sort
129
+ ## clean the file
116
130
  ##===========================================================================##
117
131
  clean_yml(file_path)
132
+
133
+ ##===========================================================================##
134
+ ## parse the file & sort if necessary
135
+ ##===========================================================================##
118
136
  parsed_xml = parse_xml(file_path)
119
- parsed_xml = sort_by_key(parsed_xml, options[:sort]) if options[:sort]
137
+ parsed_xml = parsed_xml.sort_by_key(true) if options[:sort]
120
138
 
121
139
  ##===========================================================================##
122
140
  ## Restore original content
123
141
  ##===========================================================================##
124
- File.write(file_path, original_content)
142
+ File.write(file_path, original_content) if !original_content.nil?
125
143
 
126
144
  ##===========================================================================##
127
145
  ## Return the parsed YAML file
128
146
  ##===========================================================================##
129
147
  parsed_xml
130
148
  rescue StandardError => e
149
+ ##===========================================================================##
150
+ ## Restore original content
151
+ ##===========================================================================##
152
+ File.write(file_path, original_content) if !original_content.nil?
131
153
  puts(e.message)
132
154
  false
133
155
  end
@@ -222,21 +244,23 @@ module ImmosquareYaml
222
244
  ## The total number of lines in the normalized file.
223
245
  ##===========================================================================##
224
246
  def normalize_last_line(file_path)
247
+ end_of_line = $INPUT_RECORD_SEPARATOR
225
248
  ##============================================================##
226
249
  ## Read all lines from the file
227
250
  ## https://gist.github.com/guilhermesimoes/d69e547884e556c3dc95
228
251
  ##============================================================##
229
252
  content = File.read(file_path)
230
253
 
254
+
231
255
  ##===========================================================================##
232
256
  ## Remove all trailing empty lines at the end of the file
233
- content.gsub!(/#{Regexp.escape($INPUT_RECORD_SEPARATOR)}+\z/, "")
234
257
  ##===========================================================================##
258
+ content.gsub!(/#{Regexp.escape(end_of_line)}+\z/, "")
235
259
 
236
260
  ##===========================================================================##
237
- ## Append a newline at the end to maintain the file structure
238
- ###===========================================================================##
239
- content += $INPUT_RECORD_SEPARATOR
261
+ ## Append an EOL at the end to maintain the file structure
262
+ ##===========================================================================##
263
+ content << end_of_line
240
264
 
241
265
  ##===========================================================================##
242
266
  ## Write the modified lines back to the file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-yaml
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-09-29 00:00:00.000000000 Z
11
+ date: 2023-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: iso-639
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: immosquare-extensions
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: IMMOSQUARE-YAML is a specialized Ruby gem tailored primarily for parsing
42
56
  and dumping YML translation files, addressing challenges faced with other parsers
43
57
  like interpreting translation keys as booleans, multi-line strings, and more.