hmap 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -0
- data/Rakefile +10 -0
- data/hmap.gemspec +23 -0
- data/lib/hmap.rb +19 -0
- data/lib/hmap/version.rb +5 -0
- data/test/hmap_test.rb +35 -0
- metadata +73 -0
data/Gemfile
ADDED
data/Rakefile
ADDED
data/hmap.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# Copyright © 2011, José Pablo Fernández
|
3
|
+
|
4
|
+
$:.push File.expand_path("../lib", __FILE__)
|
5
|
+
require "hmap/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "hmap"
|
9
|
+
s.version = Hmap::VERSION
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.authors = ["J. Pablo Fernández"]
|
12
|
+
s.email = ["pupeno@pupeno.com"]
|
13
|
+
s.homepage = ""
|
14
|
+
s.summary = %q{A map for hashes that returns a hash}
|
15
|
+
s.description = %q{A map for hashes that returs a hash}
|
16
|
+
|
17
|
+
s.rubyforge_project = "hmap"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n").grep(/^[^\.]/)
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
data/lib/hmap.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Copyright © 2011, José Pablo Fernández
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
def hmap(*attrs, &block)
|
5
|
+
map(*attrs, &block).inject({}) do |new_hash, pair|
|
6
|
+
if pair.is_a? Hash
|
7
|
+
new_hash.merge(pair)
|
8
|
+
elsif pair.is_a? Array
|
9
|
+
if pair.length != 2
|
10
|
+
raise "When using hmap, when you return arrays, they must have two and only two elements; #{pair} doesn't"
|
11
|
+
end
|
12
|
+
new_hash[pair[0]] = pair[1]
|
13
|
+
new_hash
|
14
|
+
else
|
15
|
+
raise "When using hmap you need to return arrays or hashes, #{pair} isn't either."
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/hmap/version.rb
ADDED
data/test/hmap_test.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Copyright © 2011, José Pablo Fernández
|
2
|
+
|
3
|
+
require "hmap.rb"
|
4
|
+
require "test/unit"
|
5
|
+
|
6
|
+
class HashTest < Test::Unit::TestCase
|
7
|
+
def test_empty
|
8
|
+
assert_equal({}, {}.hmap { |a, b| [a, b] })
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_raises_when_totally_invalid
|
12
|
+
assert_raises RuntimeError do
|
13
|
+
{:a => :b}.hmap { |a, _| a }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_raises_when_invalid_array
|
18
|
+
assert_raises RuntimeError do
|
19
|
+
{:a => :b}.hmap { |a, _| [a] }
|
20
|
+
end
|
21
|
+
assert_raises RuntimeError do
|
22
|
+
{:a => :b}.hmap { |a, b| [a, b, b] }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_identity_with_hash
|
27
|
+
hash = {:a => :b, :c => :d, :e => :f}
|
28
|
+
assert_equal(hash, hash.hmap { |a, b| {a => b} })
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_identity_with_array
|
32
|
+
hash = {:a => :b, :c => :d, :e => :f}
|
33
|
+
assert_equal(hash, hash.hmap { |a, b| [a, b] })
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hmap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "J. Pablo Fern\xC3\xA1ndez"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-19 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A map for hashes that returs a hash
|
23
|
+
email:
|
24
|
+
- pupeno@pupeno.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- Gemfile
|
33
|
+
- Rakefile
|
34
|
+
- hmap.gemspec
|
35
|
+
- lib/hmap.rb
|
36
|
+
- lib/hmap/version.rb
|
37
|
+
- test/hmap_test.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: ""
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: hmap
|
68
|
+
rubygems_version: 1.5.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: A map for hashes that returns a hash
|
72
|
+
test_files:
|
73
|
+
- test/hmap_test.rb
|