lookup-hash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (10) hide show
  1. data/.document +5 -0
  2. data/Gemfile +12 -0
  3. data/Gemfile.lock +24 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.md +42 -0
  6. data/Rakefile +37 -0
  7. data/VERSION +1 -0
  8. data/lib/lookup-hash.rb +121 -0
  9. data/test +43 -0
  10. metadata +111 -0
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "hash-utils", ">= 0.11.0"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ gem "riot", ">= 0.12.1"
12
+ end
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ hash-utils (0.11.0)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ riot (0.12.1)
12
+ rr
13
+ term-ansicolor
14
+ rr (1.0.2)
15
+ term-ansicolor (1.0.5)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.0.0)
22
+ hash-utils (>= 0.11.0)
23
+ jeweler (~> 1.5.2)
24
+ riot (>= 0.12.1)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Martin Kozák
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a 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
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ Lookup Hash
2
+ ===========
3
+
4
+ **lookup-hash** is intended for using as fast lookup table for simply
5
+ checking of existency of some item inside. It doesn't bring any
6
+ additional performance, it's defacto only [Hash][1] with booleans,
7
+ but it's better and more readable to write:
8
+
9
+ require "lookup-hash"
10
+ allowed = LookupHash[:alfa, :beta]
11
+
12
+ …than:
13
+
14
+ require "lookup-hash"
15
+ allowed = Hash[:alfa, true, :beta, true]
16
+
17
+ Other methods are equivalent to +Hash+ with exception of data assignment
18
+ methods which convert all values to booleans. New key it's possible to
19
+ add also by:
20
+
21
+ hash << :key # …or…
22
+ hash.add(:key)
23
+
24
+ Contributing
25
+ ------------
26
+
27
+ 1. Fork it.
28
+ 2. Create a branch (`git checkout -b 20101220-my-change`).
29
+ 3. Commit your changes (`git commit -am "Added something"`).
30
+ 4. Push to the branch (`git push origin 20101220-my-change`).
31
+ 5. Create an [Issue][2] with a link to your branch.
32
+ 6. Enjoy a refreshing Diet Coke and wait.
33
+
34
+ Copyright
35
+ ---------
36
+
37
+ Copyright &copy; 2011 [Martin Kozák][3]. See `LICENSE.txt` for
38
+ further details.
39
+
40
+ [1]: http://ruby-doc.org/core/classes/Hash.html
41
+ [2]: http://github.com/martinkozak/lookup-hash/issues
42
+ [3]: http://www.martinkozak.net/
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
16
+ gem.name = "lookup-hash"
17
+ gem.homepage = "http://github.com/martinkozak/lookup-hash"
18
+ gem.license = "MIT"
19
+ gem.summary = 'Hash intended for using as lookup table only for simply checking of existency of some item (key) inside.'
20
+ gem.email = "martinkozak@martinkozak.net"
21
+ gem.authors = ["Martin Kozák"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/rdoctask'
30
+ Rake::RDocTask.new do |rdoc|
31
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
32
+
33
+ rdoc.rdoc_dir = 'rdoc'
34
+ rdoc.title = "hash-utils #{version}"
35
+ rdoc.rdoc_files.include('README*')
36
+ rdoc.rdoc_files.include('lib/**/*.rb')
37
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,121 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "hash-utils/hash" # 0.11.0
5
+ require "hash-utils/object"
6
+
7
+ ##
8
+ # Hash intended for using as fast lookup table for simply checking of an
9
+ # existency of some item. It doesn't bring any additional performance,
10
+ # it's defacto only Hash with Booleans, but it's better write:
11
+ # allowed = LookupHash[:alfa, :beta]
12
+ #
13
+ # than:
14
+ # allowed = Hash[:alfa, true, :beta, true]
15
+ #
16
+
17
+ class LookupHash < Hash
18
+ ##
19
+ # Creates lookup hash. Expects key names as input. If array given,
20
+ # treat it as just array of keys.
21
+ #
22
+ # @return [Hash] new hash
23
+ #
24
+
25
+ def self.[](*args)
26
+ if args.first.kind_of? Array
27
+ args = args.first
28
+ end
29
+
30
+ new = [ ]
31
+ args.each do |i|
32
+ new << [i, true]
33
+ end
34
+
35
+ result = super(new)
36
+ result.default = false
37
+
38
+ return result
39
+ end
40
+
41
+ ##
42
+ # Returns a new, empty hash. If this hash is subsequently accessed
43
+ # by a key that doesn‘t correspond to a hash entry, the value
44
+ # returned depends on the style of new used to create the hash. In
45
+ # the first form, the access returns nil. If obj is specified,
46
+ # this single object will be used for all default values. If a block
47
+ # is specified, it will be called with the hash object and the key,
48
+ # and should return the default value. It is the block‘s
49
+ # responsibility to store the value in the hash if required.
50
+ #
51
+ # @note All values will be converted using +hash-utils+ Hash#to_b
52
+ # to Boolean in the {LookupHash}. Assigning of default value block
53
+ # isn't allowed.
54
+ # @see http://ruby-doc.org/core/classes/Hash.html#M000718
55
+ #
56
+
57
+ def initialize
58
+ super(false)
59
+ self.map_values! { |v| v.to_b }
60
+ end
61
+
62
+ ##
63
+ # Element Assignment—Associates the value given by value with the
64
+ # key given by key. key should not have its value changed while it
65
+ # is in use as a key (a +String+ passed as a key will be duplicated
66
+ # and frozen).
67
+ #
68
+ # @note Value will be converted using +hash-utils+ Hash#to_b
69
+ # to Boolean in the {LookupHash}.
70
+ # @see http://ruby-doc.org/core/classes/Hash.html#M000729
71
+ #
72
+
73
+ def []=(key, value)
74
+ super(key, value.to_b)
75
+ end
76
+
77
+ ##
78
+ # Replaces the contents of Hash with the contents of +other_hash+.
79
+ #
80
+ # @note Value will be converted using +hash-utils+ Hash#to_b
81
+ # to Boolean in the {LookupHash}.
82
+ # @see http://ruby-doc.org/core/classes/Hash.html#M000757
83
+ #
84
+
85
+ def replace(other_hash)
86
+ super(other_hash.map_values { |v| v.to_b })
87
+ end
88
+
89
+ ##
90
+ # Adds key to lookup hash.
91
+ # @param [Object] key key for add
92
+ #
93
+
94
+ def <<(key)
95
+ self[key] = true
96
+ end
97
+
98
+ alias :add :<<
99
+
100
+ ##
101
+ # Bans set the default value.
102
+ #
103
+
104
+ def default=
105
+ end
106
+
107
+ ##
108
+ # Bans set the default value.
109
+ #
110
+
111
+ def default=(value)
112
+ super(false)
113
+ end
114
+
115
+ ##
116
+ # Bans set the default block.
117
+ #
118
+
119
+ def default_proc=(value)
120
+ end
121
+ end
data/test ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
4
+
5
+ $:.push("./lib")
6
+ require "lookup-hash"
7
+ require "riot"
8
+
9
+ context "LookupHash" do
10
+ setup do
11
+ LookupHash[:alfa, :beta]
12
+ end
13
+
14
+ asserts("#[] (static)") do
15
+ topic == {:alfa => true, :beta => true}
16
+ end
17
+ asserts("#<<") do
18
+ hash = topic.dup
19
+ hash << :gama
20
+ hash == {:alfa => true, :beta => true, :gama => true}
21
+ end
22
+ asserts("#[]=") do
23
+ hash = topic.dup
24
+ hash[:gama] = "something"
25
+ hash == {:alfa => true, :beta => true, :gama => true}
26
+ end
27
+ asserts("#default=") do
28
+ topic.default = "something"
29
+ topic.default == false
30
+ end
31
+ asserts("#default_proc=") do
32
+ topic.default_proc = "something"
33
+ topic.default_proc.nil?
34
+ end
35
+ asserts("#replace") do
36
+ hash = topic.dup
37
+ hash.replace(:alfa => "alfa", :beta => nil)
38
+ hash == {:alfa => true, :beta => false}
39
+ end
40
+ asserts("default value") do
41
+ topic[:gama] == false
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lookup-hash
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - "Martin Koz\xC3\xA1k"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-01 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hash-utils
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.11.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.0.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.2
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: riot
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.12.1
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ description:
61
+ email: martinkozak@martinkozak.net
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.md
69
+ files:
70
+ - .document
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - VERSION
77
+ - lib/lookup-hash.rb
78
+ - test
79
+ has_rdoc: true
80
+ homepage: http://github.com/martinkozak/lookup-hash
81
+ licenses:
82
+ - MIT
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 950339550497495953
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.5.3
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Hash intended for using as lookup table only for simply checking of existency of some item (key) inside.
110
+ test_files: []
111
+