hashslice 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/CHANGES +5 -0
  2. data/README +5 -1
  3. data/lib/hashslice.rb +20 -5
  4. data/test/test_hashslice.rb +13 -1
  5. metadata +13 -4
data/CHANGES CHANGED
@@ -1,3 +1,8 @@
1
+ == 1.0.6 - 26-Aug-2009
2
+ * Added the Hash#slice alias for Hash#[].
3
+ * Added the Hash#hash_of method that returns a sub-hash.
4
+ * Added test-unit 2.x as a development dependency.
5
+
1
6
  == 1.0.5 - 30-Jul-2009
2
7
  * Several updates to the gemspec, including a change in the license from
3
8
  Artistic to Artistic 2.0.
data/README CHANGED
@@ -18,10 +18,14 @@
18
18
  hash['a', 'b'] = 7, 8
19
19
 
20
20
  hash # -> {'a' => 7, 'b' => 8, 'c' => 3}
21
+
22
+ # Sub hash
23
+ hash.hash_of('a', 'b') # -> {'a' => 1, 'b' => 2}
21
24
 
22
25
  = Overview
23
26
  This library modifies the Hash#[] and Hash#[]= methods so that they can
24
- handle list reference or assignment.
27
+ handle list reference or assignment. It also adds the Hash#hash_of method
28
+ that returns a hash slice.
25
29
 
26
30
  == Hash#[*keys]
27
31
  If more than one key is provided then an array is returned. Single
data/lib/hashslice.rb CHANGED
@@ -1,16 +1,16 @@
1
1
  class Hash
2
- alias :href :[]
3
- alias :hset :[]=
2
+ alias href []
3
+ alias hset []=
4
4
 
5
- # The version of this library
6
- VERSION_HASHSLICE = '1.0.5'
5
+ # The version of the hashslice library
6
+ VERSION_HASHSLICE = '1.0.6'
7
7
 
8
8
  # Retrieve a hash slice. If a single key is provided, returns a single
9
9
  # value. If multiple keys are provided, an array of values is returned.
10
10
  #
11
11
  # Examples:
12
12
  #
13
- # hash = {'a', 1, 'b', 2, 'c', 3}
13
+ # hash = {'a' => 1, 'b' => 2, 'c' => 3}
14
14
  # hash['a'] -> 1
15
15
  # hash['a', 'c'] -> [1, 3]
16
16
  #
@@ -22,6 +22,8 @@ class Hash
22
22
  end
23
23
  end
24
24
 
25
+ alias slice []
26
+
25
27
  # Hash slice assignment. You can assign a list of values to a list of keys
26
28
  # in a single operation on a one for one basis.
27
29
  #
@@ -47,4 +49,17 @@ class Hash
47
49
  args.each_index{ |i| hset(args[i], values[i]) }
48
50
  end
49
51
  end
52
+
53
+ # Returns a sub-hash of the current hash.
54
+ #
55
+ # Example:
56
+ #
57
+ # hash = {'a' => 1, 'b' => 2, 'c' => 3}
58
+ # hash.hash_of('a', 'b') -> {'a' => 1, 'b' => 2}
59
+ #
60
+ def hash_of(*args)
61
+ temp = {}
62
+ args.map{ |k| temp[k] = href(k) }
63
+ temp
64
+ end
50
65
  end
@@ -4,6 +4,9 @@
4
4
  # Test suite for the hashslice library. You should run these tests
5
5
  # via the 'rake test' task.
6
6
  #######################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
7
10
  require 'hashslice'
8
11
  require 'test/unit'
9
12
 
@@ -13,7 +16,7 @@ class TC_Hashslice < Test::Unit::TestCase
13
16
  end
14
17
 
15
18
  def test_version
16
- assert_equal('1.0.5', Hash::VERSION_HASHSLICE)
19
+ assert_equal('1.0.6', Hash::VERSION_HASHSLICE)
17
20
  end
18
21
 
19
22
  def test_get_slice_instance_method_basic
@@ -77,6 +80,15 @@ class TC_Hashslice < Test::Unit::TestCase
77
80
  assert_equal({'a' => 3, 'b' => 4}, @hash)
78
81
  end
79
82
 
83
+ def test_slice_alias
84
+ assert_true(Hash.instance_method(:slice) == Hash.instance_method(:[]))
85
+ end
86
+
87
+ def test_hash_of
88
+ assert_respond_to(@hash, :hash_of)
89
+ assert_equal({'a' => 1, 'b' => 2}, @hash.hash_of('a', 'b'))
90
+ end
91
+
80
92
  def teardown
81
93
  @hash = nil
82
94
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashslice
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
@@ -10,10 +10,19 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-07-29 00:00:00 -06:00
13
+ date: 2009-08-26 00:00:00 -06:00
14
14
  default_executable:
15
- dependencies: []
16
-
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: test-unit
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.0.3
25
+ version:
17
26
  description: " The hashslice library adds builtin hash slicing to Ruby's Hash class.\n This lets you reference, or assign to, multiple hash keys simultaneously\n via the Hash#[] and Hash#[]= methods, respectively.\n"
18
27
  email: djberg96@gmail.com
19
28
  executables: []