lookup-hash 0.1.0 → 0.2.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/README.md +5 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/lookup-hash.rb +2 -0
- data/lib/lookup-hash/frozen.rb +68 -0
- data/lookup-hash.gemspec +58 -0
- data/test +18 -0
- metadata +6 -4
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Lookup Hash
|
2
2
|
===========
|
3
3
|
|
4
|
-
**lookup-hash** is intended for using as fast lookup table for
|
4
|
+
**lookup-hash** is intended for using as fast lookup table for simple
|
5
5
|
checking of existency of some item inside. It doesn't bring any
|
6
6
|
additional performance, it's defacto only [Hash][1] with booleans,
|
7
7
|
but it's better and more readable to write:
|
@@ -14,12 +14,15 @@ but it's better and more readable to write:
|
|
14
14
|
require "lookup-hash"
|
15
15
|
allowed = Hash[:alfa, true, :beta, true]
|
16
16
|
|
17
|
-
Other methods are equivalent to
|
17
|
+
Other methods are equivalent to `Hash` with exception of data assignment
|
18
18
|
methods which convert all values to booleans. New key it's possible to
|
19
19
|
add also by:
|
20
20
|
|
21
21
|
hash << :key # …or…
|
22
22
|
hash.add(:key)
|
23
|
+
|
24
|
+
*Implicitly frozen* lookup hash is available as `Frozen::LookupHash`
|
25
|
+
in `lookup-hash/frozen`.
|
23
26
|
|
24
27
|
Contributing
|
25
28
|
------------
|
data/Rakefile
CHANGED
@@ -16,7 +16,7 @@ Jeweler::Tasks.new do |gem|
|
|
16
16
|
gem.name = "lookup-hash"
|
17
17
|
gem.homepage = "http://github.com/martinkozak/lookup-hash"
|
18
18
|
gem.license = "MIT"
|
19
|
-
gem.summary = 'Hash intended for using as lookup table only for
|
19
|
+
gem.summary = 'Hash intended for using as lookup table only for simple checking of existency of some item (key) inside.'
|
20
20
|
gem.email = "martinkozak@martinkozak.net"
|
21
21
|
gem.authors = ["Martin Kozák"]
|
22
22
|
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/lookup-hash.rb
CHANGED
@@ -15,6 +15,7 @@ require "hash-utils/object"
|
|
15
15
|
#
|
16
16
|
|
17
17
|
class LookupHash < Hash
|
18
|
+
|
18
19
|
##
|
19
20
|
# Creates lookup hash. Expects key names as input. If array given,
|
20
21
|
# treat it as just array of keys.
|
@@ -118,4 +119,5 @@ class LookupHash < Hash
|
|
118
119
|
|
119
120
|
def default_proc=(value)
|
120
121
|
end
|
122
|
+
|
121
123
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
require "lookup-hash"
|
5
|
+
|
6
|
+
##
|
7
|
+
# Implicitly frozen objects.
|
8
|
+
# @since 0.2.0
|
9
|
+
#
|
10
|
+
|
11
|
+
module Frozen
|
12
|
+
|
13
|
+
##
|
14
|
+
# Hash intended for using as fast lookup table for simply checking of an
|
15
|
+
# existency of some item. It doesn't bring any additional performance,
|
16
|
+
# it's defacto only Hash with Booleans, but it's better to write:
|
17
|
+
#
|
18
|
+
# class Foo
|
19
|
+
# CONST = Frozen::LookupHash[:alfa, :beta]
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# than:
|
23
|
+
# class Foo
|
24
|
+
# CONST = Hash[:alfa, true, :beta, true].freeze
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# Implicitly frozen for use in class constants.
|
28
|
+
#
|
29
|
+
# @since 0.2.0
|
30
|
+
#
|
31
|
+
|
32
|
+
class LookupHash < ::LookupHash
|
33
|
+
|
34
|
+
##
|
35
|
+
# Creates lookup hash. Expects key names as input. If array given,
|
36
|
+
# treat it as just array of keys.
|
37
|
+
#
|
38
|
+
# @return [Hash] new hash
|
39
|
+
#
|
40
|
+
|
41
|
+
def self.[](*args)
|
42
|
+
result = super(*args)
|
43
|
+
result.freeze
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Returns a new, empty hash. If this hash is subsequently accessed
|
48
|
+
# by a key that doesn‘t correspond to a hash entry, the value
|
49
|
+
# returned depends on the style of new used to create the hash. In
|
50
|
+
# the first form, the access returns nil. If obj is specified,
|
51
|
+
# this single object will be used for all default values. If a block
|
52
|
+
# is specified, it will be called with the hash object and the key,
|
53
|
+
# and should return the default value. It is the block‘s
|
54
|
+
# responsibility to store the value in the hash if required.
|
55
|
+
#
|
56
|
+
# @note All values will be converted using +hash-utils+ Hash#to_b
|
57
|
+
# to Boolean in the {LookupHash}. Assigning of default value block
|
58
|
+
# isn't allowed.
|
59
|
+
# @see http://ruby-doc.org/core/classes/Hash.html#M000718
|
60
|
+
#
|
61
|
+
|
62
|
+
def initialize
|
63
|
+
super
|
64
|
+
self.freeze
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
data/lookup-hash.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{lookup-hash}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Martin Kozák"]
|
12
|
+
s.date = %q{2011-03-02}
|
13
|
+
s.email = %q{martinkozak@martinkozak.net}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE.txt",
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.md",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/lookup-hash.rb",
|
27
|
+
"lib/lookup-hash/frozen.rb",
|
28
|
+
"lookup-hash.gemspec",
|
29
|
+
"test"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/martinkozak/lookup-hash}
|
32
|
+
s.licenses = ["MIT"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.5.3}
|
35
|
+
s.summary = %q{Hash intended for using as lookup table only for simple checking of existency of some item (key) inside.}
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
s.specification_version = 3
|
39
|
+
|
40
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
41
|
+
s.add_runtime_dependency(%q<hash-utils>, [">= 0.11.0"])
|
42
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
43
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
44
|
+
s.add_development_dependency(%q<riot>, [">= 0.12.1"])
|
45
|
+
else
|
46
|
+
s.add_dependency(%q<hash-utils>, [">= 0.11.0"])
|
47
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
49
|
+
s.add_dependency(%q<riot>, [">= 0.12.1"])
|
50
|
+
end
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<hash-utils>, [">= 0.11.0"])
|
53
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
54
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
55
|
+
s.add_dependency(%q<riot>, [">= 0.12.1"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/test
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
|
5
5
|
$:.push("./lib")
|
6
6
|
require "lookup-hash"
|
7
|
+
require "lookup-hash/frozen"
|
7
8
|
require "riot"
|
8
9
|
|
9
10
|
context "LookupHash" do
|
@@ -41,3 +42,20 @@ context "LookupHash" do
|
|
41
42
|
topic[:gama] == false
|
42
43
|
end
|
43
44
|
end
|
45
|
+
|
46
|
+
context "Frozen::LookupHash" do
|
47
|
+
setup do
|
48
|
+
Frozen::LookupHash[:alfa, :beta]
|
49
|
+
end
|
50
|
+
|
51
|
+
asserts("non-frozen class object equivalence") do
|
52
|
+
topic == LookupHash[:alfa, :beta]
|
53
|
+
end
|
54
|
+
asserts("frozen state") do
|
55
|
+
begin
|
56
|
+
topic.replace({:c => :d})
|
57
|
+
rescue RuntimeError => e
|
58
|
+
true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: lookup-hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Martin Koz\xC3\xA1k"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-02 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -75,6 +75,8 @@ files:
|
|
75
75
|
- Rakefile
|
76
76
|
- VERSION
|
77
77
|
- lib/lookup-hash.rb
|
78
|
+
- lib/lookup-hash/frozen.rb
|
79
|
+
- lookup-hash.gemspec
|
78
80
|
- test
|
79
81
|
has_rdoc: true
|
80
82
|
homepage: http://github.com/martinkozak/lookup-hash
|
@@ -90,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
92
|
requirements:
|
91
93
|
- - ">="
|
92
94
|
- !ruby/object:Gem::Version
|
93
|
-
hash:
|
95
|
+
hash: 12821966045354292
|
94
96
|
segments:
|
95
97
|
- 0
|
96
98
|
version: "0"
|
@@ -106,6 +108,6 @@ rubyforge_project:
|
|
106
108
|
rubygems_version: 1.5.3
|
107
109
|
signing_key:
|
108
110
|
specification_version: 3
|
109
|
-
summary: Hash intended for using as lookup table only for
|
111
|
+
summary: Hash intended for using as lookup table only for simple checking of existency of some item (key) inside.
|
110
112
|
test_files: []
|
111
113
|
|