annotate_yaml 0.0.4 → 0.0.5
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/README.md +1 -1
- data/lib/annotate_yaml/annotate_yaml.rb +99 -39
- data/lib/annotate_yaml/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b7f2f675a016a98a7caaa4b63723982dd09a9cc
|
4
|
+
data.tar.gz: e5be5202b3f4022d523548f85a7c41eafc7f429b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 536f04133bd9f6dc8fac888afef2323b0c3b3a967c981daf658e700ac3a766114e43ce9223e13fc41e654c9f8b51c3608cc04484f341863f028984337859ecd3
|
7
|
+
data.tar.gz: bbe4a147fbb99d768fa673e42be66490bef3b12001f9e22234eb0df54effea0d12d13b960eb05a7fb86ba80f3940c106b10d8c0714effd8fa24b11b815c76281
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# AnnotateYaml
|
1
|
+
# AnnotateYaml [](https://codeclimate.com/github/purban/annotate_yaml)
|
2
2
|
|
3
3
|
Have you never been bored to scroll up an entire YAML structure to find out how to access the value?
|
4
4
|
|
@@ -1,58 +1,118 @@
|
|
1
1
|
module AnnotateYaml
|
2
2
|
require 'yaml'
|
3
3
|
|
4
|
-
|
5
|
-
def annotate_locales
|
4
|
+
DEFAULT_YML_LOOKUP = 'config/locales/*.yml'
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
6
|
+
class YamlHashExplorer
|
7
|
+
attr_reader :result
|
8
|
+
|
9
|
+
def initialize(hash)
|
10
|
+
@result = []
|
11
|
+
yaml_navigation(hash, [])
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def yaml_navigation(yaml_hash, keys_array_origin)
|
17
|
+
yaml_hash.each do |key, value|
|
18
|
+
keys_array_updated = keys_array_origin.dup
|
19
|
+
keys_array_updated.push(key)
|
20
|
+
result.push({ value => keys_array_updated.join('.') }) if desired_terminatory_value?(value)
|
21
|
+
|
22
|
+
yaml_navigation(value, keys_array_updated) if value.is_a?(Hash)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def desired_terminatory_value?(value)
|
27
|
+
!(value.nil? || value.is_a?(Hash) || value.is_a?(Array))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class FileHandler
|
32
|
+
|
33
|
+
attr_reader :file_name, :result
|
34
|
+
|
35
|
+
def initialize(file_name)
|
36
|
+
@file_name = file_name
|
37
|
+
@result = ""
|
38
|
+
end
|
39
|
+
|
40
|
+
def call
|
41
|
+
create_annotated_text
|
42
|
+
rewrite_file
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def create_annotated_text
|
48
|
+
adapted_file_content.each_line do |line|
|
49
|
+
annotate_line(line)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def annotate_line(line)
|
54
|
+
begin
|
55
|
+
if line_hash = YAML::load(line)
|
56
|
+
_, line_value = line_hash.first
|
57
|
+
|
58
|
+
if line_value.nil?
|
59
|
+
result << line
|
30
60
|
else
|
31
|
-
line_key, line_value = line_hash.first
|
32
61
|
key, value = current_hash.first
|
33
62
|
if line_value == key
|
34
|
-
|
35
|
-
current_hash = array_of_hashes_of_values_with_keys.shift
|
63
|
+
result << annotation_for_line(line, value)
|
36
64
|
else
|
37
|
-
|
65
|
+
result << line
|
38
66
|
end
|
39
67
|
end
|
40
|
-
|
41
|
-
|
42
|
-
f.write(string_result)
|
43
|
-
end
|
68
|
+
else
|
69
|
+
result << line
|
44
70
|
end
|
71
|
+
# YAML::load will generate an exception when references are used inside the yaml file. Exemple: errors: &errors
|
72
|
+
rescue Psych::BadAlias
|
73
|
+
result << line
|
45
74
|
end
|
46
75
|
end
|
47
76
|
|
48
|
-
def
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
77
|
+
def annotation_for_line(line, value)
|
78
|
+
(line.end_with?(" \# #{value}\n") ? line : line.gsub("\n", '') + " \# #{value}\n")
|
79
|
+
end
|
80
|
+
|
81
|
+
def rewrite_file
|
82
|
+
File.open(file_name, "w+") do |f|
|
83
|
+
f.write(result)
|
54
84
|
end
|
55
85
|
end
|
86
|
+
|
87
|
+
def adapted_file_content
|
88
|
+
File.open(file_name).read.gsub(/\r\n?/, "\n")
|
89
|
+
end
|
90
|
+
|
91
|
+
def hash_content
|
92
|
+
# Remove the references inside the yaml file otherwise the hash will be duplicated at each reference, and we don't want that.
|
93
|
+
# Exemple of reference: <<: *errors
|
94
|
+
YAML.load(File.open(file_name).read.gsub(/<<: \*.*/, ''))
|
95
|
+
end
|
96
|
+
|
97
|
+
def array_of_hashes_of_values_with_keys
|
98
|
+
@array_of_hashes_of_values_with_keys ||= YamlHashExplorer.new(hash_content).result
|
99
|
+
end
|
100
|
+
|
101
|
+
def current_hash
|
102
|
+
array_of_hashes_of_values_with_keys.shift
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class << self
|
107
|
+
def annotate_locales
|
108
|
+
file_names.each do |file_name|
|
109
|
+
FileHandler.new(file_name).call
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def file_names
|
114
|
+
Dir.glob(DEFAULT_YML_LOOKUP)
|
115
|
+
end
|
56
116
|
end
|
57
117
|
|
58
118
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: annotate_yaml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierre URBAN
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|