superclass_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - rbx
5
+ - rbx-2.0
6
+ - jruby
7
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in superclass_hash.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2011 Piotr Jakubowski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a
4
+ copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included
12
+ in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ #SuperclassHash ![Build Status](http://travis-ci.org/piotrj/superclass_hash.png)
2
+
3
+ SuperclassHash is a hash that you could use if you are storing
4
+ values under class keys. If hash does not have particular class key
5
+ it would serach through superclasses and return the first available
6
+ value if any.
7
+
8
+ ```ruby
9
+ require "superclass_hash"
10
+
11
+ class A
12
+ end
13
+
14
+ class B < A
15
+ end
16
+
17
+ superhash = SuperclassHash.new
18
+ superhash[A] = "a"
19
+ superhash[A] # => "a"
20
+ superhash[B] # => "b"
21
+ ```
22
+
23
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.rspec_opts = ["--color", "--format documentation"]
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ class SuperclassHash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ class SuperclassHash
2
+ def initialize()
3
+ @hash = {}
4
+ end
5
+
6
+ def [](key)
7
+ unless key.is_a? Class
8
+ return @hash[key]
9
+ else
10
+ ancestors = key.ancestors
11
+ ancestors = ancestors[0..ancestors.index(Object)] #Going only as far as you can via superclass method
12
+ ancestors.each do |klass|
13
+ return @hash[klass] if @hash.has_key?(klass)
14
+ end
15
+ return nil
16
+ end
17
+ end
18
+
19
+ def []=(key, value)
20
+ @hash[key] = value
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ require "superclass_hash"
2
+
3
+ RSpec.configure do |c|
4
+ c.mock_with :rspec
5
+ end
@@ -0,0 +1,52 @@
1
+ require "spec_helper"
2
+
3
+ describe SuperclassHash do
4
+ before(:each) do
5
+ @superhash = SuperclassHash.new
6
+ end
7
+
8
+ context "for non-class keys" do
9
+ it "should return value if assigned for given key" do
10
+ @superhash[:key] = "a"
11
+ @superhash[:key].should == "a"
12
+ end
13
+
14
+ it "should return nil if no value has been assigned for given key" do
15
+ @superhash[:other_key].should == nil
16
+ end
17
+ end
18
+
19
+ context "for class keys" do
20
+ before(:all) do
21
+ class A
22
+ end
23
+
24
+ class B < A
25
+ end
26
+
27
+ class C < B
28
+ end
29
+ end
30
+
31
+ it "should return value if assigned for given key" do
32
+ @superhash[A] = "a"
33
+ @superhash[A].should == "a"
34
+ end
35
+
36
+ it "should return value if assigned for a superclass of key" do
37
+ @superhash[A] = "a"
38
+ @superhash[B].should == "a"
39
+ @superhash[C].should == "a"
40
+ end
41
+
42
+ it "should return nil if no value assigned for any of ancestors" do
43
+ @superhash[C].should == nil
44
+ end
45
+
46
+ it "if values has been assigned for few ancestors it should return the value of the nearest ancestor" do
47
+ @superhash[A] = "a"
48
+ @superhash[B] = "b"
49
+ @superhash[C].should == "b"
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "superclass_hash/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "superclass_hash"
7
+ s.version = SuperclassHash::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Piotr Jakubowski"]
10
+ s.email = ["piotrj@gmail.com"]
11
+ s.homepage = "https://github.com/piotrj/superclass_hash"
12
+ s.summary = %q{Hash useful for class keys}
13
+ s.description = %q{Hash that would return values that has been assigned to keys that are superclasses of asked key}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "rspec", ">= 2.6.0"
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "bundler"
23
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: superclass_hash
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Piotr Jakubowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-11 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.6.0
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id003
48
+ description: Hash that would return values that has been assigned to keys that are superclasses of asked key
49
+ email:
50
+ - piotrj@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - .travis.yml
60
+ - Gemfile
61
+ - LICENSE
62
+ - README.md
63
+ - Rakefile
64
+ - lib/superclass_hash.rb
65
+ - lib/superclass_hash/version.rb
66
+ - spec/spec_helper.rb
67
+ - spec/superclass_hash/superclass_hash_spec.rb
68
+ - superclass_hash.gemspec
69
+ homepage: https://github.com/piotrj/superclass_hash
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.5
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Hash useful for class keys
96
+ test_files:
97
+ - spec/spec_helper.rb
98
+ - spec/superclass_hash/superclass_hash_spec.rb