yaml_convertor 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +166 -0
- data/Rakefile +2 -0
- data/lib/yaml_convertor/version.rb +3 -0
- data/lib/yaml_convertor.rb +45 -0
- data/test/devise.en.yml +11 -0
- data/test/test_convertor.rb +17 -0
- data/yaml_convertor.gemspec +22 -0
- metadata +127 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 nour
|
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,166 @@
|
|
1
|
+
# YamlConvertor
|
2
|
+
|
3
|
+
Ruby gem that converts a YAML hash structure to a simple hash consisting of key:value only, and vice versa
|
4
|
+
|
5
|
+
a YAML hash structure could look like this:
|
6
|
+
````
|
7
|
+
{"fruit"=> {
|
8
|
+
"lemon"=>"yellow",
|
9
|
+
"apple"=>{ "red_apples"=>"red" }
|
10
|
+
}
|
11
|
+
}
|
12
|
+
````
|
13
|
+
The resulting simple key:value hash looks like this:
|
14
|
+
````
|
15
|
+
{"fruit.lemon"=>"yellow", "fruit.apple.red_apples"=>"red"}
|
16
|
+
````
|
17
|
+
|
18
|
+
This works the other way around, too: if you had the simple hash; you can reconstruct the YAML hash structure.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
gem 'yaml_convertor'
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install yaml_convertor
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
There are two methods you can use:
|
37
|
+
- flattener converts the structured nested YAML hash to a simple key:value
|
38
|
+
|
39
|
+
````
|
40
|
+
YamlConvertor.flattener(yml_hash)
|
41
|
+
````
|
42
|
+
|
43
|
+
- builder rebuilds the simple hash to a nested YAML structure
|
44
|
+
|
45
|
+
````
|
46
|
+
YamlConvertor.builder(flat_hash)
|
47
|
+
````
|
48
|
+
|
49
|
+
## Example
|
50
|
+
|
51
|
+
Let's assume we have a YAML file called "file.yml":
|
52
|
+
|
53
|
+
````
|
54
|
+
en:
|
55
|
+
errors:
|
56
|
+
messages:
|
57
|
+
not_locked: "was not locked"
|
58
|
+
not_saved:
|
59
|
+
other: "other error"
|
60
|
+
devise:
|
61
|
+
failure:
|
62
|
+
already_authenticated: 'You are already signed in.'
|
63
|
+
sessions:
|
64
|
+
signed_in: 'Signed in successfully.'
|
65
|
+
````
|
66
|
+
|
67
|
+
We open this file using Psych (Ruby's YAML parser), and it will return a nested YAML hash structure
|
68
|
+
|
69
|
+
````ruby
|
70
|
+
yml_hash = Psych.load_file('file.yml')
|
71
|
+
````
|
72
|
+
|
73
|
+
yml_hash looks like this:
|
74
|
+
|
75
|
+
````
|
76
|
+
{"en"=>{
|
77
|
+
"errors"=>{
|
78
|
+
"messages"=>{
|
79
|
+
"not_locked"=>"was not locked",
|
80
|
+
"not_saved"=>{"other"=>"other error"}
|
81
|
+
}
|
82
|
+
},
|
83
|
+
"devise"=>{
|
84
|
+
"failure"=>{"already_authenticated"=>"You are already signed in."},
|
85
|
+
"sessions"=>{"signed_in"=>"Signed in successfully."}}
|
86
|
+
}
|
87
|
+
}
|
88
|
+
````
|
89
|
+
|
90
|
+
Now! If we use flattener:
|
91
|
+
|
92
|
+
````
|
93
|
+
flat_hash = YamlConvertor.flattener(yml_hash)
|
94
|
+
````
|
95
|
+
|
96
|
+
flat_hash will look like this:
|
97
|
+
|
98
|
+
````
|
99
|
+
{
|
100
|
+
"en.errors.messages.not_locked"=>"was not locked",
|
101
|
+
"en.errors.messages.not_saved.other"=>"other error",
|
102
|
+
"en.devise.failure.already_authenticated"=>"You are already signed in.",
|
103
|
+
"en.devise.sessions.signed_in"=>"Signed in successfully."
|
104
|
+
}
|
105
|
+
````
|
106
|
+
|
107
|
+
## Example 2
|
108
|
+
|
109
|
+
Now let's assume we have a flat hash and we want to rebuild it to a YAML file.
|
110
|
+
Here's our flat_hash from our example above:
|
111
|
+
|
112
|
+
````ruby
|
113
|
+
flat_hash = {"en.errors.messages.not_locked"=>"was not locked", "en.errors.messages.not_saved.other"=>"other error", "en.devise.failure.already_authenticated"=>"You are already signed in.", "en.devise.sessions.signed_in"=>"Signed in successfully."}
|
114
|
+
|
115
|
+
nested_hash = YamlConvertor.builder(flat_hash)
|
116
|
+
````
|
117
|
+
nested_hash is now:
|
118
|
+
|
119
|
+
````
|
120
|
+
{"en"=>{
|
121
|
+
"errors"=>{
|
122
|
+
"messages"=>{
|
123
|
+
"not_locked"=>"was not locked",
|
124
|
+
"not_saved"=>{"other"=>"other error"}
|
125
|
+
}
|
126
|
+
},
|
127
|
+
"devise"=>{
|
128
|
+
"failure"=>{"already_authenticated"=>"You are already signed in."},
|
129
|
+
"sessions"=>{"signed_in"=>"Signed in successfully."}}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
````
|
133
|
+
|
134
|
+
Dump it with Pysch..
|
135
|
+
|
136
|
+
````ruby
|
137
|
+
Psych.dump(nested_hash)
|
138
|
+
````
|
139
|
+
|
140
|
+
The output will be:
|
141
|
+
````
|
142
|
+
---
|
143
|
+
en:
|
144
|
+
errors:
|
145
|
+
messages:
|
146
|
+
not_locked: was not locked
|
147
|
+
not_saved:
|
148
|
+
other: other error
|
149
|
+
devise:
|
150
|
+
failure:
|
151
|
+
already_authenticated: You are already signed in.
|
152
|
+
sessions:
|
153
|
+
signed_in: Signed in successfully.
|
154
|
+
````
|
155
|
+
|
156
|
+
## Note
|
157
|
+
|
158
|
+
These methods only work properly if you have valid YAML structure. I didn't test that yet but I assume so!
|
159
|
+
|
160
|
+
## Contributing
|
161
|
+
|
162
|
+
1. Fork it
|
163
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
164
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
165
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
166
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'yaml_convertor/version'
|
2
|
+
require 'psych'
|
3
|
+
|
4
|
+
module YamlConvertor
|
5
|
+
def self.flattener(nested_hash)
|
6
|
+
raise "Argument must be a Hash" unless nested_hash.is_a?(Hash)
|
7
|
+
Convertor.new.flatten(nested_hash)
|
8
|
+
end
|
9
|
+
def self.builder(flat_hash)
|
10
|
+
raise "Argument must be a Hash" unless flat_hash.is_a?(Hash)
|
11
|
+
Convertor.new.build(flat_hash)
|
12
|
+
end
|
13
|
+
|
14
|
+
class Convertor
|
15
|
+
def flatten(nested_hash, keyname = '')
|
16
|
+
hash = {}
|
17
|
+
nested_hash.each_pair do |key, value|
|
18
|
+
if value.is_a?(Hash)
|
19
|
+
name = keyname.empty? ? "#{key}" : "#{keyname}.#{key}"
|
20
|
+
hash.merge!(flatten(value, name))
|
21
|
+
else
|
22
|
+
hash["#{keyname}.#{key}"] = value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
hash
|
26
|
+
end
|
27
|
+
def build(flat_hash)
|
28
|
+
# create a hash where every key's value is an empty hash
|
29
|
+
# hash[key] <- creates a key and its value will be {}
|
30
|
+
hash = Hash.new{ |h, k| h[k] = Hash.new(&h.default_proc) }
|
31
|
+
flat_hash.each_pair do |key, value|
|
32
|
+
tmp = hash
|
33
|
+
arr_keys = key.split('.')
|
34
|
+
arr_keys.each do |item|
|
35
|
+
if item.eql?(arr_keys.last)
|
36
|
+
tmp[item] = value
|
37
|
+
else
|
38
|
+
tmp = tmp[item] #creates key
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
hash
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/test/devise.en.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'yaml_convertor'
|
3
|
+
|
4
|
+
class TestConvertor < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_must_return_flattened_hash
|
7
|
+
nested_hash = {"k1"=>{"k2"=>"v", "k3"=>{"k4"=>"v", "k5"=>["v", "v"], "k6"=>{"k7"=>"v"}}, "k8"=>{"k9"=>{"k10"=>{"k11"=>"v", "k12"=>"v"}}}}}
|
8
|
+
flat_hash = {"k1.k2"=>"v", "k1.k3.k4"=>"v", "k1.k3.k5"=>["v", "v"], "k1.k3.k6.k7"=>"v", "k1.k8.k9.k10.k11"=>"v", "k1.k8.k9.k10.k12"=>"v"}
|
9
|
+
assert_equal flat_hash, YamlConvertor.flattener(nested_hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_must_return_nested_hash
|
13
|
+
flat_hash = {"k1.k2"=>"v", "k1.k3.k4"=>"v", "k1.k3.k5"=>["v", "v"], "k1.k3.k6.k7"=>"v", "k1.k8.k9.k10.k11"=>"v", "k1.k8.k9.k10.k12"=>"v"}
|
14
|
+
nested_hash = {"k1"=>{"k2"=>"v", "k3"=>{"k4"=>"v", "k5"=>["v", "v"], "k6"=>{"k7"=>"v"}}, "k8"=>{"k9"=>{"k10"=>{"k11"=>"v", "k12"=>"v"}}}}}
|
15
|
+
assert_equal nested_hash, YamlConvertor.builder(flat_hash)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/yaml_convertor/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Nour"]
|
6
|
+
gem.email = ["nour.fwh@gmail.com"]
|
7
|
+
gem.description = %q{yaml_convertor converts YAML structure to JSON }
|
8
|
+
gem.summary = %q{YAML to JSON convertor}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "yaml_convertor"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = YamlConvertor::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake'
|
19
|
+
gem.add_development_dependency 'rb-readline'
|
20
|
+
gem.add_development_dependency 'test-unit'
|
21
|
+
gem.add_runtime_dependency 'psych', '1.3.4'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml_convertor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nour
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
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: rb-readline
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
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: test-unit
|
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: psych
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - '='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.3.4
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - '='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.3.4
|
78
|
+
description: ! 'yaml_convertor converts YAML structure to JSON '
|
79
|
+
email:
|
80
|
+
- nour.fwh@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/yaml_convertor.rb
|
91
|
+
- lib/yaml_convertor/version.rb
|
92
|
+
- test/devise.en.yml
|
93
|
+
- test/test_convertor.rb
|
94
|
+
- yaml_convertor.gemspec
|
95
|
+
homepage: ''
|
96
|
+
licenses: []
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
hash: 412658065
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
hash: 412658065
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 1.8.24
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: YAML to JSON convertor
|
125
|
+
test_files:
|
126
|
+
- test/devise.en.yml
|
127
|
+
- test/test_convertor.rb
|