yaml-validator 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yaml-validator (0.1.1)
4
+ yaml-validator (0.1.2)
5
5
  colorize
6
6
  rake
7
7
  rspec
data/lib/helpers.rb CHANGED
@@ -20,5 +20,13 @@ module Helpers
20
20
  array.each_with_index { |val, i| hash[i.to_s] = val }
21
21
  hash
22
22
  end
23
+
24
+ def self.pluralization?(object)
25
+ return false if object.nil?
26
+
27
+ keys = object.keys.map { |k| k.to_sym }
28
+
29
+ (keys.include? :one) and (keys.include? :other)
30
+ end
23
31
 
24
32
  end
@@ -0,0 +1,65 @@
1
+
2
+ KEYS_BY_LANGUAGE = {
3
+ :be=>[:one, :few, :many, :other],
4
+ :bs=>[:one, :few, :many, :other],
5
+ :by=>[:one, :few, :many, :other],
6
+ :cs=>[:one, :few, :other],
7
+ :hr=>[:one, :few, :many, :other],
8
+ :iu=>[:one, :two, :other],
9
+ :kw=>[:one, :two, :other],
10
+ :mo=>[:one, :few, :other],
11
+ :naq=>[:one, :two, :other],
12
+ :ro=>[:one, :few, :other],
13
+ :ru=>[:one, :few, :many, :other],
14
+ :se=>[:one, :two, :other],
15
+ :sh=>[:one, :few, :many, :other],
16
+ :sk=>[:one, :few, :other],
17
+ :sma=>[:one, :two, :other],
18
+ :smi=>[:one, :two, :other],
19
+ :smj=>[:one, :two, :other],
20
+ :smn=>[:one, :two, :other],
21
+ :sms=>[:one, :two, :other],
22
+ :sr=>[:one, :few, :many, :other],
23
+ :uk=>[:one, :few, :many, :other]
24
+ }
25
+
26
+ class PluralizationValidator
27
+ def self.validate(language, yaml_object)
28
+ validate_object(language, '', yaml_object)
29
+ end
30
+
31
+ def self.validate_object(language, full_key, yaml_object)
32
+ return [] if yaml_object.nil?
33
+
34
+ if Helpers.pluralization? yaml_object
35
+ self.validate_pluralization(language, full_key, yaml_object)
36
+ else
37
+ errors = []
38
+ yaml_object.each do |key, value|
39
+ if value.is_a? Hash
40
+ full_subkey = (full_key.empty?) ? key : "#{full_key}.#{key}"
41
+ errors.concat validate_object(language, full_subkey, value)
42
+ end
43
+ end
44
+ errors
45
+ end
46
+ end
47
+
48
+ def self.validate_pluralization(language, full_key, yaml_object)
49
+ language = language.to_sym
50
+ if KEYS_BY_LANGUAGE.has_key? language
51
+ errors = []
52
+ KEYS_BY_LANGUAGE[language].each do |key|
53
+ unless (yaml_object.has_key? key) or
54
+ (yaml_object.has_key? key.to_s)
55
+
56
+ errors << "missing '#{key.to_s}' pluralization for '#{full_key}'"
57
+ end
58
+ end
59
+ errors
60
+ else
61
+ []
62
+ end
63
+
64
+ end
65
+ end
@@ -1,3 +1,3 @@
1
1
  class YamlValidator
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require 'yaml'
2
2
  require 'yaml-validator/version'
3
3
  require_relative './helpers'
4
+ require_relative './pluralization-validator'
4
5
 
5
6
  class YamlValidator
6
7
 
@@ -54,6 +55,7 @@ class YamlValidator
54
55
  errors += validate_yaml_object('', yaml_object)
55
56
  if @options[:show_missing]
56
57
  errors.concat find_missing_translations(yaml_object)
58
+ errors.concat find_missing_pluralizations(filename, yaml_object)
57
59
  end
58
60
 
59
61
  errors.map { |err| "#{filename}: #{err}" }
@@ -73,11 +75,12 @@ class YamlValidator
73
75
  def validate_yaml_object(full_key, yaml_object)
74
76
  return [] if yaml_object.nil?
75
77
  errors = []
78
+ is_pluralization = Helpers.pluralization? yaml_object
76
79
 
77
80
  yaml_object.each do |key, value|
78
81
  full_subkey = (full_key.empty?) ? key : "#{full_key}.#{key}"
79
82
  if value.is_a? String
80
- errors.concat validate_item(full_subkey, value)
83
+ errors.concat validate_item(full_subkey, value, is_pluralization)
81
84
  else
82
85
  errors.concat validate_yaml_object(full_subkey, value)
83
86
  end
@@ -105,6 +108,11 @@ class YamlValidator
105
108
  end
106
109
  errors
107
110
  end
111
+
112
+ def find_missing_pluralizations(filename, yaml_object)
113
+ language = File.basename(filename, '.*')
114
+ PluralizationValidator.validate(language, yaml_object)
115
+ end
108
116
 
109
117
  def self.find_key_in_yaml_object(full_key, yaml_object)
110
118
  position = yaml_object
@@ -120,10 +128,14 @@ class YamlValidator
120
128
  end
121
129
  end
122
130
 
123
- def validate_item(full_key, value)
131
+ def validate_item(full_key, value, is_pluralization = false)
124
132
  real_vars = get_key_en_vars(full_key)
125
133
  if real_vars.nil?
126
- return ["#{full_key} doesn't exist in en.yml"]
134
+ if is_pluralization
135
+ return []
136
+ else
137
+ return ["#{full_key} doesn't exist in en.yml"]
138
+ end
127
139
  end
128
140
 
