hashslice 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +9 -0
- data/MANIFEST +7 -0
- data/README +46 -0
- data/Rakefile +16 -0
- data/lib/hashslice.rb +50 -0
- data/test/test_hashslice.rb +79 -0
- metadata +60 -0
data/CHANGES
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
= 1.0.4 - 22-May-2008
|
2
|
+
* Library now managed by Daniel Berger with the kind permission of
|
3
|
+
the original author, Michael Granger.
|
4
|
+
* Added gemspec, README, MANIFEST, and CHANGES files.
|
5
|
+
* Added a Rakefile with tasks for testing and installation.
|
6
|
+
* Replaced the test suite with one based on Test::Unit.
|
7
|
+
|
8
|
+
= 1.0.3 - 9-Jun-2004
|
9
|
+
* Last release by Michael Granger
|
data/MANIFEST
ADDED
data/README
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
= Description
|
2
|
+
Slicing for Ruby hashes.
|
3
|
+
|
4
|
+
= Installation
|
5
|
+
rake test (optional)
|
6
|
+
rake install
|
7
|
+
|
8
|
+
= Synopsis
|
9
|
+
require 'hashslice'
|
10
|
+
|
11
|
+
hash = {'a' => 1, 'b' => 2, 'c' => 3}
|
12
|
+
|
13
|
+
# Slice reference
|
14
|
+
hash['a', 'b'] # -> [1, 2]
|
15
|
+
hash['a'] # -> 1
|
16
|
+
|
17
|
+
# Slice assignment
|
18
|
+
hash['a', 'b'] = 7, 8
|
19
|
+
|
20
|
+
hash # -> {'a' => 7, 'b' => 8, 'c' => 3}
|
21
|
+
|
22
|
+
= Overview
|
23
|
+
This library modifies the Hash#[] and Hash#[]= methods so that they can
|
24
|
+
handle list reference or assignment.
|
25
|
+
|
26
|
+
== Hash#[*keys]
|
27
|
+
If more than one key is provided then an array is returned. Single
|
28
|
+
keys work as before, i.e. they return a single value.
|
29
|
+
|
30
|
+
== Hash#[*keys]=(*values)
|
31
|
+
The values on the right are assigned to the keys on left on a one for one
|
32
|
+
If there are more values than keys, the extra values are dropped.
|
33
|
+
|
34
|
+
= Copyright
|
35
|
+
Copyright (c) 2001-2008, The FaerieMUD Consortium and Daniel J. Berger.
|
36
|
+
All rights reserved.
|
37
|
+
|
38
|
+
= License
|
39
|
+
This module is free software. You may use, modify, and/or redistribute this
|
40
|
+
software under the terms of the Perl Artistic License.
|
41
|
+
|
42
|
+
See http://language.perl.com/misc/Artistic.html
|
43
|
+
|
44
|
+
= Authors
|
45
|
+
Michael Granger (original author)
|
46
|
+
Daniel Berger (current maintainer)
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/clean'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
desc "Install the pure hashslice library (non-gem)"
|
6
|
+
task :install do
|
7
|
+
cp 'lib/hashslice.rb', Config::CONFIG['sitelibdir']
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
desc "Run the tests"
|
12
|
+
Rake::TestTask.new('test') do |t|
|
13
|
+
t.warning = true
|
14
|
+
t.libs << 'lib'
|
15
|
+
t.test_files = FileList['test/*.rb']
|
16
|
+
end
|
data/lib/hashslice.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
class Hash
|
2
|
+
alias :href :[]
|
3
|
+
alias :hset :[]=
|
4
|
+
|
5
|
+
# The version of this library
|
6
|
+
VERSION_HASHSLICE = '1.0.4'
|
7
|
+
|
8
|
+
# Retrieve a hash slice. If a single key is provided, returns a single
|
9
|
+
# value. If multiple keys are provided, an array of values is returned.
|
10
|
+
#
|
11
|
+
# Examples:
|
12
|
+
#
|
13
|
+
# hash = {'a', 1, 'b', 2, 'c', 3}
|
14
|
+
# hash['a'] -> 1
|
15
|
+
# hash['a', 'c'] -> [1, 3]
|
16
|
+
#
|
17
|
+
def [](*args)
|
18
|
+
if args.length == 1
|
19
|
+
href(args[0])
|
20
|
+
else
|
21
|
+
args.map{ |k| href(k) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Hash slice assignment. You can assign a list of values to a list of keys
|
26
|
+
# in a single operation on a one for one basis.
|
27
|
+
#
|
28
|
+
# If the number of keys exceeds the number of values, the remaining keys
|
29
|
+
# are assigned a value of nil.
|
30
|
+
#
|
31
|
+
# If the number of values exceeds the number of keys, the extra values are
|
32
|
+
# dropped.
|
33
|
+
#
|
34
|
+
# Examples:
|
35
|
+
#
|
36
|
+
# hash['a'] = 1, 2 -> {a => [1, 2]}
|
37
|
+
# hash['a', 'b'] = 3, 4 -> {a => 3, b => 4}
|
38
|
+
# hash['a', 'b'] = 5 -> {a => 5, b => nil}
|
39
|
+
# hash['a', 'b'] = 3, 4, 5 -> {a => 3, b => 4}
|
40
|
+
#
|
41
|
+
def []=(*args)
|
42
|
+
if args.length <= 2
|
43
|
+
hset(*args)
|
44
|
+
else
|
45
|
+
values = args.pop # Last arg is the value. The rest are keys.
|
46
|
+
values = [values] unless values.is_a?(Array)
|
47
|
+
args.each_index{ |i| hset(args[i], values[i]) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
#######################################################################
|
2
|
+
# test_hashslice.rb
|
3
|
+
#
|
4
|
+
# Test suite for the hashslice library. You should run these tests
|
5
|
+
# via the 'rake test' task.
|
6
|
+
#######################################################################
|
7
|
+
require 'hashslice'
|
8
|
+
require 'test/unit'
|
9
|
+
|
10
|
+
class TC_Hashslice < Test::Unit::TestCase
|
11
|
+
def setup
|
12
|
+
@hash = {'a', 1, 'b', 2}
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_get_slice_instance_method_basic
|
16
|
+
assert_respond_to(@hash, :[])
|
17
|
+
assert_nothing_raised{ @hash['a'] }
|
18
|
+
assert_nothing_raised{ @hash['a', 'b'] }
|
19
|
+
assert_kind_of(Integer, @hash['a'])
|
20
|
+
assert_kind_of(Array, @hash['a', 'b'])
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_get_slice_instance_method
|
24
|
+
assert_equal(1, @hash['a'])
|
25
|
+
assert_equal([1, 2], @hash['a', 'b'])
|
26
|
+
assert_equal([1, 2, nil], @hash['a', 'b', 'c'])
|
27
|
+
assert_nil(@hash['bogus'])
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_get_slice_duplicate_keys
|
31
|
+
assert_equal([1, 1], @hash['a', 'a'])
|
32
|
+
assert_equal([1, 2, 1, 2], @hash['a', 'b', 'a', 'b'])
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_set_slice_instance_method_basic
|
36
|
+
assert_respond_to(@hash, :[]=)
|
37
|
+
assert_nothing_raised{ @hash['a'] = 3 }
|
38
|
+
assert_nothing_raised{ @hash['a', 'b'] = 3 }
|
39
|
+
assert_nothing_raised{ @hash['a', 'b'] = 3, 4 }
|
40
|
+
assert_kind_of(Fixnum, @hash['a'] = 3)
|
41
|
+
assert_kind_of(Fixnum, @hash['a', 'b'] = 3)
|
42
|
+
assert_kind_of(Fixnum, @hash['a', 'b'] = 3, 4)
|
43
|
+
end
|
44
|
+
|
45
|
+
# hash[key] = value
|
46
|
+
def test_set_slice_instance_method_single_key_and_value
|
47
|
+
assert_nothing_raised{ @hash['a'] = 3 }
|
48
|
+
assert_nothing_raised{ @hash['b'] = [1, 2] }
|
49
|
+
assert_equal({'a' => 3, 'b' => [1, 2]}, @hash)
|
50
|
+
end
|
51
|
+
|
52
|
+
# hash[key1, key2] = value
|
53
|
+
def test_set_slice_instance_method_multiple_keys_single_value
|
54
|
+
assert_nothing_raised{ @hash['a', 'b'] = 3 }
|
55
|
+
assert_equal({'a' => 3, 'b' => nil}, @hash)
|
56
|
+
end
|
57
|
+
|
58
|
+
# hash[key1, key2] = value1, value2
|
59
|
+
def test_set_slice_instance_method_multiple_keys_multiple_values
|
60
|
+
assert_nothing_raised{ @hash['a', 'b'] = 3, 4 }
|
61
|
+
assert_equal({'a' => 3, 'b' => 4}, @hash)
|
62
|
+
end
|
63
|
+
|
64
|
+
# hash[key] = value1, value2
|
65
|
+
def test_set_slice_instance_method_single_key_multiple_values
|
66
|
+
assert_nothing_raised{ @hash['a'] = 3, 4 }
|
67
|
+
assert_equal({'a' => [3, 4], 'b' => 2}, @hash)
|
68
|
+
end
|
69
|
+
|
70
|
+
# hash[key1, key2] = value1, value2, value3
|
71
|
+
def test_set_slice_instance_method_multiple_keys_odd_values
|
72
|
+
assert_nothing_raised{ @hash['a', 'b'] = 3, 4, 5 }
|
73
|
+
assert_equal({'a' => 3, 'b' => 4}, @hash)
|
74
|
+
end
|
75
|
+
|
76
|
+
def teardown
|
77
|
+
@hash = nil
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashslice
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel J. Berger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-05-23 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: djberg96@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- CHANGES
|
25
|
+
- MANIFEST
|
26
|
+
files:
|
27
|
+
- lib/hashslice.rb
|
28
|
+
- CHANGES
|
29
|
+
- MANIFEST
|
30
|
+
- README
|
31
|
+
- Rakefile
|
32
|
+
- test/test_hashslice.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://www.rubyforge.org/projects/shards
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project: shards
|
55
|
+
rubygems_version: 1.1.1
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: An easy way to implement the facade pattern in your class
|
59
|
+
test_files:
|
60
|
+
- test/test_hashslice.rb
|