immosquare-yaml 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/immosquare-yaml/delete.rb +101 -0
- data/lib/immosquare-yaml/version.rb +1 -1
- data/lib/immosquare-yaml.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4ad1b77c2a0aa94cccc901f2095b51481bcb225a9930c863aab7ea5fa49ced69
|
|
4
|
+
data.tar.gz: c9cd9777e43963c3d2ff939b60fb1caf76dde884d7d28dee9357f1b9aad646c5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 79952197dbc28d89be6770eff07d3870332050acfd8e7c5897816a006d969d917f9500bc31540457ab43676324c7280a8dae9434033279c4c820bdccde714b87
|
|
7
|
+
data.tar.gz: db4d002fcd3086e86fde1b9d718f23223f6476d895a6973b49373798468ec5a1a39083e91f58ab33b410cfe9b39b7655b5dff48541145ece19d4e8edc9f73e18
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
module ImmosquareYaml
|
|
2
|
+
##============================================================##
|
|
3
|
+
## Delete — removes one or more dot-paths from an i18n YAML
|
|
4
|
+
## file and re-emits it through the standard dump pipeline
|
|
5
|
+
## (sort, quoting, literal blocks). Empty parent Hashes left
|
|
6
|
+
## behind by a deletion are pruned recursively so the YAML
|
|
7
|
+
## doesn't keep dangling empty maps.
|
|
8
|
+
##
|
|
9
|
+
## Path quoting follows the same convention as flatten_keys /
|
|
10
|
+
## parse_path : reserved (yes/no/true/...) and purely numeric
|
|
11
|
+
## segments must be wrapped in "..." inside the dot-path.
|
|
12
|
+
##============================================================##
|
|
13
|
+
class << self
|
|
14
|
+
|
|
15
|
+
##============================================================##
|
|
16
|
+
## delete_paths(file_path, paths, sort: true, output: file_path)
|
|
17
|
+
##
|
|
18
|
+
## paths : a single dot-path String or an Array<String>.
|
|
19
|
+
##
|
|
20
|
+
## Returns a Hash :
|
|
21
|
+
## { :deleted => [paths actually removed],
|
|
22
|
+
## :not_found => [paths that did not exist in the file] }
|
|
23
|
+
##
|
|
24
|
+
## Returns false if the file cannot be parsed or if its root
|
|
25
|
+
## node is not a Hash (i18n YAML files are always mappings).
|
|
26
|
+
##
|
|
27
|
+
## Note : the file is always rewritten through `dump`, even when
|
|
28
|
+
## every path is reported as :not_found. This means calling
|
|
29
|
+
## `delete_paths` doubles as a `clean` (sort + reformat) on the
|
|
30
|
+
## target file. Use `:output => "..."` to write elsewhere.
|
|
31
|
+
##============================================================##
|
|
32
|
+
def delete_paths(file_path, paths, **options)
|
|
33
|
+
options = {
|
|
34
|
+
:sort => true,
|
|
35
|
+
:output => file_path
|
|
36
|
+
}.merge(options)
|
|
37
|
+
|
|
38
|
+
begin
|
|
39
|
+
raise("File not found") if !File.exist?(file_path)
|
|
40
|
+
|
|
41
|
+
parsed = parse(file_path, :sort => options[:sort])
|
|
42
|
+
return false if parsed == false || !parsed.is_a?(Hash)
|
|
43
|
+
|
|
44
|
+
paths = Array(paths)
|
|
45
|
+
deleted = []
|
|
46
|
+
not_found = []
|
|
47
|
+
|
|
48
|
+
##============================================================##
|
|
49
|
+
## Walk each dot-path, delete the leaf if present, prune
|
|
50
|
+
## empty parents on the way back up.
|
|
51
|
+
##============================================================##
|
|
52
|
+
paths.each do |dot_path|
|
|
53
|
+
segments = parse_path(dot_path)
|
|
54
|
+
if !segments.empty? && delete_at_segments(parsed, segments)
|
|
55
|
+
deleted << dot_path
|
|
56
|
+
else
|
|
57
|
+
not_found << dot_path
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
output = dump(parsed)
|
|
62
|
+
FileUtils.mkdir_p(File.dirname(options[:output]))
|
|
63
|
+
File.write(options[:output], output)
|
|
64
|
+
|
|
65
|
+
{:deleted => deleted, :not_found => not_found}
|
|
66
|
+
rescue StandardError => e
|
|
67
|
+
puts(e.message)
|
|
68
|
+
puts(e.backtrace)
|
|
69
|
+
false
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
##============================================================##
|
|
78
|
+
## Recursive segment walker. Returns true if a leaf was
|
|
79
|
+
## actually removed. On the way back up, an intermediate Hash
|
|
80
|
+
## that has become empty is pruned from its parent so the
|
|
81
|
+
## final YAML doesn't keep dangling empty maps.
|
|
82
|
+
##============================================================##
|
|
83
|
+
def delete_at_segments(hash, segments)
|
|
84
|
+
return false if !hash.is_a?(Hash)
|
|
85
|
+
|
|
86
|
+
head, *rest = segments
|
|
87
|
+
return false if !hash.key?(head)
|
|
88
|
+
|
|
89
|
+
if rest.empty?
|
|
90
|
+
hash.delete(head)
|
|
91
|
+
return true
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
child = hash[head]
|
|
95
|
+
removed = delete_at_segments(child, rest)
|
|
96
|
+
hash.delete(head) if removed && child.is_a?(Hash) && child.empty?
|
|
97
|
+
removed
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
end
|
|
101
|
+
end
|
data/lib/immosquare-yaml.rb
CHANGED
|
@@ -3,6 +3,7 @@ require "fileutils"
|
|
|
3
3
|
require "immosquare-extensions"
|
|
4
4
|
require_relative "immosquare-yaml/shared_methods"
|
|
5
5
|
require_relative "immosquare-yaml/flatten"
|
|
6
|
+
require_relative "immosquare-yaml/delete"
|
|
6
7
|
|
|
7
8
|
##============================================================##
|
|
8
9
|
## ImmosquareYaml — post-processeur Psych dédié aux fichiers
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: immosquare-yaml
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- immosquare
|
|
@@ -39,6 +39,7 @@ extensions: []
|
|
|
39
39
|
extra_rdoc_files: []
|
|
40
40
|
files:
|
|
41
41
|
- lib/immosquare-yaml.rb
|
|
42
|
+
- lib/immosquare-yaml/delete.rb
|
|
42
43
|
- lib/immosquare-yaml/flatten.rb
|
|
43
44
|
- lib/immosquare-yaml/shared_methods.rb
|
|
44
45
|
- lib/immosquare-yaml/version.rb
|