abroad 4.1.3 → 4.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f814fbe28b3a7cc583852c28b409cb1af6fae4d
4
- data.tar.gz: 1e3a91d89598201db9b378884caabda6bca16918
3
+ metadata.gz: 0fe36cd26c80451ce1514284dd1d4948797e8a95
4
+ data.tar.gz: c1645f6188fc0494a51153bfb2a06ca4220b697f
5
5
  SHA512:
6
- metadata.gz: ed65796bcf2919b65b0379899a5b09e37be18fa5a510344a83b6eb3e11ddb2b9734a58578ce4f248a037208ea437596899f4d7b8b53cf427d7b19a9d9b77bf35
7
- data.tar.gz: 57f8ef7d24417e3bc58c3271452a35a4d744bd81ae453539d66324c58cf77e9b3ea5337e74f05683eb632ca99fd9f58b38f93ed7e42ab816ff6d2e72db674b39
6
+ metadata.gz: 9995680ee6439cad5c4d6086fa3efde9fcd9b12ffc2c8cf410d97aa5a6dca9539b0bcd49e1c66dd444045bed8c413098f90fa43dc205968e8588b5bd07ac7c5d
7
+ data.tar.gz: e6609ea0b1c92ba0130db811f359e7aa48daf9659e787340bb671d952b0720fe1e25361c2690e2a425d7b32f41bc96d636cd50b2521037f0bdc8a75863dd09bf
data/lib/abroad.rb CHANGED
@@ -8,10 +8,12 @@ module Abroad
8
8
  Extractors.register('yaml/dotted-key', Extractors::Yaml::DottedKeyExtractor)
9
9
  Extractors.register('json/key-value', Extractors::Json::KeyValueExtractor)
10
10
  Extractors.register('xml/android', Extractors::Xml::AndroidExtractor)
11
+ Extractors.register('txt/lines', Extractors::Txt::LinesExtractor)
11
12
 
12
13
  Serializers.register('yaml/rails', Serializers::Yaml::RailsSerializer)
13
14
  Serializers.register('json/key-value', Serializers::Json::KeyValueSerializer)
14
15
  Serializers.register('xml/android', Serializers::Xml::AndroidSerializer)
16
+ Serializers.register('txt/lines', Serializers::Txt::LinesSerializer)
15
17
 
16
18
  class << self
17
19
  def extractors
@@ -2,6 +2,7 @@ module Abroad
2
2
  module Extractors
3
3
  autoload :Extractor, 'abroad/extractors/extractor'
4
4
  autoload :Json, 'abroad/extractors/json'
5
+ autoload :Txt, 'abroad/extractors/txt'
5
6
  autoload :Xml, 'abroad/extractors/xml'
6
7
  autoload :Yaml, 'abroad/extractors/yaml'
7
8
 
@@ -0,0 +1,8 @@
1
+ module Abroad
2
+ module Extractors
3
+ module Txt
4
+ autoload :LinesExtractor, 'abroad/extractors/txt/lines_extractor'
5
+ autoload :TxtExtractor, 'abroad/extractors/txt/txt_extractor'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ require 'json/stream'
2
+
3
+ module Abroad
4
+ module Extractors
5
+ module Txt
6
+
7
+ class LinesExtractor < TxtExtractor
8
+ private
9
+
10
+ def each_entry
11
+ return to_enum(__method__) unless block_given?
12
+
13
+ stream.each_line.with_index do |line, idx|
14
+ # keys should always be strings
15
+ yield idx.to_s, line.chomp
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ module Abroad
2
+ module Extractors
3
+ module Txt
4
+
5
+ class TxtExtractor < Extractor
6
+ def extract_each(options = {}, &block)
7
+ if block_given?
8
+ each_entry(&block)
9
+ else
10
+ to_enum(__method__, options)
11
+ end
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -2,6 +2,7 @@ module Abroad
2
2
  module Serializers
