yaml_converters 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ script: bundle exec rspec spec
6
+ notifications:
7
+ email:
8
+ - ja@jestem.tw
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## Version 0.2 (2012-07-29)
4
+
5
+ * Allow to read YAML file contents from a different source (file, AWS)
6
+
3
7
  ## Version 0.1 (2012-07-29)
4
8
 
5
9
  * Add CHANGELOG
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # YamlConverters
1
+ # YamlConverters [![Build Status](https://secure.travis-ci.org/tomekw/yaml_converters.png)](http://travis-ci.org/tomekw/yaml_converters)
2
2
 
3
3
  Convert YAML to segments and back.
4
4
 
@@ -16,11 +16,26 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install yaml_converters
18
18
 
19
+ Supported Ruby versions:
20
+
21
+ * `1.9.3`
22
+ * `1.9.2`
23
+
19
24
  ## Usage
20
25
 
26
+ ### Converting YAML into segments
27
+
21
28
  Use `YamlConverters::YamlToSegmentsConverter#convert` to split YAML file
22
29
  into flat key-value pairs.
23
30
 
31
+ ``` ruby
32
+ yaml_reader = YamlConverters::YamlFileReader.new('path/to/file.yml')
33
+ converter = YamlConverters::YamlToSegmentsConverter.new(
34
+ yaml_reader, YamlConverters::SegmentToHashWriter.new
35
+ )
36
+ converter.convert # => { 'key' => 'value', ... }
37
+ ```
38
+
24
39
  You can pass custom `segment_writer` to dump generated segments
25
40
  somewhere (database, for example). Custom writer should respond to
26
41
  two public methods:
@@ -47,9 +62,40 @@ module YamlConverters
47
62
  end
48
63
  ```
49
64
 
65
+ You can pass custom `yaml_reader` to read YAML file from
66
+ a different source (AWS, for example). Custom reader should respond to
67
+ one public method:
68
+
69
+ * `read` - returns string containing YAML file contents
70
+
71
+ Example `yaml_reader`:
72
+
73
+ ``` ruby
74
+ module YamlConverters
75
+ class YamlFileReader
76
+ def initialize(file_path)
77
+ @file_path = file_path
78
+ end
79
+
80
+ def read
81
+ File.read(@file_path)
82
+ end
83
+ end
84
+ end
85
+ ```
86
+
87
+ ### Converting segments into YAML
88
+
50
89
  Use `YamlConverters::SegmentsToYamlConverter#convert` to dump segments
51
90
  (key-value pairs) to YAML file.
52
91
 
92
+ ``` ruby
93
+ converter = YamlConverters::SegmentsToYamlConverter(
94
+ { 'key' => 'value' }, YamlConverters::YamlToStringWriter.new
95
+ )
96
+ converter.convert # => "---\nkey: value\n"
97
+ ```
98
+
53
99
  You can pass custom `yaml_writer` to dump generated YAML file
54
100
  somewhere (file, for example). Custom writer should respond to
55
101
  two public methods:
@@ -81,6 +127,10 @@ end
81
127
  Implementation of flattening a YAML file and generating a YAML file
82
128
  from key-value pairs taken (I hope, for now) from Tolk codebase.
83
129
 
130
+ ## Warning
131
+
132
+ Until version `1.0.0` is reached API may be changed without any notice!
133
+
84
134
  ## Contributing
85
135
 
86
136
  1. Fork it
@@ -1,3 +1,3 @@
1
1
  module YamlConverters
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,11 @@
1
+ module YamlConverters
2
+ class YamlFileReader
3
+ def initialize(file_path)
4
+ @file_path = file_path
5
+ end
6
+
7
+ def read
8
+ File.read(@file_path)
9
+ end
10
+ end
11
+ end
@@ -1,7 +1,7 @@
1
1
  module YamlConverters
2
2
  class YamlToSegmentsConverter
3
- def initialize(file_path, segment_writer = YamlConverters::SegmentToHashWriter.new)
4
- @file_path = file_path
3
+ def initialize(yaml_reader, segment_writer = YamlConverters::SegmentToHashWriter.new)
4
+ @yaml_reader = yaml_reader
5
5
  @segment_writer = segment_writer
6
6
  end
7
7
 
@@ -14,8 +14,7 @@ module YamlConverters
14
14
  private
15
15
 
16
16
  def file_contents
17
- # TODO: Allow fetching from different source
18
- @file_contents ||= File.read(@file_path)
17
+ @file_contents ||= @yaml_reader.read
19
18
  end
20
19
 
21
20
  def raw_yaml_hash
@@ -5,6 +5,7 @@ require 'yaml_converters/yaml_to_segments_converter'
5
5
  require 'yaml_converters/segments_to_yaml_converter'
6
6
  require 'yaml_converters/segment_to_hash_writer'
7
7
  require 'yaml_converters/yaml_to_string_writer'
8
+ require 'yaml_converters/yaml_file_reader'
8
9
 
9
10
  module YamlConverters
