yaml_converters 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +1 -0
- data/lib/yaml_converters/segment_to_hash_writer.rb +13 -0
- data/lib/yaml_converters/segments_to_yaml_converter.rb +38 -0
- data/lib/yaml_converters/version.rb +3 -0
- data/lib/yaml_converters/yaml_to_segments_converter.rb +40 -0
- data/lib/yaml_converters/yaml_to_string_writer.rb +13 -0
- data/lib/yaml_converters.rb +11 -0
- data/spec/fixtures/deeply_nested_key_value_pairs.yml +6 -0
- data/spec/fixtures/nested_key_value_pair.yml +3 -0
- data/spec/fixtures/one_key_value_pair.yml +2 -0
- data/spec/fixtures/two_key_value_pairs.yml +3 -0
- data/spec/segment_to_hash_writer_spec.rb +12 -0
- data/spec/segments_to_yaml_converter_spec.rb +37 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/yaml_to_segments_converter_spec.rb +35 -0
- data/spec/yaml_to_string_writer_spec.rb +12 -0
- data/yaml_converters.gemspec +26 -0
- metadata +162 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Tomek Wałkuski
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# YamlConverters
|
2
|
+
|
3
|
+
Convert YAML to segments and back.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'yaml_converters'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install yaml_converters
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Use `YamlConverters::YamlToSegmentsConverter#convert` to split YAML file
|
22
|
+
into flat key-value pairs.
|
23
|
+
|
24
|
+
You can pass custom `segment_writer` to dump generated segments
|
25
|
+
somewhere (database, for example). Custom writer should respond to
|
26
|
+
two public methods:
|
27
|
+
|
28
|
+
* `write` - performs actual key-value pair dumping
|
29
|
+
* `result` - returns whatever you want from `convert`
|
30
|
+
|
31
|
+
Default `segment_writer` is an instance of
|
32
|
+
`YamlConverters::SegmentToHashWriter` that returns a `Hash`.
|
33
|
+
|
34
|
+
Use `YamlConverters::SegmentsToYamlConverter#convert` to dump segments
|
35
|
+
(key-value pairs) to YAML file.
|
36
|
+
|
37
|
+
You can pass custom `yaml_writer` to dump generated YAML file
|
38
|
+
somewhere (file, for example). Custom writer should respond to
|
39
|
+
two public methods:
|
40
|
+
|
41
|
+
* `write` - performs actual YAML string dumping
|
42
|
+
* `result` - returns whatever you want from `convert`
|
43
|
+
|
44
|
+
Default `yaml_writer` is an instance of
|
45
|
+
`YamlConverters::YamlToStringWriter` that returns a `String`.
|
46
|
+
|
47
|
+
## Acknowledgements
|
48
|
+
|
49
|
+
Implementation of flattening a YAML file and generating a YAML file
|
50
|
+
from key-value pairs taken (I hope, for now) from Tolk codebase.
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module YamlConverters
|
2
|
+
class SegmentsToYamlConverter
|
3
|
+
def initialize(segments, yaml_writer = YamlConverters::YamlToStringWriter.new)
|
4
|
+
@segments = segments
|
5
|
+
@yaml_writer = yaml_writer
|
6
|
+
end
|
7
|
+
|
8
|
+
def convert
|
9
|
+
@yaml_writer.write(Psych.dump(raw_yaml_hash))
|
10
|
+
|
11
|
+
@yaml_writer.result
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def raw_yaml_hash
|
17
|
+
@raw_yaml_hash ||= @segments.each_with_object({}) do |(key, value), raw_yaml_hash|
|
18
|
+
if key.include?(YamlConverters::SEGMENT_KEY_DELIMITER)
|
19
|
+
raw_yaml_hash.deep_merge!(unsquish(key, value))
|
20
|
+
else
|
21
|
+
raw_yaml_hash[key] = value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def unsquish(string, value)
|
27
|
+
if string.is_a?(String)
|
28
|
+
unsquish(string.split(YamlConverters::SEGMENT_KEY_DELIMITER), value)
|
29
|
+
elsif string.size == 1
|
30
|
+
{ string.first => value }
|
31
|
+
else
|
32
|
+
key = string[0]
|
33
|
+
rest = string[1..-1]
|
34
|
+
{ key => unsquish(rest, value) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module YamlConverters
|
2
|
+
class YamlToSegmentsConverter
|
3
|
+
def initialize(file_path, segment_writer = YamlConverters::SegmentToHashWriter.new)
|
4
|
+
@file_path = file_path
|
5
|
+
@segment_writer = segment_writer
|
6
|
+
end
|
7
|
+
|
8
|
+
def convert
|
9
|
+
flatten_hash(raw_yaml_hash)
|
10
|
+
|
11
|
+
@segment_writer.result
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def file_contents
|
17
|
+
# TODO: Allow fetching from different source
|
18
|
+
@file_contents ||= File.read(@file_path)
|
19
|
+
end
|
20
|
+
|
21
|
+
def raw_yaml_hash
|
22
|
+
@raw_yaml_hash ||= Psych.load(file_contents)
|
23
|
+
end
|
24
|
+
|
25
|
+
def flatten_hash(hash, prefix = nil, result = {})
|
26
|
+
hash.each do |key, value|
|
27
|
+
current_prefix = prefix ? "#{prefix}#{YamlConverters::SEGMENT_KEY_DELIMITER}#{key}" : String(key)
|
28
|
+
|
29
|
+
if !value.is_a?(Hash)
|
30
|
+
result[current_prefix] = value
|
31
|
+
@segment_writer.write({ current_prefix => value })
|
32
|
+
else
|
33
|
+
flatten_hash(value, current_prefix, result)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
result
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'active_support/core_ext/hash'
|
2
|
+
|
3
|
+
require 'yaml_converters/version'
|
4
|
+
require 'yaml_converters/yaml_to_segments_converter'
|
5
|
+
require 'yaml_converters/segments_to_yaml_converter'
|
6
|
+
require 'yaml_converters/segment_to_hash_writer'
|
7
|
+
require 'yaml_converters/yaml_to_string_writer'
|
8
|
+
|
9
|
+
module YamlConverters
|
10
|
+
SEGMENT_KEY_DELIMITER = '.'
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe YamlConverters::SegmentToHashWriter do
|
4
|
+
subject { described_class.new }
|
5
|
+
let(:hash) { { 'key' => 'value' } }
|
6
|
+
|
7
|
+
it 'should return the same hash' do
|
8
|
+
expect do
|
9
|
+
subject.write(hash)
|
10
|
+
end.to change { subject.result }.from({}).to(hash)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe YamlConverters::SegmentsToYamlConverter do
|
4
|
+
subject { described_class.new(segments) }
|
5
|
+
|
6
|
+
context 'simple one key-value pair segment' do
|
7
|
+
let(:segments) { { 'key' => 'value' } }
|
8
|
+
|
9
|
+
its(:convert) { should eq("---\nkey: value\n") }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'simple two key-value pairs segments' do
|
13
|
+
let(:segments) { { 'key1' => 'value1', 'key2' => 'value2' } }
|
14
|
+
|
15
|
+
its(:convert) { should eq("---\nkey1: value1\nkey2: value2\n") }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'nested key-value pair segments' do
|
19
|
+
let(:segments) { { 'parent.child' => 'value' } }
|
20
|
+
|
21
|
+
its(:convert) { should eq("---\nparent:\n child: value\n") }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'deeply nested key-value pairs segments' do
|
25
|
+
let(:segments) do
|
26
|
+
{
|
27
|
+
'grandmother.mother.daughter' => 'olga',
|
28
|
+
'grandmother.mother.sister' => 'ania',
|
29
|
+
'grandfather' => 'eugeniusz'
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
its(:convert) { should eq(
|
34
|
+
"---\ngrandmother:\n mother:\n daughter: olga\n sister: ania\ngrandfather: eugeniusz\n"
|
35
|
+
) }
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe YamlConverters::YamlToSegmentsConverter do
|
4
|
+
subject { described_class.new(file_path) }
|
5
|
+
|
6
|
+
context 'simple one key-value pair file' do
|
7
|
+
let(:file_path) { 'spec/fixtures/one_key_value_pair.yml' }
|
8
|
+
|
9
|
+
its(:convert) { should eq({ 'key' => 'value' }) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'simple two key-value pairs file' do
|
13
|
+
let(:file_path) { 'spec/fixtures/two_key_value_pairs.yml' }
|
14
|
+
|
15
|
+
its(:convert) { should eq({ 'key1' => 'value1', 'key2' => 'value2' }) }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'nested key-value pair file' do
|
19
|
+
let(:file_path) { 'spec/fixtures/nested_key_value_pair.yml' }
|
20
|
+
|
21
|
+
its(:convert) { should eq({ 'parent.child' => 'value' }) }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'deeply ested key-value pairs file' do
|
25
|
+
let(:file_path) { 'spec/fixtures/deeply_nested_key_value_pairs.yml' }
|
26
|
+
|
27
|
+
its(:convert) do
|
28
|
+
should eq({
|
29
|
+
'grandmother.mother.daughter' => 'olga',
|
30
|
+
'grandmother.mother.sister' => 'ania',
|
31
|
+
'grandfather' => 'eugeniusz'
|
32
|
+
})
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe YamlConverters::YamlToStringWriter do
|
4
|
+
subject { described_class.new }
|
5
|
+
let(:yaml) { "---\nparent:\n child: value\n" }
|
6
|
+
|
7
|
+
it 'should return the same yaml' do
|
8
|
+
expect do
|
9
|
+
subject.write(yaml)
|
10
|
+
end.to change { subject.result }.from('').to(yaml)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yaml_converters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'yaml_converters'
|
8
|
+
gem.version = YamlConverters::VERSION
|
9
|
+
gem.authors = ['Tomek Wałkuski']
|
10
|
+
gem.email = ['ja@jestem.tw']
|
11
|
+
gem.description = %q{Convert YAML to segments and back}
|
12
|
+
gem.summary = %q{Convert YAML to segments and back}
|
13
|
+
gem.homepage = ''
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_dependency('activesupport')
|
21
|
+
gem.add_dependency('psych')
|
22
|
+
|
23
|
+
gem.add_development_dependency('rake')
|
24
|
+
gem.add_development_dependency('rspec')
|
25
|
+
gem.add_development_dependency('simplecov')
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml_converters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tomek Wałkuski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: psych
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: simplecov
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Convert YAML to segments and back
|
95
|
+
email:
|
96
|
+
- ja@jestem.tw
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/yaml_converters.rb
|
108
|
+
- lib/yaml_converters/segment_to_hash_writer.rb
|
109
|
+
- lib/yaml_converters/segments_to_yaml_converter.rb
|
110
|
+
- lib/yaml_converters/version.rb
|
111
|
+
- lib/yaml_converters/yaml_to_segments_converter.rb
|
112
|
+
- lib/yaml_converters/yaml_to_string_writer.rb
|
113
|
+
- spec/fixtures/deeply_nested_key_value_pairs.yml
|
114
|
+
- spec/fixtures/nested_key_value_pair.yml
|
115
|
+
- spec/fixtures/one_key_value_pair.yml
|
116
|
+
- spec/fixtures/two_key_value_pairs.yml
|
117
|
+
- spec/segment_to_hash_writer_spec.rb
|
118
|
+
- spec/segments_to_yaml_converter_spec.rb
|
119
|
+
- spec/spec_helper.rb
|
120
|
+
- spec/yaml_to_segments_converter_spec.rb
|
121
|
+
- spec/yaml_to_string_writer_spec.rb
|
122
|
+
- yaml_converters.gemspec
|
123
|
+
homepage: ''
|
124
|
+
licenses: []
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
segments:
|
136
|
+
- 0
|
137
|
+
hash: -872080065287608122
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ! '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
segments:
|
145
|
+
- 0
|
146
|
+
hash: -872080065287608122
|
147
|
+
requirements: []
|
148
|
+
rubyforge_project:
|
149
|
+
rubygems_version: 1.8.24
|
150
|
+
signing_key:
|
151
|
+
specification_version: 3
|
152
|
+
summary: Convert YAML to segments and back
|
153
|
+
test_files:
|
154
|
+
- spec/fixtures/deeply_nested_key_value_pairs.yml
|
155
|
+
- spec/fixtures/nested_key_value_pair.yml
|
156
|
+
- spec/fixtures/one_key_value_pair.yml
|
157
|
+
- spec/fixtures/two_key_value_pairs.yml
|
158
|
+
- spec/segment_to_hash_writer_spec.rb
|
159
|
+
- spec/segments_to_yaml_converter_spec.rb
|
160
|
+
- spec/spec_helper.rb
|
161
|
+
- spec/yaml_to_segments_converter_spec.rb
|
162
|
+
- spec/yaml_to_string_writer_spec.rb
|