abroad 4.0.3 → 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/abroad/extractors/extractor.rb +3 -3
- data/lib/abroad/extractors/json/json_extractor.rb +2 -2
- data/lib/abroad/extractors/xml/xml_extractor.rb +2 -2
- data/lib/abroad/extractors/yaml/dotted_key_extractor.rb +11 -7
- data/lib/abroad/serializers/serializer.rb +2 -2
- data/lib/abroad/serializers/yaml/rails_serializer.rb +39 -3
- data/lib/abroad/version.rb +1 -1
- data/spec/extractors/yaml/dotted_key_extractor_spec.rb +19 -0
- data/spec/serializers/yaml/rails_serializer_spec.rb +12 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dadd8e40a0bd722140b69e9afc49eae5aa28f28e
|
4
|
+
data.tar.gz: f204d17dffbc54735c6255765e675e809bc0cd29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bca09d5714337ed9456c3dfa774d3ba63287114a4e8d2a6403dfcf0f71267902f9dcdf76840537b9be98231c6bac86a19342f27dd495977a0636021d2ab09c1d
|
7
|
+
data.tar.gz: 63b5cc4df1bef7e34f0501932b22c5e7c54f90e13be4738d9b6664848310e8daf3e653c90c1c099968a2922c08110ad4f237ad07e1c360129814d84284ef1b6a
|
@@ -23,8 +23,8 @@ module Abroad
|
|
23
23
|
from_stream(StringIO.new(string), &block)
|
24
24
|
end
|
25
25
|
|
26
|
-
def open(file, &block)
|
27
|
-
from_stream(File.open(file,
|
26
|
+
def open(file, mode = 'r', &block)
|
27
|
+
from_stream(File.open(file, mode), &block)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -32,7 +32,7 @@ module Abroad
|
|
32
32
|
@stream = stream
|
33
33
|
end
|
34
34
|
|
35
|
-
def extract_each
|
35
|
+
def extract_each(options = {})
|
36
36
|
raise NotImplementedError,
|
37
37
|
'expected to be implemented in derived classes'
|
38
38
|
end
|
@@ -3,11 +3,11 @@ module Abroad
|
|
3
3
|
module Json
|
4
4
|
|
5
5
|
class JsonExtractor < Extractor
|
6
|
-
def extract_each(&block)
|
6
|
+
def extract_each(options = {}, &block)
|
7
7
|
if block_given?
|
8
8
|
each_entry(&block)
|
9
9
|
else
|
10
|
-
to_enum(__method__)
|
10
|
+
to_enum(__method__, options)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -3,26 +3,30 @@ module Abroad
|
|
3
3
|
module Yaml
|
4
4
|
|
5
5
|
class DottedKeyExtractor < YamlExtractor
|
6
|
-
def extract_each(&block)
|
6
|
+
def extract_each(options = {}, &block)
|
7
7
|
if block_given?
|
8
|
-
walk(parse, [], &block)
|
8
|
+
walk(parse, [], options, &block)
|
9
9
|
else
|
10
|
-
to_enum(__method__)
|
10
|
+
to_enum(__method__, options)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
15
15
|
|
16
|
-
def walk(obj, cur_path, &block)
|
16
|
+
def walk(obj, cur_path, options, &block)
|
17
17
|
case obj
|
18
18
|
when Hash
|
19
19
|
obj.each_pair do |key, val|
|
20
20
|
segment = is_numeric?(key) ? "'#{key}'" : key
|
21
|
-
walk(val, cur_path + [segment], &block)
|
21
|
+
walk(val, cur_path + [segment], options, &block)
|
22
22
|
end
|
23
23
|
when Array
|
24
|
-
|
25
|
-
|
24
|
+
if options[:preserve_arrays]
|
25
|
+
yield scrub_path(cur_path).join('.'), obj
|
26
|
+
else
|
27
|
+
obj.each_with_index do |val, idx|
|
28
|
+
walk(val, cur_path + [idx.to_s], options, &block)
|
29
|
+
end
|
26
30
|
end
|
27
31
|
else
|
28
32
|
yield scrub_path(cur_path).join('.'), obj
|
@@ -16,8 +16,7 @@ module Abroad
|
|
16
16
|
|
17
17
|
def write_key_value(key, value)
|
18
18
|
key_parts = split_key(key)
|
19
|
-
|
20
|
-
trie.add(key_parts, encoded_value)
|
19
|
+
trie.add(key_parts, encode(value))
|
21
20
|
end
|
22
21
|
|
23
22
|
def flush
|
@@ -29,6 +28,19 @@ module Abroad
|
|
29
28
|
|
30
29
|
private
|
31
30
|
|
31
|
+
def encode(value)
|
32
|
+
case value
|
33
|
+
when Array
|
34
|
+
value.map { |elem| encode(elem) }
|
35
|
+
else
|
36
|
+
if value.respond_to?(:encode)
|
37
|
+
value.encode(encoding)
|
38
|
+
else
|
39
|
+
value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
32
44
|
def split_key(key)
|
33
45
|
# Doesn't allow dots to come before spaces or at the end of the key.
|
34
46
|
# Uses regex negative lookahead, that's what the (?!) sections are.
|
@@ -67,8 +79,17 @@ module Abroad
|
|
67
79
|
def write_value(node, parent_key)
|
68
80
|
value = (node ? node.value : '') || ''
|
69
81
|
|
82
|
+
case value
|
83
|
+
when Array
|
84
|
+
write_array_value(parent_key, value)
|
85
|
+
else
|
86
|
+
write_textual_value(parent_key, value)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def coerce(value)
|
70
91
|
# coerce numeric values
|
71
|
-
|
92
|
+
case value
|
72
93
|
when /\A\d+\z/
|
73
94
|
value.to_i
|
74
95
|
when /\A\d+\.\d+\z/
|
@@ -76,6 +97,10 @@ module Abroad
|
|
76
97
|
else
|
77
98
|
value
|
78
99
|
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def write_textual_value(parent_key, value)
|
103
|
+
value = coerce(value)
|
79
104
|
|
80
105
|
if writer.in_map?
|
81
106
|
writer.write_key_value(parent_key, value)
|
@@ -84,6 +109,17 @@ module Abroad
|
|
84
109
|
end
|
85
110
|
end
|
86
111
|
|
112
|
+
def write_array_value(parent_key, elements)
|
113
|
+
# we should _always_ be in a map, but just in case...
|
114
|
+
writer.write_sequence(parent_key) if writer.in_map?
|
115
|
+
|
116
|
+
elements.each do |element|
|
117
|
+
writer.write_element(coerce(element))
|
118
|
+
end
|
119
|
+
|
120
|
+
writer.close_sequence
|
121
|
+
end
|
122
|
+
|
87
123
|
def write_map(node, parent_key)
|
88
124
|
if writer.in_map?
|
89
125
|
writer.write_map(parent_key)
|
data/lib/abroad/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Abroad::Extractors::Yaml::DottedKeyExtractor do
|
6
|
+
it 'preserves arrays as single values when asked' do
|
7
|
+
content = YAML.dump(foo: { bar: %w(a b c) })
|
8
|
+
extractor = Abroad::Extractors::Yaml::DottedKeyExtractor.from_string(content)
|
9
|
+
enum = extractor.extract_each(preserve_arrays: true)
|
10
|
+
|
11
|
+
phrases = enum.with_object({}) do |(key, value), ret|
|
12
|
+
ret[key] = value
|
13
|
+
end
|
14
|
+
|
15
|
+
expect(phrases).to eq(
|
16
|
+
'foo.bar' => %w(a b c)
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
@@ -1,12 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
describe Yaml::RailsSerializer do
|
3
|
+
describe Abroad::Serializers::Yaml::RailsSerializer do
|
6
4
|
let(:stream) { StringIO.new }
|
7
5
|
let(:locale) { 'fr' }
|
8
6
|
let(:serializer) do
|
9
|
-
Yaml::RailsSerializer.new(stream, locale)
|
7
|
+
Abroad::Serializers::Yaml::RailsSerializer.new(stream, locale)
|
10
8
|
end
|
11
9
|
|
12
10
|
def serialize
|
@@ -133,6 +131,16 @@ describe Yaml::RailsSerializer do
|
|
133
131
|
})
|
134
132
|
end
|
135
133
|
|
134
|
+
it 'writes successfully if given an array object instead of a string' do
|
135
|
+
result = serialize do
|
136
|
+
serializer.write_key_value('foo.bar', %w(a b c))
|
137
|
+
end
|
138
|
+
|
139
|
+
expect(result).to eq({
|
140
|
+
'fr' => { 'foo' => { 'bar' => %w(a b c) } }
|
141
|
+
})
|
142
|
+
end
|
143
|
+
|
136
144
|
it 'does not write arrays for sequential but non-numeric keys' do
|
137
145
|
result = serialize do
|
138
146
|
serializer.write_key_value('foo.bar1', 'b')
|
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.0
|
4
|
+
version: 4.1.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: 2016-
|
11
|
+
date: 2016-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: htmlentities
|
@@ -147,6 +147,7 @@ files:
|
|
147
147
|
- spec/extractors/xml/fixtures/newlines.xml
|
148
148
|
- spec/extractors/xml/fixtures/quotes.xml
|
149
149
|
- spec/extractors/xml/xml_extractor_spec.rb
|
150
|
+
- spec/extractors/yaml/dotted_key_extractor_spec.rb
|
150
151
|
- spec/extractors/yaml/fixtures.yml
|
151
152
|
- spec/extractors/yaml/fixtures/arrays.yml
|
152
153
|
- spec/extractors/yaml/fixtures/arrays_and_hashes.yml
|