10
11
  SEGMENT_KEY_DELIMITER = '.'
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'YAML to segments conversion' do
4
+ subject { YamlConverters::YamlToSegmentsConverter.new(yaml_reader) }
5
+ let(:yaml_reader) { YamlConverters::YamlFileReader.new(file_path) }
6
+ let(:file_path) { 'spec/fixtures/deeply_nested_key_value_pairs.yml' }
7
+
8
+ its(:convert) do
9
+ should eq({
10
+ 'grandmother.mother.daughter' => 'olga',
11
+ 'grandmother.mother.sister' => 'ania',
12
+ 'grandfather' => 'eugeniusz'
13
+ })
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe YamlConverters::YamlFileReader do
4
+ subject { described_class.new(file_path) }
5
+ let(:file_path) { 'spec/fixtures/one_key_value_pair.yml' }
6
+ let(:expected_yaml_string) { "---\nparent:\n child: value\n" }
7
+
8
+ before do
9
+ File.stub(:read).with(file_path).and_return(expected_yaml_string)
10
+ end
11
+
12
+ its(:read) { should eq(expected_yaml_string) }
13
+ end
@@ -1,28 +1,31 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe YamlConverters::YamlToSegmentsConverter do
4
- subject { described_class.new(file_path) }
4
+ subject { described_class.new(yaml_reader) }
5
+ let(:yaml_reader) { stub(read: file_contents) }
5
6
 
6
7
  context 'simple one key-value pair file' do
7
- let(:file_path) { 'spec/fixtures/one_key_value_pair.yml' }
8
+ let(:file_contents) { "---\nkey: value\n" }
8
9
 
9
10
  its(:convert) { should eq({ 'key' => 'value' }) }
10
11
  end
11
12
 
12
13
  context 'simple two key-value pairs file' do
13
- let(:file_path) { 'spec/fixtures/two_key_value_pairs.yml' }
14
+ let(:file_contents) { "---\nkey1: value1\nkey2: value2\n" }
14
15
 
15
16
  its(:convert) { should eq({ 'key1' => 'value1', 'key2' => 'value2' }) }
16
17
  end
17
18
 
18
19
  context 'nested key-value pair file' do
19
- let(:file_path) { 'spec/fixtures/nested_key_value_pair.yml' }
20
+ let(:file_contents) { "---\nparent:\n child: value\n" }
20
21
 
21
22
  its(:convert) { should eq({ 'parent.child' => 'value' }) }
22
23
  end
23
24
 
24
25
  context 'deeply ested key-value pairs file' do
25
- let(:file_path) { 'spec/fixtures/deeply_nested_key_value_pairs.yml' }
26
+ let(:file_contents) do
27
+ "---\ngrandmother:\n mother:\n daughter: olga\n sister: ania\ngrandfather: eugeniusz\n"
28
+ end
26
29
 
27
30
  its(:convert) do
28
31
  should eq({
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_converters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -100,6 +100,7 @@ extra_rdoc_files: []
100
100
  files:
101
101
  - .gitignore
102
102
  - .rspec
103
+ - .travis.yml
103
104
  - CHANGELOG.md
104
105
  - Gemfile
105
106
  - LICENSE.txt
@@ -109,17 +110,20 @@ files:
109
110
  - lib/yaml_converters/segment_to_hash_writer.rb
110
111
  - lib/yaml_converters/segments_to_yaml_converter.rb
111
112
  - lib/yaml_converters/version.rb
113
+ - lib/yaml_converters/yaml_file_reader.rb
112
114
  - lib/yaml_converters/yaml_to_segments_converter.rb
113
115
  - lib/yaml_converters/yaml_to_string_writer.rb
114
116
  - spec/fixtures/deeply_nested_key_value_pairs.yml
115
117
  - spec/fixtures/nested_key_value_pair.yml
116
118
  - spec/fixtures/one_key_value_pair.yml
117
119
  - spec/fixtures/two_key_value_pairs.yml
118
- - spec/segment_to_hash_writer_spec.rb
119
- - spec/segments_to_yaml_converter_spec.rb
120
+ - spec/integration/yaml_to_segments_spec.rb
120
121
  - spec/spec_helper.rb
121
- - spec/yaml_to_segments_converter_spec.rb
122
- - spec/yaml_to_string_writer_spec.rb
122
+ - spec/unit/segment_to_hash_writer_spec.rb
123
+ - spec/unit/segments_to_yaml_converter_spec.rb
124
+ - spec/unit/yaml_file_reader_spec.rb
125
+ - spec/unit/yaml_to_segments_converter_spec.rb
126
+ - spec/unit/yaml_to_string_writer_spec.rb
123
127
  - yaml_converters.gemspec
124
128
  homepage: https://github.com/tomekw/yaml_converters
125
129
  licenses: []
@@ -135,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
139
  version: '0'
136
140
  segments:
137
141
  - 0
138
- hash: 4349722421781478138
142
+ hash: -1522767103268194722
139
143
  required_rubygems_version: !ruby/object:Gem::Requirement
140
144
  none: false
141
145
  requirements:
@@ -144,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
148
  version: '0'
145
149
  segments:
146
150
  - 0
147
- hash: 4349722421781478138
151
+ hash: -1522767103268194722
148
152
  requirements: []
149
153
  rubyforge_project:
150
154
  rubygems_version: 1.8.24
@@ -156,8 +160,10 @@ test_files:
156
160
  - spec/fixtures/nested_key_value_pair.yml
157
161
  - spec/fixtures/one_key_value_pair.yml
158
162
  - spec/fixtures/two_key_value_pairs.yml
159
- - spec/segment_to_hash_writer_spec.rb
160
- - spec/segments_to_yaml_converter_spec.rb
163
+ - spec/integration/yaml_to_segments_spec.rb
161
164
  - spec/spec_helper.rb
162
- - spec/yaml_to_segments_converter_spec.rb
163
- - spec/yaml_to_string_writer_spec.rb
165
+ - spec/unit/segment_to_hash_writer_spec.rb
166
+ - spec/unit/segments_to_yaml_converter_spec.rb
167
+ - spec/unit/yaml_file_reader_spec.rb
168
+ - spec/unit/yaml_to_segments_converter_spec.rb
169
+ - spec/unit/yaml_to_string_writer_spec.rb