typed_map 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +2 -0
- data/lib/typed_map/version.rb +3 -0
- data/lib/typed_map.rb +40 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/typed_map_spec.rb +147 -0
- data/typed_map.gemspec +26 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 092755b6e9e3f9778c4c20756959b48d9f4ac4f9
|
4
|
+
data.tar.gz: 76c5d56be0cb22bad9f1b4098549f63240bd810c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e86bcfbc64b9d1fdfac3107f536fbda61788709c4ee6c054e607a169a452612964d017f87123399ff79e4f5942bba81161eaab8aca4b156890f51a0456921bb
|
7
|
+
data.tar.gz: e5d8087bb6ed099778cd677b50d43a9b9f4020510c405a22e483e04242b10cc891783ee8ee1971e9fc01b63b921abec6e374d7255ceba3c8d6815738ed92cf23
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Vladimir Melnik
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# TypedMap
|
2
|
+
|
3
|
+
TypedMap is a Hash with typed keys and values.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'typed_map'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install typed_map
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TypedMap is a very simple data structure. It provides only 4 methods that are described below:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
pathes = TypedMap.new ktype: Symbol, vtype: String
|
27
|
+
|
28
|
+
pathes.add :root, "/"
|
29
|
+
pathes.add "key", "val" # Error
|
30
|
+
pathes.add :key, :val # Error
|
31
|
+
|
32
|
+
pathes.has? :root # => true
|
33
|
+
pathes.keys # => [:root]
|
34
|
+
pathes[:root] # => "/"
|
35
|
+
|
36
|
+
```
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it ( https://github.com/[my-github-username]/typed_map/fork )
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/typed_map.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require "typed_map/version"
|
2
|
+
|
3
|
+
class TypedMap
|
4
|
+
attr_reader :ktype, :vtype
|
5
|
+
|
6
|
+
def initialize(ktype:, vtype:)
|
7
|
+
raise ArgumentError, "'ktype' should be an instance of Class" unless ktype.instance_of?(Class)
|
8
|
+
raise ArgumentError, "'vtype' should be an instance of Class" unless vtype.instance_of?(Class)
|
9
|
+
|
10
|
+
@ktype = ktype
|
11
|
+
@vtype = vtype
|
12
|
+
|
13
|
+
@map = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def add(k, v)
|
17
|
+
raise ArgumentError, "'k' should be an instance of #{ktype}" unless k.instance_of?(ktype)
|
18
|
+
raise ArgumentError, "'v' should be an instance of #{vtype}" unless v.instance_of?(vtype)
|
19
|
+
raise ArgumentError, "key '#{k}' already exists" if has?(k)
|
20
|
+
|
21
|
+
@map[k] = v
|
22
|
+
end
|
23
|
+
|
24
|
+
def keys
|
25
|
+
@map.keys
|
26
|
+
end
|
27
|
+
|
28
|
+
def [](k)
|
29
|
+
raise ArgumentError, "'k' should be an instance of #{ktype}" unless k.instance_of?(ktype)
|
30
|
+
raise ArgumentError, "key '#{k}' not exists" unless has?(k)
|
31
|
+
|
32
|
+
@map[k]
|
33
|
+
end
|
34
|
+
|
35
|
+
def has?(k)
|
36
|
+
raise ArgumentError, "'k' should be an instance of #{ktype}" unless k.instance_of?(ktype)
|
37
|
+
|
38
|
+
@map.has_key? k
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
RSpec.describe TypedMap do
|
2
|
+
describe "class" do
|
3
|
+
subject { described_class }
|
4
|
+
|
5
|
+
describe ".new" do
|
6
|
+
context "when with good args" do
|
7
|
+
it "returns typed map" do
|
8
|
+
expect(subject.new ktype: Symbol, vtype: Symbol).to be_instance_of described_class
|
9
|
+
expect(subject.new ktype: Bignum, vtype: String).to be_instance_of described_class
|
10
|
+
expect(subject.new ktype: Hash, vtype: Array).to be_instance_of described_class
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when with bad args" do
|
15
|
+
it "raises ArgumentError" do
|
16
|
+
expect { subject.new }.to raise_error(ArgumentError).with_message("missing keywords: ktype, vtype")
|
17
|
+
expect { subject.new ktype: nil }.to raise_error(ArgumentError).with_message("missing keyword: vtype")
|
18
|
+
expect { subject.new vtype: nil }.to raise_error(ArgumentError).with_message("missing keyword: ktype")
|
19
|
+
expect { subject.new ktype: Symbol }.to raise_error(ArgumentError).with_message("missing keyword: vtype")
|
20
|
+
expect { subject.new vtype: Symbol }.to raise_error(ArgumentError).with_message("missing keyword: ktype")
|
21
|
+
expect { subject.new ktype: Symbol, vtype: nil }.to raise_error(ArgumentError).with_message("'vtype' should be an instance of Class")
|
22
|
+
expect { subject.new ktype: nil, vtype: Symbol }.to raise_error(ArgumentError).with_message("'ktype' should be an instance of Class")
|
23
|
+
expect { subject.new ktype: Symbol, vtype: nil }.to raise_error(ArgumentError).with_message("'vtype' should be an instance of Class")
|
24
|
+
expect { subject.new ktype: true, vtype: Symbol }.to raise_error(ArgumentError).with_message("'ktype' should be an instance of Class")
|
25
|
+
expect { subject.new ktype: Math, vtype: Enumerable }.to raise_error(ArgumentError).with_message("'ktype' should be an instance of Class")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "instance" do
|
32
|
+
subject { described_class.new ktype: Symbol, vtype: String }
|
33
|
+
|
34
|
+
describe "#[]" do
|
35
|
+
context "when good args" do
|
36
|
+
context "when element by given key exists" do
|
37
|
+
before { subject.add :key, "value" }
|
38
|
+
|
39
|
+
it "returns the coresponding value" do
|
40
|
+
expect(subject[:key]).to eq "value"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when element by given key doesn't exist" do
|
45
|
+
it "raises ArgumentError" do
|
46
|
+
expect { subject[:bad_key] }.to raise_error(ArgumentError).with_message("key 'bad_key' not exists")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "when bad args" do
|
52
|
+
before { subject.add :key, "value" }
|
53
|
+
|
54
|
+
it "returns ArgumentError" do
|
55
|
+
expect { subject[] }.to raise_error(ArgumentError).with_message("wrong number of arguments (0 for 1)")
|
56
|
+
expect { subject[nil] }.to raise_error(ArgumentError).with_message("'k' should be an instance of Symbol")
|
57
|
+
expect { subject[true] }.to raise_error(ArgumentError).with_message("'k' should be an instance of Symbol")
|
58
|
+
expect { subject["key"] }.to raise_error(ArgumentError).with_message("'k' should be an instance of Symbol")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#keys" do
|
64
|
+
context "when empty" do
|
65
|
+
it "returns an empty array" do
|
66
|
+
expect(subject.keys).to eq []
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "when has one key-value pair" do
|
71
|
+
before { subject.add :key1, "value" }
|
72
|
+
|
73
|
+
it "returns an array with key" do
|
74
|
+
expect(subject.keys).to eq [:key1]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context "when has many key-value pairs" do
|
79
|
+
before do
|
80
|
+
subject.add :key1, "value"
|
81
|
+
subject.add :key2, "value"
|
82
|
+
end
|
83
|
+
|
84
|
+
it "returns an array with key" do
|
85
|
+
expect(subject.keys).to eq [:key1, :key2]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "#add" do
|
91
|
+
context "when good args" do
|
92
|
+
context "when new key" do
|
93
|
+
it "adds new key-value pair" do
|
94
|
+
expect { subject.add :key, "value" }.to change { subject.has? :key }.from(false).to(true)
|
95
|
+
expect(subject[:key]).to eq "value"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context "when key already exists" do
|
100
|
+
before { subject.add :key, "value" }
|
101
|
+
|
102
|
+
it "raises ArgumentError" do
|
103
|
+
expect { subject.add :key, "value" }.to raise_error(ArgumentError).with_message("key 'key' already exists")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "when bad args" do
|
109
|
+
it "raises ArgumentError" do
|
110
|
+
expect { subject.add }.to raise_error(ArgumentError).with_message("wrong number of arguments (0 for 2)")
|
111
|
+
expect { subject.add :key }.to raise_error(ArgumentError).with_message("wrong number of arguments (1 for 2)")
|
112
|
+
expect { subject.add nil, nil }.to raise_error(ArgumentError).with_message("'k' should be an instance of Symbol")
|
113
|
+
expect { subject.add nil, "value" }.to raise_error(ArgumentError).with_message("'k' should be an instance of Symbol")
|
114
|
+
expect { subject.add "key", "value" }.to raise_error(ArgumentError).with_message("'k' should be an instance of Symbol")
|
115
|
+
expect { subject.add :key, nil }.to raise_error(ArgumentError).with_message("'v' should be an instance of String")
|
116
|
+
expect { subject.add :key, :value}.to raise_error(ArgumentError).with_message("'v' should be an instance of String")
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "#has?" do
|
122
|
+
context "when good args" do
|
123
|
+
context "when key exists" do
|
124
|
+
before { subject.add :key, "value" }
|
125
|
+
|
126
|
+
it "returns true" do
|
127
|
+
expect(subject.has? :key).to be true
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "when key doesn't exist" do
|
132
|
+
it "returns false" do
|
133
|
+
expect(subject.has? :key).to be false
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context "when bad args" do
|
139
|
+
it "raises ArgumentError" do
|
140
|
+
expect { subject.has? }.to raise_error(ArgumentError).with_message("wrong number of arguments (0 for 1)")
|
141
|
+
expect { subject.has? nil }.to raise_error(ArgumentError).with_message("'k' should be an instance of Symbol")
|
142
|
+
expect { subject.has? "key" }.to raise_error(ArgumentError).with_message("'k' should be an instance of Symbol")
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
data/typed_map.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'typed_map/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "typed_map"
|
8
|
+
spec.version = TypedMap::VERSION
|
9
|
+
spec.authors = ["Vladimir Melnik"]
|
10
|
+
spec.email = ["egotraumatic@gmail.com"]
|
11
|
+
spec.summary = %q{TypedMap is a Hash with typed keys and values.}
|
12
|
+
spec.description = %q{TypedMap is a Hash with typed keys and values.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
24
|
+
spec.add_development_dependency "mutant-rspec", "~> 0.8"
|
25
|
+
spec.add_development_dependency "guard-rspec", "~> 4.6"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: typed_map
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vladimir Melnik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mutant-rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.6'
|
83
|
+
description: TypedMap is a Hash with typed keys and values.
|
84
|
+
email:
|
85
|
+
- egotraumatic@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/typed_map.rb
|
97
|
+
- lib/typed_map/version.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
- spec/typed_map_spec.rb
|
100
|
+
- typed_map.gemspec
|
101
|
+
homepage: ''
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.5.1
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: TypedMap is a Hash with typed keys and values.
|
125
|
+
test_files:
|
126
|
+
- spec/spec_helper.rb
|
127
|
+
- spec/typed_map_spec.rb
|
128
|
+
has_rdoc:
|