hashie_walker 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +1 -0
- data/hashie_walker.gemspec +24 -0
- data/lib/hashie_walker.rb +32 -0
- data/spec/hashie_walker_spec.rb +63 -0
- data/spec/spec_helper.rb +3 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 08b599bf48ae7e150f9f92002a356cbc039bd2de
|
4
|
+
data.tar.gz: df6872c2ddabe66f11b5bcfa9c2cbe84b004f2c2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bda3d67a4b2b754603353eb87c7768ee73f977775d0fc66a7bc7482bd4492bdad2ba1c11c1a90ac2cb7682ee5b875861d9e183f9c60d4ccdb6ce7aed3816b312
|
7
|
+
data.tar.gz: 5ab9f8317e3666c54e72d62898d05b3a12019b55ed6eff66b8bd34b6eb62cc67ed9b2708d513d36ec48e52ba08de902ab99d45e113542112c49ea5acaa715bec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
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,48 @@
|
|
1
|
+
# HashieWalker
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/monterail/hashie_walker.png)](http://travis-ci.org/monterail/hashie_walker)
|
4
|
+
|
5
|
+
`Hash`-version of `Array#map` that is recursive and works on keys and values simultaneously.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'hashie_walker'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hashie_walker
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
`HashieWalker` walks through object and transforms keys and values along the way.
|
24
|
+
|
25
|
+
object = { "One" => 1, "Two" => 2 }
|
26
|
+
|
27
|
+
HashieWalker.walk(object) do |map|
|
28
|
+
map.key { |key| key.downcase }
|
29
|
+
map.value { |value| value * value }
|
30
|
+
end
|
31
|
+
# => { "one" => 1, "two" => 4 }
|
32
|
+
|
33
|
+
Object can be a `Hash`, `Array` or nested structure (e.g. an array of nested hashes with arrays as values). See specs for more examples.
|
34
|
+
|
35
|
+
If you don't want to change keys simply do not provide a respective mapping function.
|
36
|
+
|
37
|
+
HashieWalker.walk(object) do |map|
|
38
|
+
map.key { |key| key.downcase }
|
39
|
+
end
|
40
|
+
# => { "one" => 1, "two" => 2 }
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "hashie_walker"
|
7
|
+
spec.version = "0.1.0"
|
8
|
+
spec.authors = ["Michał Szajbe"]
|
9
|
+
spec.email = ["michal.szajbe@gmail.com"]
|
10
|
+
spec.description = %q{Hash-version of Array's map method that is recursive and works on keys and values simultaneously.}
|
11
|
+
spec.summary = %q{Handy hash traverse and transform tool}
|
12
|
+
spec.homepage = "http://github.com/monterail/hashie_walker"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "multiblock"
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "multiblock"
|
2
|
+
|
3
|
+
class HashieWalker
|
4
|
+
def initialize
|
5
|
+
@multiblock = Multiblock.new { |obj| obj }
|
6
|
+
yield(@multiblock) if block_given?
|
7
|
+
end
|
8
|
+
|
9
|
+
def walk(object)
|
10
|
+
case object
|
11
|
+
when Hash
|
12
|
+
object.inject({}) do |hash, (k, v)|
|
13
|
+
hash[@multiblock.call(:key, k)] = walk(v)
|
14
|
+
hash
|
15
|
+
end
|
16
|
+
when Array
|
17
|
+
object.inject([]) do |arr, element|
|
18
|
+
arr << walk(element)
|
19
|
+
arr
|
20
|
+
end
|
21
|
+
else
|
22
|
+
@multiblock.call(:value, object)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class << self
|
27
|
+
def walk(object, &blk)
|
28
|
+
return object unless block_given?
|
29
|
+
new(&blk).walk(object)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HashieWalker do
|
4
|
+
let(:hash) {
|
5
|
+
{ "One" => 1, "Two" => 2 }
|
6
|
+
}
|
7
|
+
|
8
|
+
let(:array_of_hashes) {
|
9
|
+
[ { "One" => 1, "Two" => 2 }, { "Three" => 3, "Four" => 4 } ]
|
10
|
+
}
|
11
|
+
|
12
|
+
let(:nested_hash) {
|
13
|
+
{ "Hash" => { "One" => 1, "Two" => 2 }, "Array" => [ { "One" => 1, "Two" => 2 }, { "Three" => 3, "Four" => 4 } ] }
|
14
|
+
}
|
15
|
+
|
16
|
+
let(:downcase_keys) {
|
17
|
+
lambda { |on|
|
18
|
+
on.key { |key| key.downcase }
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
let(:square_values) {
|
23
|
+
lambda { |on|
|
24
|
+
on.value { |value| value * value }
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
let(:downcase_keys_and_square_values) {
|
29
|
+
lambda { |on|
|
30
|
+
on.key { |key| key.downcase }
|
31
|
+
on.value { |value| value * value }
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
it "should return unchanged object when providing no block" do
|
36
|
+
HashieWalker.walk(hash).should == hash
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should walk through hash and transform keys" do
|
40
|
+
result = HashieWalker.walk(hash, &downcase_keys)
|
41
|
+
result.should == { "one" => 1, "two" => 2 }
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should walk through hash and transform values" do
|
45
|
+
result = HashieWalker.walk(hash, &square_values)
|
46
|
+
result.should == { "One" => 1, "Two" => 4 }
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should walk through hash and transform keys and values" do
|
50
|
+
result = HashieWalker.walk(hash, &downcase_keys_and_square_values)
|
51
|
+
result.should == { "one" => 1, "two" => 4 }
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should walk through array of hashes and transform keys and values" do
|
55
|
+
result = HashieWalker.walk(array_of_hashes, &downcase_keys_and_square_values)
|
56
|
+
result.should == [ { "one" => 1, "two" => 4 }, { "three" => 9, "four" => 16 } ]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should walk through nested hash and transform keys and values" do
|
60
|
+
result = HashieWalker.walk(nested_hash, &downcase_keys_and_square_values)
|
61
|
+
result.should == { "hash" => { "one" => 1, "two" => 4 }, "array" => [ { "one" => 1, "two" => 4 }, { "three" => 9, "four" => 16 } ] }
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashie_walker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michał Szajbe
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: multiblock
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Hash-version of Array's map method that is recursive and works on keys
|
70
|
+
and values simultaneously.
|
71
|
+
email:
|
72
|
+
- michal.szajbe@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- .rspec
|
79
|
+
- .travis.yml
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- hashie_walker.gemspec
|
85
|
+
- lib/hashie_walker.rb
|
86
|
+
- spec/hashie_walker_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: http://github.com/monterail/hashie_walker
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.0.0
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Handy hash traverse and transform tool
|
112
|
+
test_files:
|
113
|
+
- spec/hashie_walker_spec.rb
|
114
|
+
- spec/spec_helper.rb
|