abroad 4.4.0 → 4.5.0

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: 6c7d8c0c7ebd22afaf330d5780b47f5ff06562d4
4
- data.tar.gz: 04edfad7c29a9e50f7b48fdbfee57242d629f13e
3
+ metadata.gz: 5348d2c2cb1364b7537eff619d098424e7a75b59
4
+ data.tar.gz: 0e81e30e065c6cd40e1f9388e55cdf15336aec38
5
5
  SHA512:
6
- metadata.gz: 607324c7da61ef85f86e860e5ab453228e9e50c0f5a3df830796aac6f08299f8309bf2ac182657a7d1dd5f7dd07210b6ce86ecf8c8e028cc686e1b1fb3125989
7
- data.tar.gz: 7a14bcc1639c089b0c995c22a21026991bc4783334428750b68ce857870cb93403a618ab4324f7e67d478f5ee71421e45e459e8c1cf836627bbc1671c32e11af
6
+ metadata.gz: f91182af853c8dc4a173138c9edf9910a91da23495d6bfbc1c6a618611c0471de1b9e4c9e6254b3d0886ba20ac93c373499f6993668837382842d8d394c8ba59
7
+ data.tar.gz: ad1c5ccbdd511ef917b1aac506cb6dfea7adc42f0fe90b3043f9c1524f256d157cfd0d53581ee9c549eb82239b720ec5b859a79fb2d2c386b738923e554fc37b
@@ -7,11 +7,13 @@ module Abroad
7
7
  Extractors.register('yaml/rails', Extractors::Yaml::RailsExtractor)
8
8
  Extractors.register('yaml/dotted-key', Extractors::Yaml::DottedKeyExtractor)
9
9
  Extractors.register('json/key-value', Extractors::Json::KeyValueExtractor)
10
+ Extractors.register('json/dotted-key', Extractors::Json::DottedKeyExtractor)
10
11
  Extractors.register('xml/android', Extractors::Xml::AndroidExtractor)
11
12
  Extractors.register('txt/lines', Extractors::Txt::LinesExtractor)
12
13
 
13
14
  Serializers.register('yaml/rails', Serializers::Yaml::RailsSerializer)
14
15
  Serializers.register('json/key-value', Serializers::Json::KeyValueSerializer)
16
+ Serializers.register('json/dotted-key', Serializers::Json::DottedKeySerializer)
15
17
  Serializers.register('xml/android', Serializers::Xml::AndroidSerializer)
16
18
  Serializers.register('txt/lines', Serializers::Txt::LinesSerializer)
17
19
 
@@ -1,8 +1,9 @@
1
1
  module Abroad
2
2
  module Extractors
3
3
  module Json
4
- autoload :KeyValueExtractor, 'abroad/extractors/json/key_value_extractor'
5
4
  autoload :JsonExtractor, 'abroad/extractors/json/json_extractor'
5
+ autoload :KeyValueExtractor, 'abroad/extractors/json/key_value_extractor'
6
+ autoload :DottedKeyExtractor, 'abroad/extractors/json/dotted_key_extractor'
6
7
  end
7
8
  end
8
9
  end
@@ -0,0 +1,64 @@
1
+ require 'json/stream'
2
+
3
+ module Abroad
4
+ module Extractors
5
+ module Json
6
+
7
+ class DottedKeyExtractor < JsonExtractor
8
+
9
+ private
10
+
11
+ def each_entry
12
+ key = []
13
+ nesting_stack = []
14
+
15
+ parser = ::JSON::Stream::Parser.new.tap do |parser|
16
+ parser.key { |k| key.push(k) }
17
+
18
+ parser.value do |val|
19
+ yield key.join('.'), val
20
+
21
+ case nesting_stack.last
22
+ when :object
23
+ key.pop
24
+
25
+ when :array
26
+ key[-1] += 1
27
+ end
28
+ end
29
+
30
+ parser.start_array do
31
+ nesting_stack.push(:array)
32
+ key.push(0)
33
+ end
34
+
35
+ parser.end_array do
36
+ nesting_stack.pop
37
+ key.pop
38
+
39
+ if nesting_stack.last == :object
40
+ key.pop
41
+ else
42
+ key[-1] += 1
43
+ end
44
+ end
45
+
46
+ parser.start_object do
47
+ nesting_stack.push(:object)
48
+ end
49
+
50
+ parser.end_object do
51
+ nesting_stack.pop
52
+ key.pop
53
+ end
54
+ end
55
+
56
+ parser << stream.read
57
+ rescue ::JSON::Stream::ParserError => e
58
+ raise Abroad::SyntaxError, "Syntax error in json: #{e.message}"
59
+ end
60
+
61
+ end
62
+ end
63
+ end
64
+ end
@@ -3,6 +3,7 @@ module Abroad
3
3
  module Json