129
141
  used_vars = identify_variables(value)
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pp'
4
+
5
+ RAILS_I18N_PATH = File.expand_path("~/playground/rails-i18n")
6
+
7
+
8
+ NORMAL_RULES = ['one_other', 'other', 'one_upto_two_other', 'one_with_zero_other']
9
+
10
+ KEYS_BY_RULE = {
11
+ 'west_slavic' => [:one, :few, :other],
12
+ 'east_slavic' => [:one, :few, :many, :other],
13
+ 'romanian' => [:one, :few, :other],
14
+ 'one_two_other' => [:one, :two, :other]
15
+ }
16
+
17
+ PLURALS_PATH = File.join(RAILS_I18N_PATH, "rails/pluralization")
18
+
19
+ def find_rule(pluralization_file)
20
+ rule = `grep require #{pluralization_file}`
21
+ if rule.length > 0
22
+ /^.*pluralizations\/(.*)'$/.match(rule)[1]
23
+ else
24
+ nil
25
+ end
26
+ end
27
+
28
+ all = {}
29
+ Dir["#{PLURALS_PATH}/*.rb"].each do |file|
30
+ lang = File.basename(file, '.*')
31
+ rule = find_rule(file)
32
+ unless rule.nil? or NORMAL_RULES.include? rule
33
+ keys = KEYS_BY_RULE[rule]
34
+ all[lang.to_sym] = keys
35
+ end
36
+ end
37
+
38
+ pp all
@@ -0,0 +1,7 @@
1
+ en:
2
+ dogs:
3
+ one: '1 dog'
4
+ other: '%{count} dogs'
5
+ cats:
6
+ one: '1 cat'
7
+ other: '%{count} cats'
@@ -0,0 +1,9 @@
1
+ ru:
2
+ dogs:
3
+ one: '1 dog'
4
+ other: '%{count} dogs'
5
+ cats:
6
+ one: '1 cat'
7
+ few: '%{count} cats'
8
+ many: '%{count} cats'
9
+ other: '%{count} cats'
data/spec/helpers_spec.rb CHANGED
@@ -50,4 +50,16 @@ describe Helpers do
50
50
  end
51
51
  end
52
52
 
53
+ describe "#pluralization?" do
54
+ it "returns true when object has :one and :other" do
55
+ Helpers.pluralization?(:one => 'one', :other => 'other').should be_true
56
+ end
57
+ it "returns true when object has 'one' and 'other'" do
58
+ Helpers.pluralization?('one' => 'one', 'other' => 'other').should be_true
59
+ end
60
+ it "returns false for {}" do
61
+ Helpers.pluralization?({}).should be_false
62
+ end
63
+ end
64
+
53
65
  end
@@ -65,6 +65,17 @@ describe YamlValidator do
65
65
  end
66
66
  end
67
67
 
68
+ describe "weird pluralizations" do
69
+ it "returns missing pluralizations error" do
70
+ validator = YamlValidator.new('spec/fixtures/weird_pluralizations')
71
+ errors = validator.validate()
72
+ errors.should == [
73
+ "ru.yml: missing 'few' pluralization for 'dogs'",
74
+ "ru.yml: missing 'many' pluralization for 'dogs'",
75
+ ]
76
+ end
77
+ end
78
+
68
79
  end
69
80
 
70
81
  describe "#validate_yaml" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml-validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-24 00:00:00.000000000 Z
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -75,15 +75,19 @@ files:
75
75
  - Rakefile
76
76
  - bin/yaml-validator
77
77
  - lib/helpers.rb
78
+ - lib/pluralization-validator.rb
78
79
  - lib/yaml-validator.rb
79
80
  - lib/yaml-validator/version.rb
80
81
  - pkg/yaml-validator-0.0.1.gem
82
+ - scripts/get-rails-i18n-rules.rb
81
83
  - spec/fixtures/inconsistent_types/en.yml
82
84
  - spec/fixtures/inconsistent_types/he.yml
83
85
  - spec/fixtures/invalid_yml/en.yml
84
86
  - spec/fixtures/invalid_yml/invalid.yml
85
87
  - spec/fixtures/missing_translations/en.yml
86
88
  - spec/fixtures/missing_translations/he.yml
89
+ - spec/fixtures/weird_pluralizations/en.yml
90
+ - spec/fixtures/weird_pluralizations/ru.yml
87
91
  - spec/fixtures/wrong_root/en.yml
88
92
  - spec/fixtures/wrong_root/he.yml
89
93
  - spec/fixtures/wrong_variables/en.yml
@@ -105,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
109
  version: '0'
106
110
  segments:
107
111
  - 0
108
- hash: 417878389105800096
112
+ hash: -1836755832709183753
109
113
  required_rubygems_version: !ruby/object:Gem::Requirement
110
114
  none: false
111
115
  requirements:
@@ -114,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
118
  version: '0'
115
119
  segments:
116
120
  - 0
117
- hash: 417878389105800096
121
+ hash: -1836755832709183753
118
122
  requirements: []
119
123
  rubyforge_project:
120
124
  rubygems_version: 1.8.24
@@ -128,6 +132,8 @@ test_files:
128
132
  - spec/fixtures/invalid_yml/invalid.yml
129
133
  - spec/fixtures/missing_translations/en.yml
130
134
  - spec/fixtures/missing_translations/he.yml
135
+ - spec/fixtures/weird_pluralizations/en.yml
136
+ - spec/fixtures/weird_pluralizations/ru.yml
131
137
  - spec/fixtures/wrong_root/en.yml
132
138
  - spec/fixtures/wrong_root/he.yml
133
139
  - spec/fixtures/wrong_variables/en.yml