hmap 0.1.0

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/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # Copyright © 2011, José Pablo Fernández
2
+
3
+ source "http://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in hmap.gemspec
6
+ gemspec
@@ -0,0 +1,10 @@
1
+ # Copyright © 2011, José Pablo Fernández
2
+
3
+ require "bundler"
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require "rake/testtask"
7
+ Rake::TestTask.new do |t|
8
+ t.test_files = FileList["test/*.rb"]
9
+ t.verbose = true
10
+ end
@@ -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
@@ -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
@@ -0,0 +1,5 @@
1
+ # Copyright © 2011, José Pablo Fernández
2
+
3
+ module Hmap
4
+ VERSION = "0.1.0"
5
+ end
@@ -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