4
4
  autoload :JsonSerializer, 'abroad/serializers/json/json_serializer'
5
5
  autoload :KeyValueSerializer, 'abroad/serializers/json/key_value_serializer'
6
+ autoload :DottedKeySerializer, 'abroad/serializers/json/dotted_key_serializer'
6
7
  end
7
8
  end
8
9
  end
@@ -0,0 +1,106 @@
1
+ module Abroad
2
+ module Serializers
3
+ module Json
4
+
5
+ class DottedKeySerializer < JsonSerializer
6
+ attr_reader :trie
7
+
8
+ def initialize(stream, locale, options = {})
9
+ super
10
+ @trie = Abroad::Serializers::Trie.new
11
+ end
12
+
13
+ def write_key_value(key, value)
14
+ trie.add(key.split('.'), encode(value))
15
+ end
16
+
17
+ def flush
18
+ write_node(trie.root, nil)
19
+ writer.flush
20
+ stream.flush
21
+ end
22
+
23
+ private
24
+
25
+ def encode(value)
26
+ case value
27
+ when Array
28
+ value.map { |elem| encode(elem) }
29
+ else
30
+ if value.respond_to?(:encode)
31
+ value.encode(encoding)
32
+ else
33
+ value
34
+ end
35
+ end
36
+ end
37
+
38
+ def write_node(node, parent_key)
39
+ if node
40
+ if node.has_children?
41
+ if children_are_sequence?(node)
42
+ write_sequence(node, parent_key)
43
+ else
44
+ write_object(node, parent_key)
45
+ end
46
+ elsif node.has_value?
47
+ write_value(node, parent_key)
48
+ end
49
+ else
50
+ write_value(node, parent_key)
51
+ end
52
+ end
53
+
54
+ def write_value(node, parent_key)
55
+ value = node ? node.value : ''
56
+
57
+ if writer.in_object?
58
+ writer.write_key_value(parent_key, value)
59
+ else
60
+ writer.write_element(value)
61
+ end
62
+ end
63
+
64
+ def write_object(node, parent_key)
65
+ if writer.in_object?
66
+ writer.write_object(parent_key)
67
+ else
68
+ writer.write_object
69
+ end
70
+
71
+ node.each_child do |key, child|
72
+ write_node(child, key)
73
+ end
74
+
75
+ writer.close_object
76
+ end
77
+
78
+ def write_sequence(node, parent_key)
79
+ if writer.in_object?
80
+ writer.write_sequence(parent_key)
81
+ else
82
+ writer.write_sequence
83
+ end
84
+
85
+ generate_sequence(node).each do |element|
86
+ write_node(element, nil)
87
+ end
88
+
89
+ writer.close_sequence
90
+ end
91
+
92
+ def children_are_sequence?(node)
93
+ node.children.all? { |key, _| key =~ /\A[\d]+\z/ }
94
+ end
95
+
96
+ def generate_sequence(node)
97
+ keys = node.children.keys.map(&:to_i)
98
+ keys.each_with_object(Array.new(keys.max)) do |idx, arr|
99
+ arr[idx] = node.children[idx.to_s]
100
+ end
101
+ end
102
+ end
103
+
104
+ end
105
+ end
106
+ end
@@ -25,7 +25,7 @@ module Abroad
25
25
  private
26
26
 
27
27
  def write_options
28
- @write_options = { before: before_text, between: between_text }
28
+ @write_options ||= { before: before_text, between: between_text }
29
29
  end
30
30
 
31
31
  def before_text
@@ -1,3 +1,3 @@
1
1
  module Abroad
2
- VERSION = '4.4.0'
2
+ VERSION = '4.5.0'
3
3
  end
@@ -20,3 +20,32 @@
20
20
  string: cool string
21
21
  - key: jelly.beans
22
22
  string: popcorn
