symboltable 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ **SymbolTable** is a handy little Ruby class that was conceived from the union
2
+ of `Hash` and `Symbol`. SymbolTable directly extends `Hash`, but it stores all
3
+ keys internally as symbols. Any key that cannot be converted to a `Symbol` is
4
+ not valid.
5
+
6
+ While this may seem restrictive, it does have the nice side effect of making
7
+ all keys slightly more memorable and usable. For example, values may be set
8
+ and retrieved using any key that resolves to the same symbol.
9
+
10
+ ```ruby
11
+ h = Hash.new
12
+ h[:a] = 1
13
+ h[:a] # => 1
14
+ h['a'] # => nil
15
+ h.a # => NoMethodError: undefined method `a' for {}:Hash
16
+
17
+ require 'symboltable'
18
+
19
+ t = SymbolTable.new
20
+ t[:a] = 1
21
+ t[:a] # => 1
22
+ t['a'] # => 1
23
+ t.a # => 1
24
+ t.b = 2 # => 2
25
+ ```
26
+
27
+ Also, because `SymbolTable` subclasses `Hash`, you get all the nice methods of
28
+ `Hash` for free.
29
+
30
+ ```ruby
31
+ t.keys # => [:a, :b]
32
+ t.values # => [1, 2]
33
+
34
+ t.merge!('c' => 3, :d => 4)
35
+ t.keys # => [:a, :b, :c, :d]
36
+ t.values # => [1, 2, 3, 4]
37
+
38
+ t.each do |k, v|
39
+ puts "#{k} = #{v}"
40
+ end
41
+ ```
42
+
43
+ You get the idea. This kind of data structure is mainly useful for large
44
+ configuration objects and the like, where you want to store a bunch of data and
45
+ you don't want to have to remember later how you stored it.
46
+
47
+ ## Why?
48
+
49
+ Mainly just for fun, and because I sometimes miss the automatic and ubiquitous
50
+ type conversion you get in PHP.
51
+
52
+ ## Installation
53
+
54
+ Using [RubyGems](http://rubygems.org):
55
+
56
+ $ sudo gem install symboltable
57
+
58
+ From a local copy:
59
+
60
+ $ git clone git://github.com/mjijackson/symboltable.git
61
+ $ cd symboltable
62
+ $ rake package && sudo rake install
63
+
64
+ ## License
65
+
66
+ Copyright 2012 Michael Jackson
67
+
68
+ Permission is hereby granted, free of charge, to any person obtaining a copy
69
+ of this software and associated documentation files (the "Software"), to deal
70
+ in the Software without restriction, including without limitation the rights
71
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
72
+ copies of the Software, and to permit persons to whom the Software is
73
+ furnished to do so, subject to the following conditions:
74
+
75
+ The above copyright notice and this permission notice shall be included in
76
+ all copies or substantial portions of the Software.
77
+
78
+ The software is provided "as is", without warranty of any kind, express or
79
+ implied, including but not limited to the warranties of merchantability,
80
+ fitness for a particular purpose and non-infringement. In no event shall the
81
+ authors or copyright holders be liable for any claim, damages or other
82
+ liability, whether in an action of contract, tort or otherwise, arising from,
83
+ out of or in connection with the software or the use or other dealings in
84
+ the software.
@@ -1,6 +1,3 @@
1
- # A Symbols-only Hash for Ruby.
2
- #
3
- # http://mjijackson.com/symboltable
4
1
  class SymbolTable < Hash
5
2
  def initialize(hash=nil)
6
3
  super()
@@ -25,7 +22,7 @@ class SymbolTable < Hash
25
22
  # Sets the value of the given +key+ to +val+.
26
23
  def store(key, val)
27
24
  if valid_key?(key)
28
- val = self.class.new(val) if Hash === val && !(self.class === val)
25
+ val = self.class.new(val) if Hash === val && !(SymbolTable === val)
29
26
  super(key.to_sym, val)
30
27
  end
31
28
  end
@@ -56,4 +53,9 @@ class SymbolTable < Hash
56
53
  super(sym, *args)
57
54
  end
58
55
  end
56
+
57
+ # Returns a Hash
58
+ def to_hash
59
+ Hash[self]
60
+ end
59
61
  end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'symboltable'
3
- s.version = '1.0.1'
4
- s.date = '2010-05-12'
3
+ s.version = '1.0.2'
4
+ s.date = Time.now.strftime('%Y-%m-%d')
5
5
 
6
6
  s.summary = 'A Symbols-only Hash for Ruby'
7
7
  s.description = 'A Symbols-only Hash for Ruby'
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.files = Dir['lib/**/*.rb'] +
15
15
  Dir['test/*.rb'] +
16
- %w< symboltable.gemspec Rakefile README >
16
+ %w< symboltable.gemspec Rakefile README.md >
17
17
 
18
18
  s.test_files = s.files.select {|path| path =~ /^test\/.*.rb/ }
19
19
 
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.has_rdoc = true
23
23
  s.rdoc_options = %w< --line-numbers --inline-source --title SymbolTable --main SymbolTable >
24
- s.extra_rdoc_files = %w< README >
24
+ s.extra_rdoc_files = %w< README.md >
25
25
 
26
- s.homepage = 'http://mjijackson.com/symboltable'
26
+ s.homepage = 'http://mjijackson.github.com/symboltable'
27
27
  end
@@ -6,6 +6,12 @@ require 'symboltable'
6
6
 
7
7
  class SymbolTableTest < Test::Unit::TestCase
8
8
 
9
+ class A < SymbolTable
10
+ end
11
+
12
+ class B < SymbolTable
13
+ end
14
+
9
15
  def test_empty
10
16
  t = SymbolTable.new
11
17
 
@@ -37,6 +43,15 @@ class SymbolTableTest < Test::Unit::TestCase
37
43
  assert_equal(1, t['b'])
38
44
  end
39
45
 
46
+ def test_store_symboltable
47
+ a = A.new
48
+ b = B.new
49
+
50
+ a.a = b
51
+
52
+ assert_equal(B, a.a.class)
53
+ end
54
+
40
55
  def test_update
41
56
  t = SymbolTable.new
42
57
 
@@ -108,4 +123,9 @@ class SymbolTableTest < Test::Unit::TestCase
108
123
  assert_equal([2], b.values)
109
124
  end
110
125
 
126
+ def test_to_hash
127
+ t = SymbolTable[:a, 1]
128
+ assert_equal(t.to_hash.class, Hash)
129
+ assert_equal(t.to_hash[:a], 1)
130
+ end
111
131
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: symboltable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 2
9
+ version: 1.0.2
5
10
  platform: ruby
6
11
  authors:
7
12
  - Michael Jackson
@@ -9,19 +14,21 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-05-12 00:00:00 -07:00
17
+ date: 2012-06-21 00:00:00 -07:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: rake
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
23
29
  version: "0"
24
- version:
30
+ type: :development
31
+ version_requirements: *id001
25
32
  description: A Symbols-only Hash for Ruby
26
33
  email: mjijackson@gmail.com
27
34
  executables: []
@@ -29,15 +36,15 @@ executables: []
29
36
  extensions: []
30
37
 
31
38
  extra_rdoc_files:
32
- - README
39
+ - README.md
33
40
  files:
34
41
  - lib/symboltable.rb
35
42
  - test/symboltable.rb
36
43
  - symboltable.gemspec
37
44
  - Rakefile
38
- - README
45
+ - README.md
39
46
  has_rdoc: true
40
- homepage: http://mjijackson.com/symboltable
47
+ homepage: http://mjijackson.github.com/symboltable
41
48
  licenses: []
42
49
 
43
50
  post_install_message:
@@ -54,18 +61,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
61
  requirements:
55
62
  - - ">="
56
63
  - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
57
66
  version: "0"
58
- version:
59
67
  required_rubygems_version: !ruby/object:Gem::Requirement
60
68
  requirements:
61
69
  - - ">="
62
70
  - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
63
73
  version: "0"
64
- version:
65
74
  requirements: []
66
75
 
67
76
  rubyforge_project:
68
- rubygems_version: 1.3.5
77
+ rubygems_version: 1.3.6
69
78
  signing_key:
70
79
  specification_version: 3
71
80
  summary: A Symbols-only Hash for Ruby
data/README DELETED
@@ -1,82 +0,0 @@
1
- +++ SymbolTable +++
2
-
3
- SymbolTable is a handy little Ruby class that was conceived from the union of
4
- Hash and Symbol. SymbolTable directly extends Hash, but it stores all keys
5
- internally as symbols. Any key that cannot be converted to a Symbol is not
6
- valid.
7
-
8
- While this may seem restrictive, it does have the nice side effect of making
9
- all keys slightly more memorable and usable. For example, values may be set
10
- and retrieved using any key that resolves to the same symbol.
11
-
12
- h = Hash.new
13
- h[:a] = 1
14
- h[:a] # => 1
15
- h['a'] # => nil
16
- h.a # => NoMethodError: undefined method `a' for {}:Hash
17
-
18
- require 'symboltable'
19
-
20
- t = SymbolTable.new
21
- t[:a] = 1
22
- t[:a] # => 1
23
- t['a'] # => 1
24
- t.a # => 1
25
- t.b = 2 # => 2
26
-
27
- Also, because SymbolTable subclasses Hash, you get all the nice methods of Hash
28
- for free.
29
-
30
- t.keys # => [:a, :b]
31
- t.values # => [1, 2]
32
-
33
- t.merge!('c' => 3, :d => 4)
34
- t.keys # => [:a, :b, :c, :d]
35
- t.values # => [1, 2, 3, 4]
36
-
37
- t.each do |k, v|
38
- puts "#{k} = #{v}"
39
- end
40
-
41
- You get the idea. This kind of data structure is mainly useful for large
42
- configuration objects and the like, where you want to store a bunch of data and
43
- you don't want to have to remember later how you stored it.
44
-
45
- == Why?
46
-
47
- Mainly just for fun, and because I sometimes miss the automatic and ubiquitous
48
- type conversion you get in PHP.
49
-
50
- == Installation
51
-
52
- Using RubyGems:
53
-
54
- $ sudo gem install symboltable
55
-
56
- From a local copy:
57
-
58
- $ git clone git://github.com/mjijackson/symboltable.git
59
- $ cd symboltable
60
- $ rake package && sudo rake install
61
-
62
- == License
63
-
64
- Copyright 2010 Michael Jackson
65
-
66
- Permission is hereby granted, free of charge, to any person obtaining a copy
67
- of this software and associated documentation files (the "Software"), to deal
68
- in the Software without restriction, including without limitation the rights
69
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
70
- copies of the Software, and to permit persons to whom the Software is
71
- furnished to do so, subject to the following conditions:
72
-
73
- The above copyright notice and this permission notice shall be included in
74
- all copies or substantial portions of the Software.
75
-
76
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
77
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
78
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
79
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
80
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
81
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
82
- THE SOFTWARE.