type_struct 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +7 -0
- data/lib/type_struct/version.rb +3 -0
- data/lib/type_struct.rb +64 -0
- data/lib/type_struct_test.rb +138 -0
- data/type_struct.gemspec +23 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 33f3efe5ddc683da818f42a1d03862f760279efe
|
4
|
+
data.tar.gz: 2bd91598e3ce93198d2fa94b8e4bdf533bfebfb2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 996654d99f6f9502831e06bbc39b0052be5094b4220579f08d9c784c7e9cc98992b738ebd4b539f29e2ae3a3947bf2b64fd959fbee1661032681067d9103b78f
|
7
|
+
data.tar.gz: 239ebbdff12351ec80082965174fe5c06630dbe44c3506c09d4ce8a712e15b0bab50b9ae5f35e3a1ea631f5464a5d7c09c6bd531551a46ddd8cb9a1989fffe7f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 ksss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# TypeStruct
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/ksss/type_struct.svg)](https://travis-ci.org/ksss/type_struct)
|
4
|
+
|
5
|
+
Imitating static typed struct.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class Sample < TypeStruct.new(
|
11
|
+
str: String,
|
12
|
+
reg: /exp/,
|
13
|
+
num: Integer,
|
14
|
+
any: Object,
|
15
|
+
); end
|
16
|
+
|
17
|
+
sample = Sample.new(
|
18
|
+
str: "instance of String",
|
19
|
+
reg: "not match to regexp",
|
20
|
+
num: 10,
|
21
|
+
any: true,
|
22
|
+
)
|
23
|
+
|
24
|
+
p sample
|
25
|
+
#=> #<TypeStructTest::Sample str="instance of String", reg="not match to regexp", num=10, any=true>
|
26
|
+
|
27
|
+
p sample.to_h
|
28
|
+
#=> {:str=>"instance of String", :reg=>"not match to regexp", :num=>10, :any=>true}
|
29
|
+
```
|
30
|
+
|
31
|
+
## Installation
|
32
|
+
|
33
|
+
Add this line to your application's Gemfile:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
gem 'type_struct'
|
37
|
+
```
|
38
|
+
|
39
|
+
And then execute:
|
40
|
+
|
41
|
+
$ bundle
|
42
|
+
|
43
|
+
Or install it yourself as:
|
44
|
+
|
45
|
+
$ gem install type_struct
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/type_struct.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
class TypeStruct
|
2
|
+
require "type_struct/version"
|
3
|
+
|
4
|
+
class NoMemberError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def new(**args)
|
9
|
+
Class.new do
|
10
|
+
attr_accessor *args.keys
|
11
|
+
const_set :MEMBERS, args
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def members
|
15
|
+
const_get(:MEMBERS)
|
16
|
+
end
|
17
|
+
|
18
|
+
def type(k)
|
19
|
+
members[k]
|
20
|
+
end
|
21
|
+
|
22
|
+
def valid?(k, v)
|
23
|
+
type(k) === v
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(**arg)
|
28
|
+
self.class.members.each do |k, _|
|
29
|
+
self[k] = arg[k]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def []=(k, v)
|
34
|
+
raise TypeStruct::NoMemberError unless respond_to?(k)
|
35
|
+
unless self.class.valid?(k, v)
|
36
|
+
raise TypeError, "expect #{self.class.type(k)} got #{v.class}"
|
37
|
+
end
|
38
|
+
__send__("#{k}=", v)
|
39
|
+
end
|
40
|
+
|
41
|
+
def [](k)
|
42
|
+
raise TypeStruct::NoMemberError unless respond_to?(k)
|
43
|
+
__send__(k)
|
44
|
+
end
|
45
|
+
|
46
|
+
def inspect
|
47
|
+
m = to_h.map do |k, v|
|
48
|
+
"#{k}=#{v.inspect}"
|
49
|
+
end
|
50
|
+
"#<#{self.class.to_s} #{m.join(', ')}>"
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_h
|
54
|
+
m = {}
|
55
|
+
self.class.members.each do |k, _|
|
56
|
+
m[k] = self[k]
|
57
|
+
end
|
58
|
+
m
|
59
|
+
end
|
60
|
+
alias to_hash to_h
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'type_struct'
|
2
|
+
|
3
|
+
module TypeStructTest
|
4
|
+
class Dummy < TypeStruct.new(
|
5
|
+
str: String,
|
6
|
+
num: Integer,
|
7
|
+
reg: /abc/,
|
8
|
+
any: Object,
|
9
|
+
); end
|
10
|
+
|
11
|
+
def test_s_members(t)
|
12
|
+
m = Dummy.members
|
13
|
+
expect = {str: String, num: Integer, reg: /abc/, any: Object}
|
14
|
+
unless m == expect
|
15
|
+
t.error("expect #{expect} got #{m}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_s_type(t)
|
20
|
+
m = Dummy.members
|
21
|
+
m.each do |k, v|
|
22
|
+
unless Dummy.type(k) == v
|
23
|
+
t.error("expect #{v} got #{Dummy.type(k)}")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_s_valid?(t)
|
29
|
+
unless Dummy.valid?(:str, "abc")
|
30
|
+
t.error('expect :str valid "abc"')
|
31
|
+
end
|
32
|
+
if Dummy.valid?(:str, 345)
|
33
|
+
t.error("expect :str invalid 345")
|
34
|
+
end
|
35
|
+
unless Dummy.valid?(:reg, "abc")
|
36
|
+
t.error('expect :reg valid "abc"')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_initialize(t)
|
41
|
+
expects = {str: "aaa", num: 123, reg: "abc", any: [1, "bbb"]}
|
42
|
+
dummy = Dummy.new(str: "aaa", num: 123, reg: "abc", any: [1, "bbb"])
|
43
|
+
expects.each do |k, v|
|
44
|
+
unless dummy[k] == v
|
45
|
+
t.error("expect #{dummy[k]} got #{v}")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_initialize_not_enough(t)
|
51
|
+
_, err = go { Dummy.new(str: "aaa") }
|
52
|
+
if err == nil
|
53
|
+
t.error("in initialize, expect raise error since not enough members but nothing raised")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_initialize_invalid_type(t)
|
58
|
+
value, err = go { Dummy.new(str: "aaa", num: 123, reg: "abb", any: nil) }
|
59
|
+
if err == nil
|
60
|
+
t.error("invalid value expect raise error")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_to_h(t)
|
65
|
+
expects = {str: "aaa", num: 123, reg: "abcde", any: [1, "bbb"]}
|
66
|
+
dummy = Dummy.new(str: "aaa", num: 123, reg: "abcde", any: [1, "bbb"])
|
67
|
+
expects.each do |k, v|
|
68
|
+
unless dummy[k] == v
|
69
|
+
t.error("expect #{dummy[k]} got #{v}")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_getter(t)
|
75
|
+
dummy = Dummy.new(str: "aaa", num: 123, reg: "abc", any: [1, "bbb"])
|
76
|
+
_, err = go { dummy[:str] }
|
77
|
+
if err != nil
|
78
|
+
t.error("expect not raise error when valid value get. got #{err}")
|
79
|
+
end
|
80
|
+
_, err = go { dummy[:nothing] }
|
81
|
+
if err == nil
|
82
|
+
t.error("expect not raise error when invalid value get")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_setter(t)
|
87
|
+
dummy = Dummy.new(str: "aaa", num: 123, reg: "abc", any: [1, "bbb"])
|
88
|
+
%i(str num reg).each do |k, v|
|
89
|
+
_, err = go { dummy[k] = nil }
|
90
|
+
if err == nil
|
91
|
+
t.error("expect raise error when invalid value set")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
_, err = go { dummy[:any] = nil }
|
96
|
+
if err != nil
|
97
|
+
t.error("expect not raise error when valid value set got #{err}")
|
98
|
+
end
|
99
|
+
|
100
|
+
_, err = go { dummy[:nothing] = nil }
|
101
|
+
if err == nil
|
102
|
+
t.error("expect not raise error when valid value set got #{err}")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class Sample < TypeStruct.new(
|
107
|
+
str: String,
|
108
|
+
reg: /exp/,
|
109
|
+
num: Integer,
|
110
|
+
any: Object
|
111
|
+
); end
|
112
|
+
|
113
|
+
def example_readme
|
114
|
+
sample = Sample.new(
|
115
|
+
str: "instance of String",
|
116
|
+
reg: "not match to regexp",
|
117
|
+
num: 10,
|
118
|
+
any: true,
|
119
|
+
)
|
120
|
+
p sample
|
121
|
+
p sample.to_h
|
122
|
+
# Output:
|
123
|
+
# #<TypeStructTest::Sample str="instance of String", reg="not match to regexp", num=10, any=true>
|
124
|
+
# {:str=>"instance of String", :reg=>"not match to regexp", :num=>10, :any=>true}
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
def go
|
130
|
+
err = nil
|
131
|
+
begin
|
132
|
+
ret = yield
|
133
|
+
rescue => e
|
134
|
+
err = e
|
135
|
+
end
|
136
|
+
[ret, err]
|
137
|
+
end
|
138
|
+
end
|
data/type_struct.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'type_struct/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "type_struct"
|
8
|
+
spec.version = TypeStruct::VERSION
|
9
|
+
spec.authors = ["ksss"]
|
10
|
+
spec.email = ["co000ri@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Imitating static typed struct.}
|
13
|
+
spec.description = %q{Imitating static typed struct.}
|
14
|
+
spec.homepage = "https://github.com/ksss/type_struct"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rgot"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: type_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ksss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-20 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: rgot
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Imitating static typed struct.
|
56
|
+
email:
|
57
|
+
- co000ri@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/type_struct.rb
|
69
|
+
- lib/type_struct/version.rb
|
70
|
+
- lib/type_struct_test.rb
|
71
|
+
- type_struct.gemspec
|
72
|
+
homepage: https://github.com/ksss/type_struct
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.5.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Imitating static typed struct.
|
96
|
+
test_files: []
|