hash_set_operators 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.tar.gz.sig +3 -0
- data/History.txt +4 -0
- data/Manifest.txt +6 -0
- data/README.txt +50 -0
- data/Rakefile +18 -0
- data/lib/hash_set_operators.rb +46 -0
- data/test/test_hash_set_operators.rb +37 -0
- metadata +84 -0
- metadata.gz.sig +2 -0
data.tar.gz.sig
ADDED
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
HashSetOperators
|
2
|
+
by Mike Judge
|
3
|
+
rubysideshow.com
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
* Adds +, -, and & methods to Hashes
|
8
|
+
|
9
|
+
== SYNOPSIS:
|
10
|
+
|
11
|
+
require 'rubygems'
|
12
|
+
require 'hash_set_operators'
|
13
|
+
|
14
|
+
{:controller => :user, :action => :edit} + {:action => :show, :id => 1}
|
15
|
+
# => {:controller => :user, :action => :show, :id => 1}
|
16
|
+
|
17
|
+
{:controller => :user, :action => :edit} - {:action => :show, :id => 1}
|
18
|
+
# => {:controller => :user}
|
19
|
+
|
20
|
+
{:controller => :user, :action => :edit} & {:action => :show, :id => 1}
|
21
|
+
# => {:action => :edit}
|
22
|
+
|
23
|
+
== INSTALL:
|
24
|
+
|
25
|
+
* sudo gem install hash_set_operators
|
26
|
+
|
27
|
+
== LICENSE:
|
28
|
+
|
29
|
+
(The MIT License)
|
30
|
+
|
31
|
+
Copyright (c) 2008 Mike Judge
|
32
|
+
|
33
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
34
|
+
a copy of this software and associated documentation files (the
|
35
|
+
'Software'), to deal in the Software without restriction, including
|
36
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
37
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
38
|
+
permit persons to whom the Software is furnished to do so, subject to
|
39
|
+
the following conditions:
|
40
|
+
|
41
|
+
The above copyright notice and this permission notice shall be
|
42
|
+
included in all copies or substantial portions of the Software.
|
43
|
+
|
44
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
45
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
46
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
47
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
48
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
49
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
50
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/hash_set_operators.rb'
|
6
|
+
|
7
|
+
Hoe.new('hash_set_operators', HashSetOperators::VERSION) do |p|
|
8
|
+
p.name = 'hash_set_operators'
|
9
|
+
p.rubyforge_name = 'rubysideshow'
|
10
|
+
p.author = 'Mike Judge'
|
11
|
+
p.email = 'mikelovesrobots@gmail.com'
|
12
|
+
p.summary = 'Adds set operators to hashes'
|
13
|
+
p.description = p.paragraphs_of('README.txt', 1..6).join("\n\n")
|
14
|
+
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
15
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
16
|
+
end
|
17
|
+
|
18
|
+
# vim: syntax=Ruby
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class HashSetOperators
|
2
|
+
VERSION = '0.1.0'
|
3
|
+
end
|
4
|
+
|
5
|
+
class Hash
|
6
|
+
# Returns a new hash containing both the contents of the first hash
|
7
|
+
# and the second. In cases where the same key is present in both
|
8
|
+
# hashes, the values from the second are retained and not the
|
9
|
+
# first.
|
10
|
+
#
|
11
|
+
# It is essentially an alias for Hash#merge
|
12
|
+
#
|
13
|
+
# {:a => 1, :b => 2} + {:c => 3}
|
14
|
+
# # => {:a => 1, :b => 2, :c => 3}
|
15
|
+
#
|
16
|
+
# {:a => 1, :b => 2} + {:a => 3}
|
17
|
+
# # => {:a => 3, :b => 2}
|
18
|
+
#
|
19
|
+
alias :+ :merge
|
20
|
+
|
21
|
+
# Hash difference returns a new hash that is a copy of the original
|
22
|
+
# removing any keys that also appear in the second hash.
|
23
|
+
#
|
24
|
+
# {:a => 1, :b => 2} - {:b => 3}
|
25
|
+
# # => {:a => 1}
|
26
|
+
#
|
27
|
+
def -(hash)
|
28
|
+
hash.keys.each do |key|
|
29
|
+
delete key
|
30
|
+
end
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
# Returns a new hash that is a copy of the original, removing any
|
35
|
+
# keys that do not appear in the second hash.
|
36
|
+
#
|
37
|
+
# {:a => 1, :b => 2} & {:b => 3}
|
38
|
+
# # => {:b => 2}
|
39
|
+
#
|
40
|
+
def &(hash)
|
41
|
+
(keys - hash.keys).each do |key|
|
42
|
+
delete key
|
43
|
+
end
|
44
|
+
self
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'lib/hash_set_operators'
|
3
|
+
|
4
|
+
describe Hash do
|
5
|
+
describe "when +'ing" do
|
6
|
+
it "another hash should behave like a.merge(b)" do
|
7
|
+
result = {:controller => :user, :action => :edit} + {:action => :show, :id => 1}
|
8
|
+
result.should == {:controller => :user, :action => :show, :id => 1}
|
9
|
+
end
|
10
|
+
|
11
|
+
it "an array, should throw an exception" do
|
12
|
+
lambda { {:controller => :user, :action => :edit} + [:action] }.should raise_error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "when -'ing" do
|
17
|
+
it "another hash should behave like the keys from the second hash were ripped out of the first" do
|
18
|
+
result = {:controller => :user, :action => :edit} - {:action => :show, :id => 1}
|
19
|
+
result.should == {:controller => :user}
|
20
|
+
end
|
21
|
+
|
22
|
+
it "an array, should throw an exception" do
|
23
|
+
lambda { {:controller => :user, :action => :edit} - [:action, :id] }.should raise_error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "when &'ing" do
|
28
|
+
it "another hash should retain only the key/value pairs from the first hash that share the same keys as the second hash." do
|
29
|
+
result = {:controller => :user, :action => :edit} & {:action => :show, :id => 1}
|
30
|
+
result.should == {:action => :edit}
|
31
|
+
end
|
32
|
+
|
33
|
+
it "an array, should throw an exception" do
|
34
|
+
lambda { {:controller => :user, :action => :edit} & [:action, :id] }.should raise_error
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: hash_set_operators
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2008-04-28 00:00:00 -07:00
|
8
|
+
summary: Adds set operators to hashes
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: mikelovesrobots@gmail.com
|
12
|
+
homepage: " by Mike Judge"
|
13
|
+
rubyforge_project: rubysideshow
|
14
|
+
description: "== DESCRIPTION: * Adds +, -, and & methods to Hashes == SYNOPSIS: require 'rubygems' require 'hash_set_operators' {:controller => :user, :action => :edit} + {:action => :show, :id => 1} # => {:controller => :user, :action => :show, :id => 1} {:controller => :user, :action => :edit} - {:action => :show, :id => 1} # => {:controller => :user} {:controller => :user, :action => :edit} & {:action => :show, :id => 1} # => {:action => :edit} == INSTALL:"
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
- |
|
29
|
+
-----BEGIN CERTIFICATE-----
|
30
|
+
MIIDQDCCAiigAwIBAgIBADANBgkqhkiG9w0BAQUFADBGMRgwFgYDVQQDDA9taWtl
|
31
|
+
bG92ZXNyb2JvdHMxFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixk
|
32
|
+
ARkWA2NvbTAeFw0wODA0MTgwNTI4MjFaFw0wOTA0MTgwNTI4MjFaMEYxGDAWBgNV
|
33
|
+
BAMMD21pa2Vsb3Zlc3JvYm90czEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYK
|
34
|
+
CZImiZPyLGQBGRYDY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
|
35
|
+
3VGB7/ek1pD/ZegUUH4jVotvO1z5G+vWZYwesXb8zdRLCHIXYNWHlPDcQKmB9vz3
|
36
|
+
ekn6BSrYEKR6Q9Ko6a2oWiadf+iz8WlD/FF2xwgoa2b7X3qMI0dowXWrHmCf19s8
|
37
|
+
+bynDIgsol3MbWJW+T5vVRjlRoN9sGCa+S8se7VNQ4pwMbddkvzcw62orbiJv9CK
|
38
|
+
HQAQnxU6v9x/wyRkBwD5blrhVHblA2YH+ZYc6lzMKHUGFrZ5E4rwG4UMnLUohg7I
|
39
|
+
y9KSfsy8QVl5RiOFDWAeCKxnRsCx4l2p8GetkEOCBFr9mKEJza3YBRQB6rWhy04H
|
40
|
+
5N02HWQFzUmLwtfBMe6p1QIDAQABozkwNzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIE
|
41
|
+
sDAdBgNVHQ4EFgQUekQzuTUI0pjbK++DouVgmNdjjvowDQYJKoZIhvcNAQEFBQAD
|
42
|
+
ggEBADfVkguKy9D2h+vvIAPmukjkZ+t0fuByLtamklmJ6HKmART5IH0oSfpJPNm2
|
43
|
+
EfGDvGwQ96W1ni1YfbRnP+cj3AOu03DfRWK52y1KYxIu4VcmZRKk8mCgJBJ3IVZf
|
44
|
+
7GQkJ7gOjgj55DBfgdvwNQ4yXw+ACUTHAFkqwT05jAVse0swCiY9ruY60wUx+RTl
|
45
|
+
SivrkNPaARhL9KM3t70UdaqAIVrMqJva19xhLussfyvLeMnybSw2zE11N+9V5EPn
|
46
|
+
h2CoJJZOXHdgmDGaqY23v663f2FahHIRWtyCABJiU3YaUyFsd7zEtfjlCfufMvcp
|
47
|
+
aHG2UbipyooL1m/rJWEPIllGmbk=
|
48
|
+
-----END CERTIFICATE-----
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
authors:
|
52
|
+
- Mike Judge
|
53
|
+
files:
|
54
|
+
- History.txt
|
55
|
+
- Manifest.txt
|
56
|
+
- README.txt
|
57
|
+
- Rakefile
|
58
|
+
- lib/hash_set_operators.rb
|
59
|
+
- test/test_hash_set_operators.rb
|
60
|
+
test_files:
|
61
|
+
- test/test_hash_set_operators.rb
|
62
|
+
rdoc_options:
|
63
|
+
- --main
|
64
|
+
- README.txt
|
65
|
+
extra_rdoc_files:
|
66
|
+
- History.txt
|
67
|
+
- Manifest.txt
|
68
|
+
- README.txt
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
dependencies:
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: hoe
|
78
|
+
version_requirement:
|
79
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 1.5.1
|
84
|
+
version:
|
metadata.gz.sig
ADDED