hash_flattener 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hash_flattener.rb +9 -0
- data/lib/hash_flattener/hash_flattener.rb +39 -0
- metadata +58 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
module HashFlattener
|
2
|
+
class HashFlattener
|
3
|
+
|
4
|
+
def initialize(hash, options={})
|
5
|
+
@hash = hash
|
6
|
+
@delimiter = options[:delimiter] || '.'
|
7
|
+
end
|
8
|
+
|
9
|
+
def flatten
|
10
|
+
flatten_enumerable @hash
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def flatten_enumerable(enumerable, parent_prefix=nil)
|
16
|
+
res = {}
|
17
|
+
|
18
|
+
enumerable.each_with_index do |elem, i|
|
19
|
+
if elem.is_a? Array
|
20
|
+
k, v = elem
|
21
|
+
else
|
22
|
+
k, v = i, elem
|
23
|
+
end
|
24
|
+
|
25
|
+
k = k.to_s
|
26
|
+
key = parent_prefix ? parent_prefix + @delimiter + k : k
|
27
|
+
|
28
|
+
if v.is_a? Enumerable
|
29
|
+
res.merge! flatten_enumerable(v, key)
|
30
|
+
else
|
31
|
+
res[key] = v
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
res
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_flattener
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthew Riley
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70363807617900 !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: *70363807617900
|
25
|
+
description:
|
26
|
+
email: matthew-github@matthewriley.name
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- lib/hash_flattener/hash_flattener.rb
|
32
|
+
- lib/hash_flattener.rb
|
33
|
+
homepage: http://rubygems.org/gems/hash_flattener
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.15
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Hash Flattener is a Ruby Gem that recursively flattens a Hash using breadcrumbs
|
57
|
+
as keys.
|
58
|
+
test_files: []
|