annotate_yaml 0.0.4 → 0.0.5

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
  SHA1:
3
- metadata.gz: ef35923c5328d440a4f7a8d75dc677be72d69836
4
- data.tar.gz: f17add2eb2c7a0223cd597181780530dfeab413d
3
+ metadata.gz: 9b7f2f675a016a98a7caaa4b63723982dd09a9cc
4
+ data.tar.gz: e5be5202b3f4022d523548f85a7c41eafc7f429b
5
5
  SHA512:
6
- metadata.gz: f41e66bf9b19168a36fc8e9f749b53a512f8e77674c45da1a574e4af619eb526840e93fa58911fb5d573078f10da25399d947c46563c219b2d1c195d05a8afb1
7
- data.tar.gz: 445c18f3d593ea1471d5f62d5c84f9dc9d18f6c7eef9b8cbfc6254f16e6bfefaa2416474fd8dac5ddd38e03c4b6af0d356cae05f18e9b1f5b0b65901a10a98b6
6
+ metadata.gz: 536f04133bd9f6dc8fac888afef2323b0c3b3a967c981daf658e700ac3a766114e43ce9223e13fc41e654c9f8b51c3608cc04484f341863f028984337859ecd3
7
+ data.tar.gz: bbe4a147fbb99d768fa673e42be66490bef3b12001f9e22234eb0df54effea0d12d13b960eb05a7fb86ba80f3940c106b10d8c0714effd8fa24b11b815c76281
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # AnnotateYaml
1
+ # AnnotateYaml [![Code Climate](https://codeclimate.com/github/purban/annotate_yaml/badges/gpa.svg)](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
- class << self
5
- def annotate_locales
4
+ DEFAULT_YML_LOOKUP = 'config/locales/*.yml'
6
5
 
7
- files = Dir.glob("config/locales/*.yml")
8
- files.each do |file|
9
- complete_yaml_file = File.open(file).read
10
- # Remove the references
11
- complete_yaml_file_without_references = complete_yaml_file.gsub(/<<: \*.*/, '')
12
- yaml_hash = YAML::load(complete_yaml_file_without_references)
13
- array_of_hashes_of_values_with_keys = []
14
- yaml_navigation(yaml_hash, [], array_of_hashes_of_values_with_keys)
15
-
16
- string_result = ''
17
-
18
- text = File.open(file).read
19
- text.gsub!(/\r\n?/, "\n")
20
- current_hash = array_of_hashes_of_values_with_keys.shift
21
- text.each_line do |line|
22
- begin
23
- line_hash = YAML::load line
24
- rescue Exception => e
25
- string_result += line
26
- next
27
- end
28
- if line_hash === false
29
- string_result += line
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
- string_result += (line.end_with?(" \# #{value}\n") ? line : line.gsub("\n", '') + " \# #{value}\n")
35
- current_hash = array_of_hashes_of_values_with_keys.shift
63
+ result << annotation_for_line(line, value)
36
64
  else
37
- string_result += line
65
+ result << line
38
66
  end
39
67
  end
40
-
41
- File.open(file, "w+") do |f|
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 yaml_navigation(yaml_hash, keys_array_origin = [], result = [])
49
- yaml_hash.each do |key, value|
50
- keys_array_updated = keys_array_origin.dup
51
- keys_array_updated.push(key)
52
- result.push({ value => keys_array_updated.join('.') }) unless value.nil? || value.is_a?(Hash) || value.is_a?(Array)
53
- yaml_navigation value, keys_array_updated, result if value.is_a?(Hash)
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
@@ -1,3 +1,3 @@
1
1
  module AnnotateYaml
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  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
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-07-10 00:00:00.000000000 Z
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler