hash_converter 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/.rspec +2 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +23 -0
- data/LICENSE +20 -0
- data/README.md +61 -0
- data/Rakefile +2 -0
- data/hash_mapper.gemspec +23 -0
- data/lib/core_ext/hash.rb +44 -0
- data/lib/hash_converter.rb +91 -0
- data/spec/lib/core_ext/hash_spec.rb +219 -0
- data/spec/lib/hash_mapper_spec.rb +261 -0
- data/spec/spec_helper.rb +6 -0
- metadata +145 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
hash_mapper (0.0.1)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: http://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
activesupport (3.0.0)
|
|
10
|
+
mocha (0.9.8)
|
|
11
|
+
rake
|
|
12
|
+
rake (0.8.7)
|
|
13
|
+
rspec-core (2.0.0)
|
|
14
|
+
|
|
15
|
+
PLATFORMS
|
|
16
|
+
ruby
|
|
17
|
+
|
|
18
|
+
DEPENDENCIES
|
|
19
|
+
activesupport (>= 3.0.0)
|
|
20
|
+
bundler (>= 1.0.0)
|
|
21
|
+
hash_mapper!
|
|
22
|
+
mocha (>= 0.9.8)
|
|
23
|
+
rspec-core (>= 2.0.0)
|
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2010 Łukasz Strzałkowski, Peder Linder
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Hash Mapper
|
|
2
|
+
===========
|
|
3
|
+
|
|
4
|
+
Ruby DSL for parsing hashes.
|
|
5
|
+
|
|
6
|
+
Installation
|
|
7
|
+
------------
|
|
8
|
+
|
|
9
|
+
hash_mapper is hosted on gemcutter.
|
|
10
|
+
|
|
11
|
+
If you are using bundle (and you should):
|
|
12
|
+
|
|
13
|
+
gem 'hash_mapper'
|
|
14
|
+
|
|
15
|
+
Run `bundle` and start hacking.
|
|
16
|
+
|
|
17
|
+
Usage
|
|
18
|
+
-----
|
|
19
|
+
|
|
20
|
+
Simple example:
|
|
21
|
+
|
|
22
|
+
require "hash_converter"
|
|
23
|
+
hash = {
|
|
24
|
+
:person => {
|
|
25
|
+
:first_name => "Jonh",
|
|
26
|
+
:last_name => "Doe",
|
|
27
|
+
:address => {
|
|
28
|
+
:street => "Hoża",
|
|
29
|
+
:city => "Warsaw",
|
|
30
|
+
:zip => "12345"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
hash_converted = HashConverter.convert(hash) do
|
|
36
|
+
path "person" do
|
|
37
|
+
map "{first_name} {last_name}", "name"
|
|
38
|
+
path "address" do
|
|
39
|
+
map "{street} {city} {zip}", "address"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
pp hash_converted
|
|
45
|
+
|
|
46
|
+
For more features, checkout tests. There are cool things like: #get, #set methods etc.
|
|
47
|
+
|
|
48
|
+
Contributing
|
|
49
|
+
------------
|
|
50
|
+
|
|
51
|
+
* Fork the project.
|
|
52
|
+
* Create branch for your changes
|
|
53
|
+
* Write tests
|
|
54
|
+
* Write change / fix
|
|
55
|
+
* Commit
|
|
56
|
+
* Pull request
|
|
57
|
+
|
|
58
|
+
Author
|
|
59
|
+
-------
|
|
60
|
+
|
|
61
|
+
- Łukasz Strzałkowski (<http://github.com/strzalek>)
|
data/Rakefile
ADDED
data/hash_mapper.gemspec
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
Gem::Specification.new do |s|
|
|
3
|
+
s.name = "hash_converter"
|
|
4
|
+
s.version = "0.0.2"
|
|
5
|
+
s.platform = Gem::Platform::RUBY
|
|
6
|
+
s.authors = ["Łukasz Strzałkowski"]
|
|
7
|
+
s.email = ["lukasz.strzalkowski@gmail.com"]
|
|
8
|
+
s.homepage = "http://rubygems.org/gems/hash_converter"
|
|
9
|
+
s.summary = "Parsing hashes made simple"
|
|
10
|
+
s.description = "Ruby DSL for parsing hashes"
|
|
11
|
+
|
|
12
|
+
s.required_rubygems_version = ">= 1.3.6"
|
|
13
|
+
s.rubyforge_project = "hash_converter"
|
|
14
|
+
|
|
15
|
+
s.add_dependency "activesupport", ">= 3.0.0"
|
|
16
|
+
|
|
17
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
|
18
|
+
s.add_development_dependency "rspec-core", ">= 2.0.0"
|
|
19
|
+
s.add_development_dependency "mocha", ">= 0.9.8"
|
|
20
|
+
|
|
21
|
+
s.files = `git ls-files`.split("\n")
|
|
22
|
+
s.require_path = 'lib'
|
|
23
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require "active_support/core_ext/hash/deep_merge"
|
|
2
|
+
|
|
3
|
+
class Hash
|
|
4
|
+
@@namespace = []
|
|
5
|
+
|
|
6
|
+
def namespace_flatten(separator = ".")
|
|
7
|
+
hash = {}
|
|
8
|
+
self.each do |key, value|
|
|
9
|
+
key = key.to_s
|
|
10
|
+
if value.is_a?(Hash)
|
|
11
|
+
@@namespace << key
|
|
12
|
+
hash.merge! value.namespace_flatten(separator)
|
|
13
|
+
@@namespace.pop
|
|
14
|
+
else
|
|
15
|
+
key = @@namespace + [key]
|
|
16
|
+
hash[key.join(separator)] = value
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
hash
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def namespace_unflatten(separator = ".")
|
|
23
|
+
hash = {}
|
|
24
|
+
self.each do |key, value|
|
|
25
|
+
hash.deep_merge! self.class.nested_value(value, key.split(separator))
|
|
26
|
+
end
|
|
27
|
+
hash
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.nested_value(x, p)
|
|
31
|
+
hash = { p.pop => x }
|
|
32
|
+
p.reverse_each do |element|
|
|
33
|
+
hash = { element => hash }
|
|
34
|
+
end
|
|
35
|
+
hash
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def recursive_symbolize_keys!
|
|
39
|
+
symbolize_keys!
|
|
40
|
+
values.each { |h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
|
|
41
|
+
values.select { |v| v.is_a?(Array) }.flatten.each { |h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
|
|
42
|
+
self
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require "date"
|
|
2
|
+
require "time"
|
|
3
|
+
require "active_support/core_ext/hash/keys"
|
|
4
|
+
require "core_ext/hash"
|
|
5
|
+
|
|
6
|
+
class HashConverter
|
|
7
|
+
SEPARATOR = "."
|
|
8
|
+
KEY_REGEX = /\{([^}]*)\}/
|
|
9
|
+
|
|
10
|
+
Types = [String, Integer, Float, Time, Date, DateTime]
|
|
11
|
+
|
|
12
|
+
def self.convert(hash = {}, &block)
|
|
13
|
+
@hash = hash.namespace_flatten(SEPARATOR)
|
|
14
|
+
@converted = {}
|
|
15
|
+
@path = []
|
|
16
|
+
|
|
17
|
+
instance_eval(&block)
|
|
18
|
+
|
|
19
|
+
@converted.namespace_unflatten.recursive_symbolize_keys!
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def self.path(path, &block)
|
|
25
|
+
@path.push(path)
|
|
26
|
+
instance_eval(&block)
|
|
27
|
+
@path.pop
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.map(input, output, type_or_symbol = nil, &block)
|
|
31
|
+
value = if input =~ KEY_REGEX
|
|
32
|
+
input.gsub(KEY_REGEX) { |var| self.get(var.gsub(/\{|\}/,"")) }
|
|
33
|
+
else
|
|
34
|
+
self.get(input)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
value = if type_or_symbol.is_a?(Symbol)
|
|
38
|
+
value.send(type_or_symbol)
|
|
39
|
+
elsif type_or_symbol.is_a?(Class)
|
|
40
|
+
typecast(value, type_or_symbol)
|
|
41
|
+
else
|
|
42
|
+
value
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
if block_given?
|
|
46
|
+
value = yield value
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
@converted[output.to_s] = value
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.typecast(value, type)
|
|
53
|
+
return value if value.nil?
|
|
54
|
+
|
|
55
|
+
if Types.include?(type)
|
|
56
|
+
case type.to_s
|
|
57
|
+
when "String"
|
|
58
|
+
value.to_s
|
|
59
|
+
when "Integer"
|
|
60
|
+
value.to_i
|
|
61
|
+
when "Float"
|
|
62
|
+
value.to_f
|
|
63
|
+
when "Time"
|
|
64
|
+
Time.parse(value)
|
|
65
|
+
when "Date"
|
|
66
|
+
Date.parse(value)
|
|
67
|
+
when "DateTime"
|
|
68
|
+
DateTime.parse(value)
|
|
69
|
+
else
|
|
70
|
+
value
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.namespaced_key(key)
|
|
76
|
+
(@path + [key]).join(SEPARATOR).to_s
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def self.get(*keys)
|
|
80
|
+
values = []
|
|
81
|
+
keys.each do |key|
|
|
82
|
+
key = namespaced_key(key)
|
|
83
|
+
values << @hash[key]
|
|
84
|
+
end
|
|
85
|
+
values.length == 1 ? values.first : values
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def self.set(key, value)
|
|
89
|
+
@converted[key] = value
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Hash do
|
|
4
|
+
describe "#namespace_flatten" do
|
|
5
|
+
before do
|
|
6
|
+
@hash = {
|
|
7
|
+
:foobar => "a",
|
|
8
|
+
:foo => {
|
|
9
|
+
:bar => {
|
|
10
|
+
:text => "foobar"
|
|
11
|
+
},
|
|
12
|
+
:a => [1,2,3]
|
|
13
|
+
},
|
|
14
|
+
:b => "b"
|
|
15
|
+
}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "should parse properly various hashes" do
|
|
19
|
+
{
|
|
20
|
+
:foo => {
|
|
21
|
+
:bar => {
|
|
22
|
+
:text => "foobar"
|
|
23
|
+
},
|
|
24
|
+
:test => {
|
|
25
|
+
:text => "tralalala"
|
|
26
|
+
},
|
|
27
|
+
:footest => {
|
|
28
|
+
:text => "asd"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}.namespace_flatten.should == {
|
|
32
|
+
"foo.bar.text" => "foobar",
|
|
33
|
+
"foo.test.text" => "tralalala",
|
|
34
|
+
"foo.footest.text" => "asd"
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
{
|
|
38
|
+
:foo => {
|
|
39
|
+
:bar => {
|
|
40
|
+
:text => "foobar"
|
|
41
|
+
},
|
|
42
|
+
:test => {
|
|
43
|
+
:text => "tralalala",
|
|
44
|
+
:text2 => "test"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}.namespace_flatten.should == {
|
|
48
|
+
"foo.bar.text" => "foobar",
|
|
49
|
+
"foo.test.text" => "tralalala",
|
|
50
|
+
"foo.test.text2" => "test"
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
{
|
|
54
|
+
:foo => {
|
|
55
|
+
:bar => "bar",
|
|
56
|
+
:foo => "foo",
|
|
57
|
+
:foobar => {
|
|
58
|
+
:bar => "bar",
|
|
59
|
+
:foo => "foo"
|
|
60
|
+
},
|
|
61
|
+
:barfoo => {
|
|
62
|
+
:bar => "bar",
|
|
63
|
+
:foo => "foo"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}.namespace_flatten.should == {
|
|
67
|
+
"foo.bar" => "bar",
|
|
68
|
+
"foo.foo" => "foo",
|
|
69
|
+
"foo.foobar.bar" => "bar",
|
|
70
|
+
"foo.foobar.foo" => "foo",
|
|
71
|
+
"foo.barfoo.bar" => "bar",
|
|
72
|
+
"foo.barfoo.foo" => "foo"
|
|
73
|
+
}
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "should return flatten hash with namespaced keys" do
|
|
77
|
+
result = {
|
|
78
|
+
"foobar" => "a",
|
|
79
|
+
"foo.bar.text" => "foobar",
|
|
80
|
+
"foo.a" => [1,2,3],
|
|
81
|
+
"b" => "b"
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@hash.namespace_flatten.should == result
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "should flatten with custom separator" do
|
|
88
|
+
result = {
|
|
89
|
+
"foobar" => "a",
|
|
90
|
+
"foo/bar/text" => "foobar",
|
|
91
|
+
"foo/a" => [1,2,3],
|
|
92
|
+
"b" => "b"
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@hash.namespace_flatten("/").should == result
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
describe "#namespace_unflatten" do
|
|
100
|
+
before do
|
|
101
|
+
@hash = {
|
|
102
|
+
"foobar" => "a",
|
|
103
|
+
"foo" => {
|
|
104
|
+
"bar" => {
|
|
105
|
+
"text" => "foobar"
|
|
106
|
+
},
|
|
107
|
+
"a" => [1,2,3]
|
|
108
|
+
},
|
|
109
|
+
"b" => "b"
|
|
110
|
+
}
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "should properly convert various hashes" do
|
|
114
|
+
{
|
|
115
|
+
"foo.bar.text" => "foobar",
|
|
116
|
+
"foo.test.text" => "tralalala",
|
|
117
|
+
"foo.footest.text" => "asd"
|
|
118
|
+
}.namespace_unflatten.should == {
|
|
119
|
+
"foo" => {
|
|
120
|
+
"bar" => {
|
|
121
|
+
"text" => "foobar"
|
|
122
|
+
},
|
|
123
|
+
"test" => {
|
|
124
|
+
"text" => "tralalala"
|
|
125
|
+
},
|
|
126
|
+
"footest" => {
|
|
127
|
+
"text" => "asd"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
{
|
|
133
|
+
"foo.bar.text" => "foobar",
|
|
134
|
+
"foo.test.text" => "tralalala",
|
|
135
|
+
"foo.test.text2" => "test"
|
|
136
|
+
}.namespace_unflatten.should == {
|
|
137
|
+
"foo" => {
|
|
138
|
+
"bar" => {
|
|
139
|
+
"text" => "foobar"
|
|
140
|
+
},
|
|
141
|
+
"test" => {
|
|
142
|
+
"text" => "tralalala",
|
|
143
|
+
"text2" => "test"
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
{
|
|
149
|
+
"foo.bar" => "bar",
|
|
150
|
+
"foo.foo" => "foo",
|
|
151
|
+
"foo.foobar.bar" => "bar",
|
|
152
|
+
"foo.foobar.foo" => "foo",
|
|
153
|
+
"foo.barfoo.bar" => "bar",
|
|
154
|
+
"foo.barfoo.foo" => "foo"
|
|
155
|
+
}.namespace_unflatten.should == {
|
|
156
|
+
"foo" => {
|
|
157
|
+
"bar" => "bar",
|
|
158
|
+
"foo" => "foo",
|
|
159
|
+
"foobar" => {
|
|
160
|
+
"bar" => "bar",
|
|
161
|
+
"foo" => "foo"
|
|
162
|
+
},
|
|
163
|
+
"barfoo" => {
|
|
164
|
+
"bar" => "bar",
|
|
165
|
+
"foo" => "foo"
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it "should return flatten hash with namespaced keys" do
|
|
172
|
+
{
|
|
173
|
+
"foobar" => "a",
|
|
174
|
+
"foo.bar.text" => "foobar",
|
|
175
|
+
"foo.a" => [1,2,3],
|
|
176
|
+
"b" => "b"
|
|
177
|
+
}.namespace_unflatten.should == @hash
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
it "should flatten with custom separator" do
|
|
181
|
+
{
|
|
182
|
+
"foobar" => "a",
|
|
183
|
+
"foo/bar/text" => "foobar",
|
|
184
|
+
"foo/a" => [1,2,3],
|
|
185
|
+
"b" => "b"
|
|
186
|
+
}.namespace_unflatten("/").should == @hash
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
describe "#nested_value" do
|
|
191
|
+
it "should generated nested hash" do
|
|
192
|
+
Hash.nested_value("value", ["foo", "bar", "foobar"]).should == {
|
|
193
|
+
"foo" => {
|
|
194
|
+
"bar" => {
|
|
195
|
+
"foobar" => "value"
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
describe "#def recursive_symbolize_keys!" do
|
|
203
|
+
it "should symbolize keys in nested hashes" do
|
|
204
|
+
{
|
|
205
|
+
"foo" => {
|
|
206
|
+
"bar" => {
|
|
207
|
+
"foobar" => "foobar"
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}.recursive_symbolize_keys!.should == {
|
|
211
|
+
:foo => {
|
|
212
|
+
:bar => {
|
|
213
|
+
:foobar => "foobar"
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe HashConverter do
|
|
4
|
+
describe "DSL" do
|
|
5
|
+
describe "maping" do
|
|
6
|
+
it "should map from flat hash" do
|
|
7
|
+
HashConverter.convert({ :key1 => "val1", :key2 => "val2", :key3 => "val3" }) do
|
|
8
|
+
map "key1", "a"
|
|
9
|
+
map "key2", "b"
|
|
10
|
+
map "key3", "c"
|
|
11
|
+
end.should == { :a => "val1", :b => "val2", :c => "val3" }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "should map keys that not exists as nil value" do
|
|
15
|
+
HashConverter.convert({ :key => "value", :nilkey => nil }) {
|
|
16
|
+
map "key", "foo"
|
|
17
|
+
map "foobar", "foobar"
|
|
18
|
+
map "nilkey", "nil"
|
|
19
|
+
}.should == {
|
|
20
|
+
:foo => "value",
|
|
21
|
+
:foobar => nil,
|
|
22
|
+
:nil => nil
|
|
23
|
+
}
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "should map keys given as symbol" do
|
|
27
|
+
h = { :key => "value" }
|
|
28
|
+
hh = { :new_key => "value" }
|
|
29
|
+
|
|
30
|
+
HashConverter.convert(h) do
|
|
31
|
+
map :key, :new_key
|
|
32
|
+
end.should == hh
|
|
33
|
+
|
|
34
|
+
HashConverter.convert(h) do
|
|
35
|
+
map "key", :new_key
|
|
36
|
+
end.should == hh
|
|
37
|
+
|
|
38
|
+
HashConverter.convert(h) do
|
|
39
|
+
map :key, "new_key"
|
|
40
|
+
end.should == hh
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
describe "nesting paths" do
|
|
45
|
+
before do
|
|
46
|
+
@hash = {
|
|
47
|
+
:foo => {
|
|
48
|
+
:bar => {
|
|
49
|
+
:text => "foobar"
|
|
50
|
+
},
|
|
51
|
+
:test => {
|
|
52
|
+
:text => "text"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "should take namespaced path name" do
|
|
59
|
+
HashConverter.convert(@hash) do
|
|
60
|
+
path "foo.bar" do
|
|
61
|
+
map :text, :foo
|
|
62
|
+
end
|
|
63
|
+
end.should == { :foo => "foobar" }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "should allow take symbol as argument" do
|
|
67
|
+
HashConverter.convert(@hash) do
|
|
68
|
+
path :foo do
|
|
69
|
+
path :bar do
|
|
70
|
+
map :text, :barfoo
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
path :foo do
|
|
74
|
+
path :test do
|
|
75
|
+
map :text, :testfoo
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end.should == { :barfoo => "foobar", :testfoo => "text" }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "should allow take string as argument" do
|
|
82
|
+
HashConverter.convert(@hash) do
|
|
83
|
+
path "foo" do
|
|
84
|
+
path "bar" do
|
|
85
|
+
map :text, :barfoo
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
path "foo" do
|
|
90
|
+
path "test" do
|
|
91
|
+
map :text, :testfoo
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end.should == { :barfoo => "foobar", :testfoo => "text" }
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
describe "combined input keys" do
|
|
99
|
+
it "should join with given white chars" do
|
|
100
|
+
HashConverter.convert({ :foo => "foo", :bar => "bar" }){
|
|
101
|
+
map "{foo}\s{bar}", :foobar
|
|
102
|
+
}.should == {
|
|
103
|
+
:foobar => "foo\sbar"
|
|
104
|
+
}
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it "should join namespaecd keys with given white chars" do
|
|
108
|
+
HashConverter.convert({ :ns => { :foo => "foo", :bar => "bar" }}){
|
|
109
|
+
map "{ns.foo}\s{ns.bar}", :foobar
|
|
110
|
+
}.should == {
|
|
111
|
+
:foobar => "foo\sbar"
|
|
112
|
+
}
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
describe "value conversion" do
|
|
117
|
+
it "should change value passed in block" do
|
|
118
|
+
h = {
|
|
119
|
+
:test => {
|
|
120
|
+
:valid => "test"
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
HashConverter.convert(h) do
|
|
125
|
+
path "test" do
|
|
126
|
+
map "valid", "foobar" do |value|
|
|
127
|
+
"value_from_block_#{value}"
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end.should == { :foobar => "value_from_block_test" }
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
describe "typecasting" do
|
|
135
|
+
it "should typecast to given type" do
|
|
136
|
+
HashConverter.convert({ :foobar => "123" }){
|
|
137
|
+
map :foobar, :foo, Integer
|
|
138
|
+
}.should == {
|
|
139
|
+
:foo => 123
|
|
140
|
+
}
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it "should invoke method given by symbol" do
|
|
144
|
+
HashConverter.convert({ :foobar => "123" }){
|
|
145
|
+
map :foobar, :foo, :to_i
|
|
146
|
+
}.should == {
|
|
147
|
+
:foo => 123
|
|
148
|
+
}
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
describe "#get" do
|
|
153
|
+
before do
|
|
154
|
+
@hash = { :foo => { :bar => "foobar" }, :bar => "foo"}
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
it "should return value from given hash" do
|
|
158
|
+
HashConverter.convert(@hash) {
|
|
159
|
+
@@var = get "foo.bar"
|
|
160
|
+
}
|
|
161
|
+
@@var.should == "foobar"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
it "should return array of values" do
|
|
165
|
+
HashConverter.convert(@hash) {
|
|
166
|
+
@@var = get("foo.bar", "bar")
|
|
167
|
+
}
|
|
168
|
+
@@var.should == ["foobar", "foo"]
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it "should works properly when nested into path" do
|
|
172
|
+
HashConverter.convert(@hash) {
|
|
173
|
+
path "foo" do
|
|
174
|
+
@@var = get "bar"
|
|
175
|
+
end
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
@@var.should == "foobar"
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
describe "#set" do
|
|
183
|
+
it "should set value for key" do
|
|
184
|
+
HashConverter.convert {
|
|
185
|
+
set "foo", "bar"
|
|
186
|
+
}.should == {
|
|
187
|
+
:foo => "bar"
|
|
188
|
+
}
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
it "should set value for namespaced key" do
|
|
192
|
+
HashConverter.convert {
|
|
193
|
+
set "foo.bar", "foobar"
|
|
194
|
+
}.should == {
|
|
195
|
+
:foo => {
|
|
196
|
+
:bar => "foobar"
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
it "should ignore path" do
|
|
202
|
+
HashConverter.convert do
|
|
203
|
+
path "ns" do
|
|
204
|
+
set "foo.bar", "foobar"
|
|
205
|
+
end
|
|
206
|
+
end.should == {
|
|
207
|
+
:foo => {
|
|
208
|
+
:bar => "foobar"
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
describe "get and set combined" do
|
|
215
|
+
it "should set value from #get" do
|
|
216
|
+
HashConverter.convert({ "foo" => { "bar" => "foobar" }}) {
|
|
217
|
+
set "test", get("foo.bar")
|
|
218
|
+
}.should == {
|
|
219
|
+
:test => "foobar"
|
|
220
|
+
}
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
describe "#typecast" do
|
|
226
|
+
describe "casting to type" do
|
|
227
|
+
|
|
228
|
+
it "should handle nils properly" do
|
|
229
|
+
lambda {
|
|
230
|
+
HashConverter.convert do
|
|
231
|
+
map "foo", "foobar", Date
|
|
232
|
+
end
|
|
233
|
+
}.should_not raise_error
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
it "string" do
|
|
237
|
+
HashConverter.typecast(123, String).should == "123"
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it "integer" do
|
|
241
|
+
HashConverter.typecast("123", Integer).should == 123
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
it "float" do
|
|
245
|
+
HashConverter.typecast("123.45", Float).should == 123.45
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
it "time" do
|
|
249
|
+
HashConverter.typecast("16:30", Time).should == Time.parse("16:30")
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
it "date" do
|
|
253
|
+
HashConverter.typecast("01-02-2003", Date).should == Date.parse("01-02-2003")
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
it "datetime" do
|
|
257
|
+
HashConverter.typecast("01-02-2003 15:30", DateTime).should == DateTime.parse("01-02-2003 15:30")
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: hash_converter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 27
|
|
5
|
+
prerelease: false
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 0
|
|
9
|
+
- 2
|
|
10
|
+
version: 0.0.2
|
|
11
|
+
platform: ruby
|
|
12
|
+
authors:
|
|
13
|
+
- "\xC5\x81ukasz Strza\xC5\x82kowski"
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2010-10-12 00:00:00 +02:00
|
|
19
|
+
default_executable:
|
|
20
|
+
dependencies:
|
|
21
|
+
- !ruby/object:Gem::Dependency
|
|
22
|
+
name: activesupport
|
|
23
|
+
prerelease: false
|
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
hash: 7
|
|
30
|
+
segments:
|
|
31
|
+
- 3
|
|
32
|
+
- 0
|
|
33
|
+
- 0
|
|
34
|
+
version: 3.0.0
|
|
35
|
+
type: :runtime
|
|
36
|
+
version_requirements: *id001
|
|
37
|
+
- !ruby/object:Gem::Dependency
|
|
38
|
+
name: bundler
|
|
39
|
+
prerelease: false
|
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
hash: 23
|
|
46
|
+
segments:
|
|
47
|
+
- 1
|
|
48
|
+
- 0
|
|
49
|
+
- 0
|
|
50
|
+
version: 1.0.0
|
|
51
|
+
type: :development
|
|
52
|
+
version_requirements: *id002
|
|
53
|
+
- !ruby/object:Gem::Dependency
|
|
54
|
+
name: rspec-core
|
|
55
|
+
prerelease: false
|
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
hash: 15
|
|
62
|
+
segments:
|
|
63
|
+
- 2
|
|
64
|
+
- 0
|
|
65
|
+
- 0
|
|
66
|
+
version: 2.0.0
|
|
67
|
+
type: :development
|
|
68
|
+
version_requirements: *id003
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: mocha
|
|
71
|
+
prerelease: false
|
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
|
73
|
+
none: false
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
hash: 43
|
|
78
|
+
segments:
|
|
79
|
+
- 0
|
|
80
|
+
- 9
|
|
81
|
+
- 8
|
|
82
|
+
version: 0.9.8
|
|
83
|
+
type: :development
|
|
84
|
+
version_requirements: *id004
|
|
85
|
+
description: Ruby DSL for parsing hashes
|
|
86
|
+
email:
|
|
87
|
+
- lukasz.strzalkowski@gmail.com
|
|
88
|
+
executables: []
|
|
89
|
+
|
|
90
|
+
extensions: []
|
|
91
|
+
|
|
92
|
+
extra_rdoc_files: []
|
|
93
|
+
|
|
94
|
+
files:
|
|
95
|
+
- .gitignore
|
|
96
|
+
- .rspec
|
|
97
|
+
- Gemfile
|
|
98
|
+
- Gemfile.lock
|
|
99
|
+
- LICENSE
|
|
100
|
+
- README.md
|
|
101
|
+
- Rakefile
|
|
102
|
+
- hash_mapper.gemspec
|
|
103
|
+
- lib/core_ext/hash.rb
|
|
104
|
+
- lib/hash_converter.rb
|
|
105
|
+
- spec/lib/core_ext/hash_spec.rb
|
|
106
|
+
- spec/lib/hash_mapper_spec.rb
|
|
107
|
+
- spec/spec_helper.rb
|
|
108
|
+
has_rdoc: true
|
|
109
|
+
homepage: http://rubygems.org/gems/hash_converter
|
|
110
|
+
licenses: []
|
|
111
|
+
|
|
112
|
+
post_install_message:
|
|
113
|
+
rdoc_options: []
|
|
114
|
+
|
|
115
|
+
require_paths:
|
|
116
|
+
- lib
|
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
|
+
none: false
|
|
119
|
+
requirements:
|
|
120
|
+
- - ">="
|
|
121
|
+
- !ruby/object:Gem::Version
|
|
122
|
+
hash: 3
|
|
123
|
+
segments:
|
|
124
|
+
- 0
|
|
125
|
+
version: "0"
|
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
|
+
none: false
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
hash: 23
|
|
132
|
+
segments:
|
|
133
|
+
- 1
|
|
134
|
+
- 3
|
|
135
|
+
- 6
|
|
136
|
+
version: 1.3.6
|
|
137
|
+
requirements: []
|
|
138
|
+
|
|
139
|
+
rubyforge_project: hash_converter
|
|
140
|
+
rubygems_version: 1.3.7
|
|
141
|
+
signing_key:
|
|
142
|
+
specification_version: 3
|
|
143
|
+
summary: Parsing hashes made simple
|
|
144
|
+
test_files: []
|
|
145
|
+
|