type_constraints 0.0.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c09c6618b3335ab1fe15343695323df479d872d
4
- data.tar.gz: 7bc2530f521a40381e98b600bcdcce3bf9d4a4cb
3
+ metadata.gz: d321d732d3d4087cda537651e00118b23d9337f6
4
+ data.tar.gz: bb47d587b09dd20882f273235a01fb66676c58c2
5
5
  SHA512:
6
- metadata.gz: 28f2a447bf63a6e714ed7190094f5afac7a441a040ab6c547a59894b19c6733baa21a3ee2d6cdebd0847e914a7ff5e2207a6f3bb83b05ad51cd75c121ba64854
7
- data.tar.gz: 331697c3de260279fbe87d0c3aecd400526bb46c564447385bf69425176f4728d5fdf7ec7f754dcef275e9958708d26b30269d4834a0a7802778ab6258459dea
6
+ metadata.gz: 28e407443eeb983a0f35409af49177b9ab417f7fabc813b1121a68a4a76d44cb82a54b20c3528fb5995f63885ac950712d62ef29acc6b9664dc9067fc056ced4
7
+ data.tar.gz: 71ce039bda02c0f5d9d0499f9e22c79d6295702d016db6aa327df96c75b150fd5791ede1917c91c4de94d81fa24ec153f7e2afab8216c3980a3fd1f9507cb85d
data/README.md CHANGED
@@ -25,6 +25,7 @@ p TypeConstraints.check?(:Array, {}) #=> false
25
25
  puts "testing ArrayOfString"
26
26
  p TypeConstraints.check?(:ArrayOfString, ["1000", "2000"]) #=> true
27
27
  p TypeConstraints.check?(:ArrayOfString, [1000, 2000]) #=> false
28
+ p TypeConstraints.check?(:ArrayOfString, {}) #=> false
28
29
 
29
30
  puts "testing ArrayOfHisa"
30
31
  p TypeConstraints.check?(:ArrayOfHisa, ["5518"]) #=> true
@@ -32,6 +33,35 @@ p TypeConstraints.check?(:ArrayOfHisa, ["hisa"]) #=> false
32
33
  p TypeConstraints.check?(:ArrayOfHisa, [5518]) #=> false