23
+ :json/dotted-key:
24
+ nested_basic.json:
25
+ - key: foo.bar
26
+ string: baz
27
+ - key: hello
28
+ string: cool string
29
+ - key: jelly.beans
30
+ string: popcorn
31
+ - key: jelly.and.delicious
32
+ string: red vines
33
+ nested_arrays.json:
34
+ - key: array1.0
35
+ string: first
36
+ - key: array1.1
37
+ string: second
38
+ - key: array1.2
39
+ string: third
40
+ - key: arrayObj.array2.0
41
+ string: fourth
42
+ - key: arrayObj.array2.1
43
+ string: fifth
44
+ - key: jelly.beans
45
+ string: popcorn
46
+ - key: arrayOfArrays.0.0
47
+ string: abc
48
+ - key: arrayOfArrays.0.1
49
+ string: def
50
+ - key: arrayOfArrays.1
51
+ string: ghi
@@ -0,0 +1,10 @@
1
+ {
2
+ "array1": ["first", "second", "third"],
3
+ "arrayObj": {
4
+ "array2": ["fourth", "fifth"]
5
+ },
6
+ "jelly": {
7
+ "beans": "popcorn"
8
+ },
9
+ "arrayOfArrays": [["abc", "def"], "ghi"]
10
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "hello": "cool string",
3
+ "foo": {
4
+ "bar": "baz"
5
+ },
6
+ "jelly": {
7
+ "beans": "popcorn",
8
+ "and": {
9
+ "delicious": "red vines"
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ include Abroad::Serializers
4
+
5
+ describe Json::DottedKeySerializer do
6
+ let(:stream) { StringIO.new }
7
+ let(:locale) { 'fr-FR' }
8
+
9
+ let(:serializer) do
10
+ Json::DottedKeySerializer.new(stream, locale)
11
+ end
12
+
13
+ def serialize
14
+ yield
15
+ serializer.flush
16
+ stream.string
17
+ end
18
+
19
+ it 'writes key/value pairs' do
20
+ result = serialize do
21
+ serializer.write_key_value('com.foo.bar', 'baz')
22
+ end
23
+
24
+ expect(result).to eq("{\"com\":{\"foo\":{\"bar\":\"baz\"}}}")
25
+ end
26
+ end
@@ -37,6 +37,8 @@ RSpec.configure do |config|
37
37
  expected_phrase['key'] == actual_key
38
38
  end
39
39
 
40
+ next unless expected_string_index
41
+
40
42
  expected_phrase = expected_phrases[expected_string_index]
41
43
  expect(expected_phrase).to_not be_nil
42
44
  expect(expected_phrase['string']).to eq(actual_string)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abroad
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.0
4
+ version: 4.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-27 00:00:00.000000000 Z
11
+ date: 2018-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: htmlentities
@@ -113,6 +113,7 @@ files:
113
113
  - lib/abroad/extractors.rb
114
114
  - lib/abroad/extractors/extractor.rb
115
115
  - lib/abroad/extractors/json.rb
116
+ - lib/abroad/extractors/json/dotted_key_extractor.rb
116
117
  - lib/abroad/extractors/json/json_extractor.rb
117
118
  - lib/abroad/extractors/json/key_value_extractor.rb
118
119
  - lib/abroad/extractors/txt.rb
@@ -128,6 +129,7 @@ files:
128
129
  - lib/abroad/extractors/yaml/yaml_extractor.rb
129
130
  - lib/abroad/serializers.rb
130
131
  - lib/abroad/serializers/json.rb
132
+ - lib/abroad/serializers/json/dotted_key_serializer.rb
131
133
  - lib/abroad/serializers/json/json_serializer.rb
132
134
  - lib/abroad/serializers/json/key_value_serializer.rb
133
135
  - lib/abroad/serializers/serializer.rb
@@ -148,6 +150,8 @@ files:
148
150
  - spec/extractors/json/fixtures.yml
149
151
  - spec/extractors/json/fixtures/arrays.json
150
152
  - spec/extractors/json/fixtures/basic.json
153
+ - spec/extractors/json/fixtures/nested_arrays.json
154
+ - spec/extractors/json/fixtures/nested_basic.json
151
155
  - spec/extractors/json/fixtures/objects.json
152
156
  - spec/extractors/json/json_extractor_spec.rb
153
157
  - spec/extractors/txt/lines_extractor_spec.rb
@@ -172,6 +176,7 @@ files:
172
176
  - spec/extractors/yaml/fixtures/short.yml
173
177
  - spec/extractors/yaml/jruby_compat_spec.rb
174
178
  - spec/extractors/yaml/yaml_extractor_spec.rb
179
+ - spec/serializers/json/dotted_key_serializer_spec.rb
175
180
  - spec/serializers/json/key_value_serializer_spec.rb
176
181
  - spec/serializers/txt/lines_serializer_spec.rb
177
182
  - spec/serializers/xml/android_serializer_spec.rb
@@ -196,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
201
  version: '0'
197
202
  requirements: []
198
203
  rubyforge_project:
199
- rubygems_version: 2.6.11
204
+ rubygems_version: 2.4.5.5
200
205
  signing_key:
201
206
  specification_version: 4
202
207
  summary: A set of parsers and serializers for dealing with localization file formats.