3
3
  autoload :Json, 'abroad/serializers/json'
4
4
  autoload :Serializer, 'abroad/serializers/serializer'
5
+ autoload :Txt, 'abroad/serializers/txt'
5
6
  autoload :Trie, 'abroad/serializers/trie'
6
7
  autoload :Xml, 'abroad/serializers/xml'
7
8
  autoload :Yaml, 'abroad/serializers/yaml'
@@ -0,0 +1,8 @@
1
+ module Abroad
2
+ module Serializers
3
+ module Txt
4
+ autoload :TxtSerializer, 'abroad/serializers/txt/txt_serializer'
5
+ autoload :LinesSerializer, 'abroad/serializers/txt/lines_serializer'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,52 @@
1
+ module Abroad
2
+ module Serializers
3
+ module Txt
4
+
5
+ class LinesSerializer < TxtSerializer
6
+ DEFAULT_NEWLINE_SEPARATOR = "\n"
7
+
8
+ attr_reader :newline_separator, :lines
9
+
10
+ def initialize(stream, locale, options = {})
11
+ @newline_separator = options.fetch(:newline_separator, DEFAULT_NEWLINE_SEPARATOR)
12
+ @lines = []
13
+ super
14
+ end
15
+
16
+ def write_key_value(key, value)
17
+ unless valid_key?(key)
18
+ raise KeyError, "'#{key}' is not a valid key"
19
+ end
20
+
21
+ add_line(key, value)
22
+ end
23
+
24
+ def flush
25
+ stream.write(lines.join(newline_separator))
26
+ super
27
+ end
28
+
29
+ private
30
+
31
+ def add_line(key, value)
32
+ key = parse_key(key)
33
+
34
+ if key >= lines.size
35
+ lines.concat(Array.new(key - lines.size + 1) { '' })
36
+ end
37
+
38
+ lines[key] = value
39
+ end
40
+
41
+ def parse_key(key)
42
+ key.to_i
43
+ end
44
+
45
+ def valid_key?(key)
46
+ !!(key =~ /[\d]+/)
47
+ end
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,15 @@
1
+ require 'stringio'
2
+
3
+ module Abroad
4
+ module Serializers
5
+ module Txt
6
+
7
+ class TxtSerializer < Serializer
8
+ def write_raw(text)
9
+ stream.write(text)
10
+ end
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Abroad
2
- VERSION = '4.1.3'
2
+ VERSION = '4.2.0'
3
3
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ include Abroad::Extractors
4
+
5
+ describe Txt::LinesExtractor do
6
+ it 'extracts text into lines' do
7
+ text = outdent(%Q(
8
+ foo
9
+
10
+ bar
11
+
12
+ baz
13
+ boo
14
+ ))
15
+
16
+ strings = described_class.from_string(text).extract_each.to_a
17
+
18
+ expect(strings).to eq([
19
+ ['0', 'foo'], ['1', ''], ['2', 'bar'],
20
+ ['3', ''], ['4', 'baz'], ['5', 'boo']
21
+ ])
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ include Abroad::Serializers
4
+
5
+ describe Txt::LinesSerializer do
6
+ let(:stream) { StringIO.new }
7
+ let(:locale) { 'en' }
8
+ let(:serializer) { described_class.from_stream(stream, locale) }
9
+
10
+ it 'writes key/value pairs' do
11
+ serializer.write_key_value('2', 'foo')
12
+ serializer.write_key_value('3', 'bar')
13
+ serializer.write_key_value('5', 'baz')
14
+
15
+ serializer.close
16
+ expect(stream.string).to eq(outdent(%Q(
17
+
18
+
19
+ foo
20
+ bar
21
+
22
+ baz
23
+ )).rstrip)
24
+ end
25
+
26
+ it 'raises an error if given a non-numeric key' do
27
+ expect { serializer.write_key_value('abc', 'def') }.to(
28
+ raise_error(KeyError)
29
+ )
30
+ end
31
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,10 +4,19 @@ require 'rspec'
4
4
  require 'abroad'