33
34
  ```
34
35
 
36
+ ## Default Type Constrains
37
+
38
+ ```ruby
39
+ BasicObject
40
+ Object
41
+ Hash
42
+ Array
43
+ Method
44
+ UnboundMethod
45
+ String
46
+ Symbol
47
+ Range
48
+ Exception
49
+ NilClass
50
+ TrueClass
51
+ FalseClass
52
+ Regexp
53
+ Time
54
+ MatchData
55
+ Proc
56
+ Module
57
+ Class
58
+ Numeric
59
+ Float
60
+ Integer
61
+ Bignum
62
+ Fixnum
63
+ ```
64
+
35
65
  ## Installation
36
66
 
37
67
  Add this line to your application's Gemfile:
@@ -16,5 +16,12 @@ module TypeConstraints
16
16
  meta.instance_eval(&code)
17
17
  metas[name] = meta
18
18
  end
19
+
20
+ # hash_type :Test, :key_name, ...
21
+ def hash_type(name, *keys, &code)
22
+ meta = Meta.new(name: name, parent: metas[:Hash], constraint: -> v { keys.all?() {|k| v.key?(k)} })
23
+ meta.instance_eval(&code) if code
24
+ metas[name] = meta
25
+ end
19
26
  end
20
27
  end
@@ -0,0 +1,51 @@
1
+ module TypeConstraints
2
+ module Type
3
+ module Default
4
+ end
5
+ end
6
+ end
7
+
8
+ module TypeConstraints::Type::Default
9
+ DEFAULT_TYPES = {
10
+ BasicObject: [-> v { v.kind_of?(BasicObject) }],
11
+ Object: [-> v { v.kind_of?(Object) }, :BasicObject],
12
+ Hash: [-> v { v.kind_of?(Hash) }, :Object],
13
+ Array: [-> v { v.kind_of?(Array) }, :Object],
14
+ Method: [-> v { v.kind_of?(Method) }, :Object],
15
+ UnboundMethod: [-> v { v.kind_of?(UnboundMethod) }, :Object],
16
+ String: [-> v { v.kind_of?(String) }, :Object],
17
+ Symbol: [-> v { v.kind_of?(Symbol) }, :Object],
18
+ Range: [-> v { v.kind_of?(Range) }, :Object],
19
+ Exception: [-> v { v.kind_of?(Exception) }, :Object],
20
+ NilClass: [-> v { v.kind_of?(NilClass) }, :Object],
21
+ TrueClass: [-> v { v.kind_of?(TrueClass) }, :Object],
22
+ FalseClass: [-> v { v.kind_of?(FalseClass) }, :Object],
23
+ Regexp: [-> v { v.kind_of?(Regexp) }, :Object],
24
+ Time: [-> v { v.kind_of?(Time) }, :Object],
25
+ MatchData: [-> v { v.kind_of?(MatchData) }, :MatchData],
26
+ Proc: [-> v { v.kind_of?(Proc) }, :Object],
27
+ Module: [-> v { v.kind_of?(Module) }, :Object],
28
+ Class: [-> v { v.kind_of?(Class) }, :Module],
29
+ Numeric: [-> v { v.kind_of?(Numeric) }, :Object],
30
+ Float: [-> v { v.kind_of?(Float) }, :Numeric],
31
+ Integer: [-> v { v.kind_of?(Integer) }, :Numeric],
32
+ Bignum: [-> v { v.kind_of?(Bignum) }, :Integer],
33
+ Fixnum: [-> v { v.kind_of?(Fixnum) }, :Integer],
34
+ Bignum: [-> v { v.kind_of?(Bignum) }, :Integer],
35
+ Fixnum: [-> v { v.kind_of?(Fixnum) }, :Integer],
36
+ }.freeze
37
+
38
+ TypeConstraints.setup do
39
+ DEFAULT_TYPES.each do |key, array|
40
+ if array[1] # has parent
41
+ subtype(key, array[1]) do
42
+ constraint array[0]
43
+ end
44
+ else
45
+ type(key) do
46
+ constraint array[0]
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module TypeConstraints
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -17,3 +17,5 @@ module TypeConstraints
17
17
  end
18
18
  end
19
19
  end
20
+
21
+ require "type_constraints/type/default"
@@ -31,4 +31,19 @@ describe TypeConstraints::Registry do
31
31
  expect(@r.metas[:ChildConstraint].nil?).to eq false
32
32
  end
33
33
  end
34
+
35
+ describe "#hash_type" do
36
+ before do
37
+ @r = TypeConstraints::Registry.new
38
+ @r.hash_type(:ChildConstraint, :test, :hoge)
39
+ end
40
+
41
+ it "Sets meta object for metas" do
42
+ expect(@r.metas.size).to eq 1
43
+ end
44
+ it "Sets hash_type meta" do
45
+ expect(@r.metas[:ChildConstraint].check?({test: "hoge"})).to eq false
46
+ expect(@r.metas[:ChildConstraint].check?({test: "hoge", hoge: "test"})).to eq true
47
+ end
48
+ end
34
49
  end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'type_constraints/type/default'
3
+
4
+ describe TypeConstraints::Type::Default do
5
+ it "return true" do
6
+ TypeConstraints.check?(:String, "test")
7
+ end
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: type_constraints
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hisaichi5518
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-16 00:00:00.000000000 Z
11
+ date: 2014-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,10 +69,12 @@ files:
69
69
  - lib/type_constraints.rb
70
70
  - lib/type_constraints/meta.rb
71
71
  - lib/type_constraints/registry.rb
72
+ - lib/type_constraints/type/default.rb
72
73
  - lib/type_constraints/version.rb
73
74
  - spec/spec_helper.rb
74
75
  - spec/type_constraints/meta_spec.rb
75
76
  - spec/type_constraints/registry_spec.rb
77
+ - spec/type_constraints/type/default_spec.rb
76
78
  - spec/type_constraints_spec.rb
77
79
  - type_constraints.gemspec
78
80
  homepage: https://github.com/hisaichi5518/type_constraints
@@ -103,4 +105,5 @@ test_files:
103
105
  - spec/spec_helper.rb
104
106
  - spec/type_constraints/meta_spec.rb
105
107
  - spec/type_constraints/registry_spec.rb
108
+ - spec/type_constraints/type/default_spec.rb
106
109
  - spec/type_constraints_spec.rb