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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89386cbcccef7a58f59510df50bd148a1759b244
4
- data.tar.gz: da9806a5703f00406080302a111b3142b7f4b74c
3
+ metadata.gz: dadd8e40a0bd722140b69e9afc49eae5aa28f28e
4
+ data.tar.gz: f204d17dffbc54735c6255765e675e809bc0cd29
5
5
  SHA512:
6
- metadata.gz: fcb3fca5ae80ac040aaed3863f916dc244d638bd573d33dea66ddf8ce861ec2747304e6d33eebad7aa24891dc89193d9a9c1a21a5e76e5ddab14e8a80f7256ca
7
- data.tar.gz: cb5ec47a06195eeba1edaa71f40dedf54048ae90c54ecb82d5ceb9432ccd68cb4902d2bbc31619b8c49ea00a1f6aa94b4e0425509b53ceed34b1bf70e52cfdfc
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, 'r'), &block)
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
@@ -5,11 +5,11 @@ module Abroad
5
5
  module Xml
6
6
 
7
7
  class XmlExtractor < Extractor
8
- def extract_each(&block)
8
+ def extract_each(options = {}, &block)
9
9
  if block_given?
10
10
  each_entry(&block)
11
11
  else
12
- to_enum(__method__)
12
+ to_enum(__method__, options)
13
13
  end
14
14
  end
15
15
 
@@ -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
- obj.each_with_index do |val, idx|
25
- walk(val, cur_path + [idx.to_s], &block)
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
@@ -19,8 +19,8 @@ module Abroad
19
19
  end
20
20
  end
21
21
 
22
- def open(file, locale, &block)
23
- from_stream(File.open(file), locale, &block)
22
+ def open(file, locale, mode = 'r', &block)
23
+ from_stream(File.open(file, mode), locale, &block)
24
24
  end
25
25
  end
26
26
 
@@ -16,8 +16,7 @@ module Abroad
16
16
 
17
17
  def write_key_value(key, value)
18
18
  key_parts = split_key(key)
19
- encoded_value = value.encode(encoding) if value
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
- value = case value
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)
@@ -1,3 +1,3 @@
1
1
  module Abroad
2
- VERSION = '4.0.3'
2
+ VERSION = '4.1.0'
3
3
  end
@@ -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
- include Abroad::Serializers
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.3
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-07-20 00:00:00.000000000 Z
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