type_constraints 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +7 -0
- data/lib/type_constraints/meta.rb +23 -0
- data/lib/type_constraints/registry.rb +20 -0
- data/lib/type_constraints/version.rb +3 -0
- data/lib/type_constraints.rb +19 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/type_constraints/meta_spec.rb +38 -0
- data/spec/type_constraints/registry_spec.rb +34 -0
- data/spec/type_constraints_spec.rb +48 -0
- data/type_constraints.gemspec +24 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1c09c6618b3335ab1fe15343695323df479d872d
|
4
|
+
data.tar.gz: 7bc2530f521a40381e98b600bcdcce3bf9d4a4cb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 28f2a447bf63a6e714ed7190094f5afac7a441a040ab6c547a59894b19c6733baa21a3ee2d6cdebd0847e914a7ff5e2207a6f3bb83b05ad51cd75c121ba64854
|
7
|
+
data.tar.gz: 331697c3de260279fbe87d0c3aecd400526bb46c564447385bf69425176f4728d5fdf7ec7f754dcef275e9958708d26b30269d4834a0a7802778ab6258459dea
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 hisaichi5518
|
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,57 @@
|
|
1
|
+
# TypeConstraints
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
require "type_constraints"
|
7
|
+
|
8
|
+
TypeConstraints.setup do
|
9
|
+
type :Array do
|
10
|
+
constraint -> v { v.kind_of?(Array) }
|
11
|
+
end
|
12
|
+
subtype :ArrayOfString, :Array do
|
13
|
+
constraint -> v { v.all?() {|v| v.kind_of?(String) } }
|
14
|
+
end
|
15
|
+
subtype :ArrayOfHisa, :ArrayOfString do
|
16
|
+
constraint -> v { v.all?() {|v| v === "5518" } }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
puts "testing Array"
|
22
|
+
p TypeConstraints.check?(:Array, []) #=> true
|
23
|
+
p TypeConstraints.check?(:Array, {}) #=> false
|
24
|
+
|
25
|
+
puts "testing ArrayOfString"
|
26
|
+
p TypeConstraints.check?(:ArrayOfString, ["1000", "2000"]) #=> true
|
27
|
+
p TypeConstraints.check?(:ArrayOfString, [1000, 2000]) #=> false
|
28
|
+
|
29
|
+
puts "testing ArrayOfHisa"
|
30
|
+
p TypeConstraints.check?(:ArrayOfHisa, ["5518"]) #=> true
|
31
|
+
p TypeConstraints.check?(:ArrayOfHisa, ["hisa"]) #=> false
|
32
|
+
p TypeConstraints.check?(:ArrayOfHisa, [5518]) #=> false
|
33
|
+
```
|
34
|
+
|
35
|
+
## Installation
|
36
|
+
|
37
|
+
Add this line to your application's Gemfile:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
gem 'type_constraints'
|
41
|
+
```
|
42
|
+
|
43
|
+
And then execute:
|
44
|
+
|
45
|
+
$ bundle
|
46
|
+
|
47
|
+
Or install it yourself as:
|
48
|
+
|
49
|
+
$ gem install type_constraints
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it ( https://github.com/[my-github-username]/type_constraints/fork )
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module TypeConstraints
|
2
|
+
class Meta
|
3
|
+
|
4
|
+
attr_accessor :name, :parent
|
5
|
+
def initialize(args={})
|
6
|
+
@name = args[:name]
|
7
|
+
@parent = args[:parent]
|
8
|
+
@constraint = args[:constraint]
|
9
|
+
end
|
10
|
+
|
11
|
+
def constraint(v=nil)
|
12
|
+
@constraint = v if !v.nil?
|
13
|
+
@constraint
|
14
|
+
end
|
15
|
+
|
16
|
+
def check?(val)
|
17
|
+
if !parent.nil? && !parent.check?(val)
|
18
|
+
return false
|
19
|
+
end
|
20
|
+
constraint.call(val)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module TypeConstraints
|
2
|
+
class Registry
|
3
|
+
attr_accessor :metas
|
4
|
+
def initialize
|
5
|
+
@metas = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def type(name, &code)
|
9
|
+
meta = Meta.new(name: name)
|
10
|
+
meta.instance_eval(&code)
|
11
|
+
metas[name] = meta
|
12
|
+
end
|
13
|
+
|
14
|
+
def subtype(name, parent, &code)
|
15
|
+
meta = Meta.new(name: name, parent: metas[parent])
|
16
|
+
meta.instance_eval(&code)
|
17
|
+
metas[name] = meta
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "type_constraints/version"
|
2
|
+
require "type_constraints/registry"
|
3
|
+
require "type_constraints/meta"
|
4
|
+
|
5
|
+
module TypeConstraints
|
6
|
+
class << self
|
7
|
+
attr_accessor :registry
|
8
|
+
def setup(&code)
|
9
|
+
@registry = Registry.new
|
10
|
+
@registry.instance_eval(&code)
|
11
|
+
@registry
|
12
|
+
end
|
13
|
+
|
14
|
+
def check?(name, val)
|
15
|
+
return false if registry.metas[name].nil?
|
16
|
+
registry.metas[name].check?(val)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'type_constraints/meta'
|
3
|
+
|
4
|
+
describe TypeConstraints::Meta do
|
5
|
+
describe "#initialize" do
|
6
|
+
context "Create new instance" do
|
7
|
+
meta = TypeConstraints::Meta.new(name: :AlwaysTrue, constraint: -> v { true })
|
8
|
+
it "returns TypeConstraints::Meta Object" do
|
9
|
+
expect(meta.class).to eq TypeConstraints::Meta
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#check?" do
|
15
|
+
context "Without parent Object." do
|
16
|
+
meta = TypeConstraints::Meta.new(name: :AlwaysTrue, constraint: -> v { true })
|
17
|
+
it "returns TrueClass Object" do
|
18
|
+
result = meta.check?("hogehoge")
|
19
|
+
expect(result).to eq true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "With parent Object" do
|
24
|
+
parent = TypeConstraints::Meta.new(name: :AlwaysTrue, constraint: -> v { false })
|
25
|
+
meta = TypeConstraints::Meta.new(
|
26
|
+
name: :AlwaysTrue,
|
27
|
+
constraint: -> v { true },
|
28
|
+
parent: parent
|
29
|
+
)
|
30
|
+
|
31
|
+
it "returns TrueClass Object" do
|
32
|
+
result = meta.check?("hogehoge")
|
33
|
+
expect(result).to eq false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'type_constraints/registry'
|
3
|
+
|
4
|
+
describe TypeConstraints::Registry do
|
5
|
+
|
6
|
+
describe "#type" do
|
7
|
+
before do
|
8
|
+
@r = TypeConstraints::Registry.new
|
9
|
+
@r.type(:ParentConstraint) { }
|
10
|
+
end
|
11
|
+
|
12
|
+
it "Sets meta object for metas" do
|
13
|
+
expect(@r.metas.size).to eq 1
|
14
|
+
end
|
15
|
+
it "Sets subtype meta" do
|
16
|
+
expect(@r.metas[:ParentConstraint].nil?).to eq false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#subtype" do
|
21
|
+
before do
|
22
|
+
@r = TypeConstraints::Registry.new
|
23
|
+
@r.type(:ParentConstraint) { }
|
24
|
+
@r.subtype(:ChildConstraint, :ParentConstraint) { }
|
25
|
+
end
|
26
|
+
|
27
|
+
it "Sets meta object for metas" do
|
28
|
+
expect(@r.metas.size).to eq 2
|
29
|
+
end
|
30
|
+
it "Sets subtype meta" do
|
31
|
+
expect(@r.metas[:ChildConstraint].nil?).to eq false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TypeConstraints do
|
4
|
+
|
5
|
+
TypeConstraints.setup do
|
6
|
+
type :TypeName do
|
7
|
+
constraint -> v { v.kind_of?(Array) }
|
8
|
+
end
|
9
|
+
subtype :TypeName2, :TypeName do
|
10
|
+
constraint -> array {
|
11
|
+
array.all? do |v|
|
12
|
+
v.kind_of?(String)
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#setup" do
|
19
|
+
it "Created registry" do
|
20
|
+
expect(TypeConstraints.registry.class).to eq TypeConstraints::Registry
|
21
|
+
end
|
22
|
+
|
23
|
+
it "registry has TypeName meta" do
|
24
|
+
expect(TypeConstraints.registry.metas[:TypeName].nil?).to eq false
|
25
|
+
expect(TypeConstraints.registry.metas[:TypeName].constraint.class).to eq Proc
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#check?" do
|
30
|
+
it "return true if passed :TypeName and array" do
|
31
|
+
expect(TypeConstraints.check?(:TypeName, [100,101,102])).to eq true
|
32
|
+
end
|
33
|
+
it "return false if passed :TypeName and integer" do
|
34
|
+
expect(TypeConstraints.check?(:TypeName, 10000000000000)).to eq false
|
35
|
+
end
|
36
|
+
|
37
|
+
it "return true if passed :TypeName2 and Array[string]" do
|
38
|
+
expect(TypeConstraints.check?(:TypeName2, ["100", "200"])).to eq true
|
39
|
+
end
|
40
|
+
it "return false if passed :TypeName2 and Array[integer]" do
|
41
|
+
expect(TypeConstraints.check?(:TypeName2, [100, 200])).to eq false
|
42
|
+
end
|
43
|
+
|
44
|
+
it "return false if not exists meta" do
|
45
|
+
expect(TypeConstraints.check?(:Hoge, [100, 200])).to eq false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'type_constraints/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "type_constraints"
|
8
|
+
spec.version = TypeConstraints::VERSION
|
9
|
+
spec.authors = ["hisaichi5518"]
|
10
|
+
spec.email = ["hisaichi5518@gmail.com"]
|
11
|
+
spec.summary = %q{Type constraint system for Ruby}
|
12
|
+
spec.description = %q{Type constraint system for Ruby}
|
13
|
+
spec.homepage = "https://github.com/hisaichi5518/type_constraints"
|
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"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: type_constraints
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hisaichi5518
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-16 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: '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: Type constraint system for Ruby
|
56
|
+
email:
|
57
|
+
- hisaichi5518@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/type_constraints.rb
|
70
|
+
- lib/type_constraints/meta.rb
|
71
|
+
- lib/type_constraints/registry.rb
|
72
|
+
- lib/type_constraints/version.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- spec/type_constraints/meta_spec.rb
|
75
|
+
- spec/type_constraints/registry_spec.rb
|
76
|
+
- spec/type_constraints_spec.rb
|
77
|
+
- type_constraints.gemspec
|
78
|
+
homepage: https://github.com/hisaichi5518/type_constraints
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.2.2
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Type constraint system for Ruby
|
102
|
+
test_files:
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/type_constraints/meta_spec.rb
|
105
|
+
- spec/type_constraints/registry_spec.rb
|
106
|
+
- spec/type_constraints_spec.rb
|