5
5
  require 'yaml'
6
6
 
7
- RSpec.configure do |config|
8
- def camelize(str)
9
- str.gsub(/(^\w|[-_]\w)/) { $1[-1].upcase }
7
+ module SpecHelpers
8
+ def outdent(str)
9
+ # The special YAML pipe operator treats the text that follows as literal,
10
+ # and includes newlines, tabs, and spaces. It also strips leading tabs and
11
+ # spaces. This means you can include a fully indented bit of, say, source
12
+ # code in your source code, and it will give you back a string with all the
13
+ # indentation preserved (but without any leading indentation).
14
+ YAML.load("|#{str}")
10
15
  end
16
+ end
17
+
18
+ RSpec.configure do |config|
19
+ config.include(SpecHelpers)
11
20
 
12
21
  shared_examples 'a fixture-based extractor' do |dir, namespace|
13
22
  fixture_manifest = YAML.load_file(File.join(dir, 'fixtures.yml'))
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.1.3
4
+ version: 4.2.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-08-10 00:00:00.000000000 Z
11
+ date: 2016-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: htmlentities
@@ -109,6 +109,9 @@ files:
109
109
  - lib/abroad/extractors/json.rb
110
110
  - lib/abroad/extractors/json/json_extractor.rb
111
111
  - lib/abroad/extractors/json/key_value_extractor.rb
112
+ - lib/abroad/extractors/txt.rb
113
+ - lib/abroad/extractors/txt/lines_extractor.rb
114
+ - lib/abroad/extractors/txt/txt_extractor.rb
112
115
  - lib/abroad/extractors/xml.rb
113
116
  - lib/abroad/extractors/xml/android_extractor.rb
114
117
  - lib/abroad/extractors/xml/xml_extractor.rb
@@ -123,6 +126,9 @@ files:
123
126
  - lib/abroad/serializers/json/key_value_serializer.rb
124
127
  - lib/abroad/serializers/serializer.rb
125
128
  - lib/abroad/serializers/trie.rb
129
+ - lib/abroad/serializers/txt.rb
130
+ - lib/abroad/serializers/txt/lines_serializer.rb
131
+ - lib/abroad/serializers/txt/txt_serializer.rb
126
132
  - lib/abroad/serializers/xml.rb
127
133
  - lib/abroad/serializers/xml/android_serializer.rb
128
134
  - lib/abroad/serializers/xml/xml_serializer.rb
@@ -138,6 +144,7 @@ files:
138
144
  - spec/extractors/json/fixtures/basic.json
139
145
  - spec/extractors/json/fixtures/objects.json
140
146
  - spec/extractors/json/json_extractor_spec.rb
147
+ - spec/extractors/txt/lines_extractor_spec.rb
141
148
  - spec/extractors/xml/fixtures.yml
142
149
  - spec/extractors/xml/fixtures/basic_arrays.xml
143
150
  - spec/extractors/xml/fixtures/basic_plurals.xml
@@ -159,6 +166,7 @@ files:
159
166
  - spec/extractors/yaml/jruby_compat_spec.rb
160
167
  - spec/extractors/yaml/yaml_extractor_spec.rb
161
168
  - spec/serializers/json/key_value_serializer_spec.rb
169
+ - spec/serializers/txt/lines_serializer_spec.rb
162
170
  - spec/serializers/xml/android_serializer_spec.rb
163
171
  - spec/serializers/yaml/rails_serializer_spec.rb
164
172
  - spec/spec_helper.rb
@@ -181,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
189
  version: '0'
182
190
  requirements: []
183
191
  rubyforge_project:
184
- rubygems_version: 2.6.6
192
+ rubygems_version: 2.2.5
185
193
  signing_key:
186
194
  specification_version: 4
187
195
  summary: A set of parsers and serializers for dealing with localization file formats.