yaml_strings 0.0.2 → 0.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.
- data/lib/tasks/yaml_strings.rake +17 -1
- data/lib/yaml_strings/strings_to_hash_encoder.rb +86 -0
- data/lib/yaml_strings/version.rb +1 -1
- data/lib/yaml_strings/yaml_to_strings_encoder.rb +27 -25
- data/lib/yaml_strings.rb +3 -3
- data/spec/lib/strings_to_hash_encoder_spec.rb +131 -0
- data/spec/lib/yaml_strings_spec.rb +1 -6
- data/spec/lib/yaml_to_strings_encoder_spec.rb +24 -20
- metadata +10 -10
- data/lib/yaml_strings/strings_to_yaml_encoder.rb +0 -82
- data/spec/lib/strings_to_yaml_encoder_spec.rb +0 -123
data/lib/tasks/yaml_strings.rake
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "yaml_strings"
|
2
2
|
namespace :yaml_strings do
|
3
|
-
desc "Converts a yaml file to a strings format"
|
3
|
+
desc "Converts a yaml file to a strings format and outputs to STDOUT"
|
4
4
|
task :yaml_to_strings do
|
5
5
|
fail "YAML_FILE must be set" unless file = ENV['YAML_FILE']
|
6
6
|
fail "YAML_FILE must be set to a file" unless File.exist?(file)
|
@@ -11,4 +11,20 @@ namespace :yaml_strings do
|
|
11
11
|
|
12
12
|
puts YamlToStringsEncoder.new(yaml_hash).to_s
|
13
13
|
end
|
14
|
+
|
15
|
+
desc "Converts a strings file to YAML format and outputs YAML_FILE"
|
16
|
+
task :strings_to_yaml do
|
17
|
+
fail "STRINGS_FILE must be set" unless file = ENV['STRINGS_FILE']
|
18
|
+
fail "STRINGS_FILE must be set to a file" unless File.exist?(file)
|
19
|
+
|
20
|
+
fail "YAML_FILE must be set" unless file = ENV['YAML_FILE']
|
21
|
+
|
22
|
+
strings_array = IO.readlines(ENV['STRINGS_FILE'])
|
23
|
+
|
24
|
+
fail "#{ENV['STRINGS_FILE']} is empty" if strings_array.empty?
|
25
|
+
|
26
|
+
File.open(ENV['YAML_FILE'], 'w') do |f|
|
27
|
+
f.write(StringsToHashEncoder.new(strings_array).to_hash.to_yaml)
|
28
|
+
end
|
29
|
+
end
|
14
30
|
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
class InvalidKeyError < StandardError; end
|
2
|
+
class InvalidValueError < StandardError; end
|
3
|
+
class InvalidTerminatorError < StandardError; end
|
4
|
+
|
5
|
+
class StringsToHashEncoder
|
6
|
+
attr_accessor :strings_array
|
7
|
+
def initialize(strings_array = [])
|
8
|
+
@strings_array = strings_array
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_hash
|
12
|
+
StringsToHashEncoder.parse_strings_array(@strings_array)
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
# Returns a hash when given
|
17
|
+
def parse_strings_array(array)
|
18
|
+
raise ArgumentError.new("Invalid strings format: missing '{\\n'") unless /{/.match(array.shift)
|
19
|
+
raise ArgumentError.new("Invalid strings format: missing '}'") unless /}/.match(array.pop)
|
20
|
+
|
21
|
+
array.inject({}) do |hash,line|
|
22
|
+
key_string, value = parse_strings_key_value(line)
|
23
|
+
|
24
|
+
key_string = remove_quotes(key_string)
|
25
|
+
value = remove_quotes(value)
|
26
|
+
keys = key_string.split('.') + [value]
|
27
|
+
|
28
|
+
recursive_merge(hash, nested_hash(keys))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Adapted from http://stackoverflow.com/a/8415328
|
33
|
+
def recursive_merge(a, b)
|
34
|
+
a.merge(b) {|key, a_item, b_item| recursive_merge(a_item, b_item) }
|
35
|
+
end
|
36
|
+
|
37
|
+
# %{ a b c d } =>
|
38
|
+
#
|
39
|
+
# { a => { b => { c => d } } }
|
40
|
+
#
|
41
|
+
def nested_hash(keys)
|
42
|
+
keys.reverse.inject { |hsh, elem| { elem => hsh } }
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns true if the value is a valid string NSString
|
46
|
+
#
|
47
|
+
# See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-BBCBDBJE
|
48
|
+
# for more info
|
49
|
+
def valid_string?(value)
|
50
|
+
(value =~ /\s*"[^"]*"\s*/ ||
|
51
|
+
value.strip =~ /\A\S+\Z/) &&
|
52
|
+
value.strip != ';' # handles case where no text in the value
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns true if the last character of the string, ignoring spaces, is a
|
56
|
+
# semi-colon.
|
57
|
+
def valid_terminator?(value)
|
58
|
+
return false if value.nil? || value == ""
|
59
|
+
|
60
|
+
value.gsub(' ', '')[-1] == ";"
|
61
|
+
end
|
62
|
+
|
63
|
+
def remove_quotes(key_string)
|
64
|
+
key_string = key_string.strip[1..-1] if key_string.strip[0] == '"'
|
65
|
+
key_string = key_string.strip[0..-2] if key_string.strip[-1] == '"'
|
66
|
+
key_string.strip!
|
67
|
+
key_string
|
68
|
+
end
|
69
|
+
|
70
|
+
# Validates the format
|
71
|
+
def parse_strings_key_value(line)
|
72
|
+
key_string, value = line.split('=')
|
73
|
+
|
74
|
+
key_string.strip!
|
75
|
+
value.strip!
|
76
|
+
|
77
|
+
raise InvalidValueError.new("Invalid strings format: Missing value for line after '=': '#{line}'") unless valid_string?(value)
|
78
|
+
raise InvalidKeyError.new("Invalid strings format: Missing key for line before '=': '#{line}'") unless valid_string?(key_string)
|
79
|
+
raise InvalidTerminatorError.new("Invalid strings format: Missing terminator ';' for line: '#{line}'") unless valid_terminator?(value)
|
80
|
+
|
81
|
+
value = value[0..-2] # Removes the ; from the value
|
82
|
+
|
83
|
+
return key_string, value
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/yaml_strings/version.rb
CHANGED
@@ -4,33 +4,35 @@ class YamlToStringsEncoder
|
|
4
4
|
@yaml_hash = yaml_hash
|
5
5
|
end
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
# prev_keys - a string representing the previous keys of enclosing hashes
|
11
|
-
#
|
12
|
-
# Ex: { a: :b } # => "a" = "b";
|
13
|
-
# Ex: { a: { b: :c } } # => "a.b" = "c";
|
14
|
-
def convert_hash_to_dict_property(hsh, prev_keys = nil)
|
15
|
-
result = []
|
16
|
-
hsh.each_pair {|k,v|
|
17
|
-
if v.is_a?(Hash)
|
18
|
-
new_prev_keys = "#{[prev_keys, k].compact.join('.')}"
|
19
|
-
result += convert_hash_to_dict_property(v, new_prev_keys)
|
20
|
-
else
|
21
|
-
result += ["\"#{[prev_keys, k].compact.join('.')}\" = \"#{v}\" ;"]
|
22
|
-
end
|
23
|
-
}
|
24
|
-
result
|
7
|
+
def to_s
|
8
|
+
array = YamlToStringsEncoder.convert_hash_to_dict_property(yaml_hash)
|
9
|
+
YamlToStringsEncoder.to_property_dict_string(array)
|
25
10
|
end
|
26
11
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
12
|
+
class << self
|
13
|
+
# Returns an array of dict properties as strings
|
14
|
+
#
|
15
|
+
# hsh - a hash to convert into the string representation
|
16
|
+
# prev_keys - a string representing the previous keys of enclosing hashes
|
17
|
+
#
|
18
|
+
# Ex: { a: :b } # => "a" = "b";
|
19
|
+
# Ex: { a: { b: :c } } # => "a.b" = "c";
|
20
|
+
def convert_hash_to_dict_property(hsh, prev_keys = nil)
|
21
|
+
result = []
|
22
|
+
hsh.each_pair {|k,v|
|
23
|
+
if v.is_a?(Hash)
|
24
|
+
new_prev_keys = "#{[prev_keys, k].compact.join('.')}"
|
25
|
+
result += convert_hash_to_dict_property(v, new_prev_keys)
|
26
|
+
else
|
27
|
+
result += ["\"#{[prev_keys, k].compact.join('.')}\" = \"#{v}\" ;"]
|
28
|
+
end
|
29
|
+
}
|
30
|
+
result
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
# Returns a string representing an old ASCII style property list
|
34
|
+
def to_property_dict_string(array_of_dict_propertes)
|
35
|
+
(["{"] + array_of_dict_propertes + ["}", nil]).join("\n")
|
36
|
+
end
|
35
37
|
end
|
36
38
|
end
|
data/lib/yaml_strings.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "yaml_strings/version"
|
2
2
|
require "yaml_strings/yaml_to_strings_encoder"
|
3
|
-
require "yaml_strings/
|
3
|
+
require "yaml_strings/strings_to_hash_encoder"
|
4
4
|
require "yaml_strings/load_rake_task"
|
5
5
|
|
6
6
|
module YamlStrings
|
@@ -10,9 +10,9 @@ module YamlStrings
|
|
10
10
|
YamlToStringsEncoder.new(yaml_hash).to_s
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
13
|
+
def strings_to_hash(string_file)
|
14
14
|
strings_array = File.readlines(string_file)
|
15
|
-
|
15
|
+
StringsToHashEncoder.new(strings_array).to_hash
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe StringsToHashEncoder do
|
4
|
+
it "should exist" do
|
5
|
+
lambda { subject.should be_a(StringsToHashEncoder) }.should_not raise_error(Exception)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#to_hash" do
|
9
|
+
it "should work" do
|
10
|
+
subject.strings_array = ["{\n", "en.app_name = awesome;", "}"]
|
11
|
+
subject.to_hash.should eql({ "en" => { "app_name" => "awesome" } })
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should work with a more complicated strings file" do
|
15
|
+
subject.strings_array = ["{\n", "en.app_name = awesome;", "en.helpers.create = \"Create Something\";", "}"]
|
16
|
+
subject.to_hash.should eql({ "en" => { "app_name" => "awesome", "helpers" => { "create" => "\"Create Something\"" } } })
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "class methods" do
|
21
|
+
subject { StringsToHashEncoder }
|
22
|
+
|
23
|
+
describe ".parse_strings_array" do
|
24
|
+
it "should raise an error if missing '{'" do
|
25
|
+
lambda { subject.parse_strings_array(["}"]) }.should raise_error(ArgumentError)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should raise an error if missing '}'" do
|
29
|
+
lambda { subject.parse_strings_array(["{\n"]) }.should raise_error(ArgumentError)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should work" do
|
33
|
+
subject.parse_strings_array(
|
34
|
+
["{\n", "en.app_name = awesome;", "}"]
|
35
|
+
).should eql({ "en" => { "app_name" => "awesome" } })
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe ".valid_string?" do
|
40
|
+
it "returns true for a quoted string" do
|
41
|
+
subject.valid_string?("\" \"").should be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns true for another quoted string" do
|
45
|
+
subject.valid_string?("\"Something good here\"").should be_true
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns true for non-whitespace string" do
|
49
|
+
subject.valid_string?("something_good_here").should be_true
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns true for non-whitespace string with a terminator" do
|
53
|
+
subject.valid_string?("something_good_here;").should be_true
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns false for non-quoted space" do
|
57
|
+
subject.valid_string?(" ").should be_false
|
58
|
+
end
|
59
|
+
|
60
|
+
it "returns false for non-quoted string" do
|
61
|
+
subject.valid_string?("something good here").should be_false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe ".valid_terminator?" do
|
66
|
+
it "returns true if last character is a ;" do
|
67
|
+
subject.valid_terminator?(";").should be_true
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns true if last character is a ; ignoring spaces" do
|
71
|
+
subject.valid_terminator?(" ; ").should be_true
|
72
|
+
end
|
73
|
+
|
74
|
+
it "returns false if last character is not a ; ignoring spaces" do
|
75
|
+
subject.valid_terminator?(" ").should be_false
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns false if last character is not a ;" do
|
79
|
+
subject.valid_terminator?(";a").should be_false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe ".parse_strings_key_value" do
|
84
|
+
it "should raise an error if there is no value for a line" do
|
85
|
+
lambda { subject.parse_strings_key_value("\"a.b\" = ;") }.should raise_error(InvalidValueError)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should raise an error if there is value for a line" do
|
89
|
+
lambda { subject.parse_strings_key_value("\"a.b\" = asdf asdf;") }.should raise_error(InvalidValueError)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should raise an error if there is no key for a line" do
|
93
|
+
lambda { subject.parse_strings_key_value(" = c;") }.should raise_error(InvalidKeyError)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should raise an error if there is no terminator" do
|
97
|
+
lambda { subject.parse_strings_key_value("a.b = c") }.should raise_error(InvalidTerminatorError)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should work fine" do
|
101
|
+
key_string, value = subject.parse_strings_key_value("a.b = c;")
|
102
|
+
key_string.should eql("a.b")
|
103
|
+
value.should eql("c")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe ".remove_quotes" do
|
108
|
+
it "should remove \" from key_string" do
|
109
|
+
subject.remove_quotes('"a.b"').should eql("a.b")
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should do nothing to the string" do
|
113
|
+
subject.remove_quotes('a.b').should eql("a.b")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe ".nested_hash" do
|
118
|
+
it "handles one element arrays" do
|
119
|
+
subject.nested_hash([:a]).should eql(:a)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "handles two element arrays" do
|
123
|
+
subject.nested_hash([:a, :b]).should eql({a: :b})
|
124
|
+
end
|
125
|
+
|
126
|
+
it "handles three element arrays" do
|
127
|
+
subject.nested_hash([:a, :b, :c]).should eql({a: { b: :c }})
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -1,41 +1,45 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe YamlToStringsEncoder do
|
4
|
-
describe "
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
describe "class methods" do
|
5
|
+
subject { YamlToStringsEncoder }
|
6
|
+
|
7
|
+
describe ".convert_hash_to_dict_property" do
|
8
|
+
it "converts a simple hash" do
|
9
|
+
result = subject.convert_hash_to_dict_property({ a: :b })
|
10
|
+
result.first.should eql('"a" = "b" ;')
|
11
|
+
end
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
+
it "converts a nested hash" do
|
14
|
+
result = subject.convert_hash_to_dict_property({ a: { b: :c } })
|
15
|
+
result.first.should eql('"a.b" = "c" ;')
|
16
|
+
end
|
13
17
|
end
|
14
|
-
end
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
describe ".to_property_dict_string" do
|
20
|
+
it "should handle an empty array" do
|
21
|
+
subject.to_property_dict_string([]).should eql(
|
19
22
|
<<STRINGS
|
20
23
|
{
|
21
24
|
}
|
22
25
|
STRINGS
|
23
|
-
)
|
24
|
-
|
26
|
+
)
|
27
|
+
end
|
25
28
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
+
it "should properly format a dict_property" do
|
30
|
+
dict_property_string = '"a.b" = "c"; '
|
31
|
+
subject.to_property_dict_string([dict_property_string]).should eql(
|
29
32
|
<<STRINGS
|
30
33
|
{
|
31
34
|
#{dict_property_string}
|
32
35
|
}
|
33
36
|
STRINGS
|
34
|
-
)
|
37
|
+
)
|
38
|
+
end
|
35
39
|
end
|
36
40
|
end
|
37
41
|
|
38
|
-
describe "to_s" do
|
42
|
+
describe "#to_s" do
|
39
43
|
it "should create a properly formatted 'strings' string" do
|
40
44
|
subject.yaml_hash = { a: { b: :c } }
|
41
45
|
subject.to_s.should eql(
|
@@ -44,7 +48,7 @@ STRINGS
|
|
44
48
|
"a.b" = "c" ;
|
45
49
|
}
|
46
50
|
STRINGS
|
47
|
-
)
|
51
|
+
)
|
48
52
|
end
|
49
53
|
end
|
50
54
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml_strings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-03-29 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: fakefs
|
16
|
-
requirement: &
|
16
|
+
requirement: &70219305851880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70219305851880
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70219305851140 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70219305851140
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70219305850180 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70219305850180
|
47
47
|
description: Convert YAML files to .string format and vice versa.
|
48
48
|
email:
|
49
49
|
- jonathan.wallace@gmail.com
|
@@ -59,10 +59,10 @@ files:
|
|
59
59
|
- lib/tasks/yaml_strings.rake
|
60
60
|
- lib/yaml_strings.rb
|
61
61
|
- lib/yaml_strings/load_rake_task.rb
|
62
|
-
- lib/yaml_strings/
|
62
|
+
- lib/yaml_strings/strings_to_hash_encoder.rb
|
63
63
|
- lib/yaml_strings/version.rb
|
64
64
|
- lib/yaml_strings/yaml_to_strings_encoder.rb
|
65
|
-
- spec/lib/
|
65
|
+
- spec/lib/strings_to_hash_encoder_spec.rb
|
66
66
|
- spec/lib/yaml_strings_spec.rb
|
67
67
|
- spec/lib/yaml_to_strings_encoder_spec.rb
|
68
68
|
- spec/spec_helper.rb
|
@@ -93,7 +93,7 @@ specification_version: 3
|
|
93
93
|
summary: Converts a YAML file, specifically the ones typically used for locale and
|
94
94
|
translations in Rails, to an old style Apple ASCII Property List, http://bit.ly/old_style_ascii_property_list
|
95
95
|
test_files:
|
96
|
-
- spec/lib/
|
96
|
+
- spec/lib/strings_to_hash_encoder_spec.rb
|
97
97
|
- spec/lib/yaml_strings_spec.rb
|
98
98
|
- spec/lib/yaml_to_strings_encoder_spec.rb
|
99
99
|
- spec/spec_helper.rb
|
@@ -1,82 +0,0 @@
|
|
1
|
-
class InvalidKeyError < StandardError; end
|
2
|
-
class InvalidValueError < StandardError; end
|
3
|
-
class InvalidTerminatorError < StandardError; end
|
4
|
-
|
5
|
-
class StringsToYamlEncoder
|
6
|
-
attr_accessor :strings_array
|
7
|
-
def initialize(strings_array = [])
|
8
|
-
@strings_array = strings_array
|
9
|
-
end
|
10
|
-
|
11
|
-
def to_s
|
12
|
-
parse_strings_array(@strings_array)
|
13
|
-
end
|
14
|
-
|
15
|
-
# Returns a hash when given
|
16
|
-
def parse_strings_array(array)
|
17
|
-
raise ArgumentError.new("Invalid strings format: missing '{\\n'") unless /{/.match(array.shift)
|
18
|
-
raise ArgumentError.new("Invalid strings format: missing '}'") unless /}/.match(array.pop)
|
19
|
-
|
20
|
-
array.inject({}) do |hash,line|
|
21
|
-
key_string, value = parse_strings_key_value(line)
|
22
|
-
|
23
|
-
key_string = remove_quotes_from_key_string(key_string)
|
24
|
-
keys = key_string.split('.')
|
25
|
-
|
26
|
-
hash.merge!(nested_hash(keys + [value]))
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# %{ a b c d } =>
|
31
|
-
#
|
32
|
-
# { a => { b => { c => d } } }
|
33
|
-
#
|
34
|
-
def nested_hash(keys)
|
35
|
-
first = keys.shift
|
36
|
-
return first if keys.empty? # base case
|
37
|
-
|
38
|
-
hash = Hash.new
|
39
|
-
hash[first] = nested_hash(keys)
|
40
|
-
hash
|
41
|
-
end
|
42
|
-
|
43
|
-
# Returns true if the value is a valid string NSString
|
44
|
-
#
|
45
|
-
# See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-BBCBDBJE
|
46
|
-
# for more info
|
47
|
-
def valid_string?(value)
|
48
|
-
(value =~ /\s*"[^"]*"\s*/ ||
|
49
|
-
value.strip =~ /\A\S+\Z/) &&
|
50
|
-
value.strip != ';' # handles case where no text in the value
|
51
|
-
end
|
52
|
-
|
53
|
-
# Returns true if the last character of the string, ignoring spaces, is a
|
54
|
-
# semi-colon.
|
55
|
-
def valid_terminator?(value)
|
56
|
-
return false if value.nil? || value == ""
|
57
|
-
|
58
|
-
value.gsub(' ', '')[-1] == ";"
|
59
|
-
end
|
60
|
-
|
61
|
-
def remove_quotes_from_key_string(key_string)
|
62
|
-
key_string = key_string.strip[1..-1] if key_string.strip[0] == '"'
|
63
|
-
key_string = key_string.strip[0..-2] if key_string.strip[-1] == '"'
|
64
|
-
key_string
|
65
|
-
end
|
66
|
-
|
67
|
-
# Validates the format
|
68
|
-
def parse_strings_key_value(line)
|
69
|
-
key_string, value = line.split('=')
|
70
|
-
|
71
|
-
key_string.strip!
|
72
|
-
value.strip!
|
73
|
-
|
74
|
-
raise InvalidValueError.new("Invalid strings format: Missing value for line after '=': '#{line}'") unless valid_string?(value)
|
75
|
-
raise InvalidKeyError.new("Invalid strings format: Missing key for line before '=': '#{line}'") unless valid_string?(key_string)
|
76
|
-
raise InvalidTerminatorError.new("Invalid strings format: Missing terminator ';' for line: '#{line}'") unless valid_terminator?(value)
|
77
|
-
|
78
|
-
value = value[0..-2] # Removes the ; from the value
|
79
|
-
|
80
|
-
return key_string, value
|
81
|
-
end
|
82
|
-
end
|
@@ -1,123 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe StringsToYamlEncoder do
|
4
|
-
it "should exist" do
|
5
|
-
lambda { subject.should be_a(StringsToYamlEncoder) }.should_not raise_error(Exception)
|
6
|
-
end
|
7
|
-
|
8
|
-
describe "#to_s" do
|
9
|
-
it "should work" do
|
10
|
-
subject.parse_strings_array(
|
11
|
-
["{\n", "en.app_name = awesome;", "}"]
|
12
|
-
).should eql({ "en" => { "app_name" => "awesome" } })
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "parse_strings_array" do
|
17
|
-
it "should raise an error if missing '{'" do
|
18
|
-
lambda { subject.parse_strings_array(["}"]) }.should raise_error(ArgumentError)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should raise an error if missing '}'" do
|
22
|
-
lambda { subject.parse_strings_array(["{\n"]) }.should raise_error(ArgumentError)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should work" do
|
26
|
-
subject.parse_strings_array(
|
27
|
-
["{\n", "en.app_name = awesome;", "}"]
|
28
|
-
).should eql({ "en" => { "app_name" => "awesome" } })
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "valid_string?" do
|
33
|
-
it "returns true for a quoted string" do
|
34
|
-
subject.valid_string?("\" \"").should be_true
|
35
|
-
end
|
36
|
-
|
37
|
-
it "returns true for another quoted string" do
|
38
|
-
subject.valid_string?("\"Something good here\"").should be_true
|
39
|
-
end
|
40
|
-
|
41
|
-
it "returns true for non-whitespace string" do
|
42
|
-
subject.valid_string?("something_good_here").should be_true
|
43
|
-
end
|
44
|
-
|
45
|
-
it "returns true for non-whitespace string with a terminator" do
|
46
|
-
subject.valid_string?("something_good_here;").should be_true
|
47
|
-
end
|
48
|
-
|
49
|
-
it "returns false for non-quoted space" do
|
50
|
-
subject.valid_string?(" ").should be_false
|
51
|
-
end
|
52
|
-
|
53
|
-
it "returns false for non-quoted string" do
|
54
|
-
subject.valid_string?("something good here").should be_false
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
describe "valid_terminator?" do
|
59
|
-
it "returns true if last character is a ;" do
|
60
|
-
subject.valid_terminator?(";").should be_true
|
61
|
-
end
|
62
|
-
|
63
|
-
it "returns true if last character is a ; ignoring spaces" do
|
64
|
-
subject.valid_terminator?(" ; ").should be_true
|
65
|
-
end
|
66
|
-
|
67
|
-
it "returns false if last character is not a ; ignoring spaces" do
|
68
|
-
subject.valid_terminator?(" ").should be_false
|
69
|
-
end
|
70
|
-
|
71
|
-
it "returns false if last character is not a ;" do
|
72
|
-
subject.valid_terminator?(";a").should be_false
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe "parse_strings_key_value" do
|
77
|
-
it "should raise an error if there is no value for a line" do
|
78
|
-
lambda { subject.parse_strings_key_value("\"a.b\" = ;") }.should raise_error(InvalidValueError)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should raise an error if there is value for a line" do
|
82
|
-
lambda { subject.parse_strings_key_value("\"a.b\" = asdf asdf;") }.should raise_error(InvalidValueError)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should raise an error if there is no key for a line" do
|
86
|
-
lambda { subject.parse_strings_key_value(" = c;") }.should raise_error(InvalidKeyError)
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should raise an error if there is no terminator" do
|
90
|
-
lambda { subject.parse_strings_key_value("a.b = c") }.should raise_error(InvalidTerminatorError)
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should work fine" do
|
94
|
-
key_string, value = subject.parse_strings_key_value("a.b = c;")
|
95
|
-
key_string.should eql("a.b")
|
96
|
-
value.should eql("c")
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
describe "remove_quotes_from_key_string" do
|
101
|
-
it "should remove \" from key_string" do
|
102
|
-
subject.remove_quotes_from_key_string('"a.b"').should eql("a.b")
|
103
|
-
end
|
104
|
-
|
105
|
-
it "should do nothing to the string" do
|
106
|
-
subject.remove_quotes_from_key_string('a.b').should eql("a.b")
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
describe "#nested_hash" do
|
111
|
-
it "handles one element arrays" do
|
112
|
-
subject.nested_hash([:a]).should eql(:a)
|
113
|
-
end
|
114
|
-
|
115
|
-
it "handles two element arrays" do
|
116
|
-
subject.nested_hash([:a, :b]).should eql({a: :b})
|
117
|
-
end
|
118
|
-
|
119
|
-
it "handles three element arrays" do
|
120
|
-
subject.nested_hash([:a, :b, :c]).should eql({a: { b: :c }})
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|