flat_keys 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/Gemfile +2 -0
- data/LICENSE +22 -0
- data/README.md +51 -0
- data/Rakefile +9 -0
- data/flat_keys.gemspec +20 -0
- data/lib/flat_keys/version.rb +3 -0
- data/lib/flat_keys.rb +41 -0
- data/test/flat_keys_test.rb +33 -0
- data/test/test_helper.rb +3 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Firebase.co
|
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,51 @@
|
|
1
|
+
# flat_keys
|
2
|
+
|
3
|
+
Encodes and Decodes key-value hashes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'flat_keys'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install flat_keys
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
|
23
|
+
require 'flat_keys'
|
24
|
+
|
25
|
+
{'name'=>{'fname'=>'John', 'lname'=>'Doe'}}.flat_keys
|
26
|
+
#=> {'name.fname'=>'John', 'name.lname'=>'Doe'}
|
27
|
+
|
28
|
+
{'name.fname'=>'John', 'name.lname'=>'Doe'}.unflat_keys
|
29
|
+
#=> {'name'=>{'fname'=>'John', 'lname'=>'Doe'}}
|
30
|
+
```
|
31
|
+
|
32
|
+
Both `flat_keys` and `unflat_keys` can be used with custom separators.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
|
36
|
+
{'name'=>{'fname'=>'John', 'lname'=>'Doe'}}.flat_keys('$')
|
37
|
+
#=> {'name$fname'=>'John', 'name$lname'=>'Doe'}
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
## Tests
|
42
|
+
|
43
|
+
rake
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/flat_keys.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'flat_keys/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "flat_keys"
|
8
|
+
gem.version = FlatKeys::VERSION
|
9
|
+
gem.authors = ["Firebaseco"]
|
10
|
+
gem.email = ["hello@firebase.co"]
|
11
|
+
gem.description = %q{Encodes and Decodes key-value hashes.}
|
12
|
+
gem.summary = %q{Encodes and Decodes key-value hashes.}
|
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
|
+
gem.add_development_dependency 'rake'
|
20
|
+
end
|
data/lib/flat_keys.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative "flat_keys/version"
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
|
5
|
+
def visit_flat_keys(hash, prefix, separator)
|
6
|
+
self.each do |k, v|
|
7
|
+
key = "#{prefix}#{k}"
|
8
|
+
if v.is_a?(Hash)
|
9
|
+
v.visit_flat_keys(hash, "#{key}#{separator}", separator)
|
10
|
+
else
|
11
|
+
hash[key] = v
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def flat_keys(separator = ".")
|
17
|
+
hash = {}
|
18
|
+
visit_flat_keys(hash, "", separator)
|
19
|
+
return hash
|
20
|
+
end
|
21
|
+
|
22
|
+
def unflat_keys(separator)
|
23
|
+
hash = {}
|
24
|
+
self.each do |k, v|
|
25
|
+
names = k.split(separator)
|
26
|
+
last_index = names.count - 1
|
27
|
+
local_hash = hash
|
28
|
+
names.each_with_index do |name, index|
|
29
|
+
last = index == last_index
|
30
|
+
if last
|
31
|
+
local_hash[name] = v
|
32
|
+
else
|
33
|
+
local_hash = local_hash[name] || (local_hash[name] = Hash.new)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
hash
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
describe "flat_keys" do
|
4
|
+
|
5
|
+
it "should return an empty hash when an empty hash is given" do
|
6
|
+
Hash.new.flat_keys.must_be_empty
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should flat_keys all root keys" do
|
10
|
+
{'a'=>'Letter A', 'b'=>'Letter B'}.flat_keys.must_equal({'a'=>'Letter A', 'b'=>'Letter B'})
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should flat_keys all nested keys" do
|
14
|
+
{'a'=>'Letter A', 'name'=>{'fname'=>'John', 'lname'=>'Doe'}}.flat_keys('$').must_equal({'a'=>'Letter A', 'name$fname'=>'John', 'name$lname'=>'Doe'})
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should flat_keys with default separator " do
|
18
|
+
{'a'=>'Letter A', 'name'=>{'fname'=>'John'}}.flat_keys('.').must_equal({'a'=>'Letter A', 'name.fname'=>'John'})
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "unflat_keys" do
|
24
|
+
|
25
|
+
it "should unflat all nested keys" do
|
26
|
+
{'a'=>'Letter A', 'name$fname'=>'John', 'name$lname'=>'Doe'}.unflat_keys('$').must_equal({'a'=>'Letter A', 'name'=>{'fname'=>'John', 'lname'=>'Doe'}})
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should unflat with default separator " do
|
30
|
+
{'a'=>'Letter A', 'name.fname'=>'John'}.unflat_keys('.').must_equal({'a'=>'Letter A', 'name'=>{'fname'=>'John'}} )
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flat_keys
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Firebaseco
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-16 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
|
+
description: Encodes and Decodes key-value hashes.
|
31
|
+
email:
|
32
|
+
- hello@firebase.co
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- flat_keys.gemspec
|
43
|
+
- lib/flat_keys.rb
|
44
|
+
- lib/flat_keys/version.rb
|
45
|
+
- test/flat_keys_test.rb
|
46
|
+
- test/test_helper.rb
|
47
|
+
homepage: ''
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.24
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Encodes and Decodes key-value hashes.
|
71
|
+
test_files:
|
72
|
+
- test/flat_keys_test.rb
|
73
|
+
- test/test_helper.rb
|
74
|
+
has_rdoc:
|