eapi 0.1.1 → 0.1.2
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 +4 -4
- data/README.md +15 -0
- data/lib/eapi/type_checker.rb +9 -1
- data/lib/eapi/version.rb +1 -1
- data/spec/type_spec.rb +23 -9
- data/spec/validations_spec.rb +69 -15
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8a6da6f094409d07aa648318c1134b587bee426
|
4
|
+
data.tar.gz: c9d3e9715481dc3d1be43324fef7f81e860f3f06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46a2d8ccee0b0ccd1ca44756b6e7b5a7ed89801c0fd0192fa58367b4a55136df17689dcf13b59f4311bbe76b168e1f3ae84c80092d976467ae249cbb9ca7a5fe
|
7
|
+
data.tar.gz: 6e53f3f52a55fb714fbf48763f075afbb2bee60c77a40f72ed72b73cae1bab78025879b997362bf8b5036d1f15d5a4d5f7613955e32bf6b32a1f2a9657157a0b
|
data/README.md
CHANGED
@@ -294,6 +294,21 @@ eapi.init_something
|
|
294
294
|
eapi.something # => {}
|
295
295
|
```
|
296
296
|
|
297
|
+
A symbol or a string can also be specified as class name in `type` option, and it will be loaded on type check. This can be helpful to avoid loading problems. Using the same example as before:
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
class TestKlass
|
301
|
+
include Eapi::Common
|
302
|
+
|
303
|
+
property :something, type: "Hash"
|
304
|
+
end
|
305
|
+
|
306
|
+
eapi = TestKlass.new
|
307
|
+
eapi.something # => nil
|
308
|
+
eapi.init_something
|
309
|
+
eapi.something # => {}
|
310
|
+
```
|
311
|
+
|
297
312
|
To trigger the error, the value must not be an instance of the given Type, and also must not respond `true` to `value.is?(type)`
|
298
313
|
|
299
314
|
#### Custom validation with `validate_with` option
|
data/lib/eapi/type_checker.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Eapi
|
2
|
-
class TypeChecker < Struct.new(:
|
2
|
+
class TypeChecker < Struct.new(:given_type)
|
3
3
|
def is_valid_type?(value)
|
4
4
|
value.nil? || is_same_type?(value) || poses_as_type?(value)
|
5
5
|
end
|
@@ -12,5 +12,13 @@ module Eapi
|
|
12
12
|
def poses_as_type?(value)
|
13
13
|
value.respond_to?(:is?) && value.is?(type)
|
14
14
|
end
|
15
|
+
|
16
|
+
def type
|
17
|
+
if given_type.kind_of? Module
|
18
|
+
given_type
|
19
|
+
else
|
20
|
+
given_type.to_s.constantize
|
21
|
+
end
|
22
|
+
end
|
15
23
|
end
|
16
24
|
end
|
data/lib/eapi/version.rb
CHANGED
data/spec/type_spec.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Eapi do
|
4
|
+
class SomeType
|
5
|
+
end
|
4
6
|
|
5
7
|
context 'type check' do
|
6
|
-
class OtherType
|
7
|
-
end
|
8
|
-
|
9
8
|
class MyTestTypeKlass
|
10
9
|
include Eapi::Common
|
11
|
-
|
12
|
-
is :one_thing, :other_thing, OtherType
|
10
|
+
is :one_thing, :other_thing, SomeType
|
13
11
|
end
|
14
12
|
|
15
13
|
describe '#is? and .is?' do
|
@@ -30,8 +28,8 @@ RSpec.describe Eapi do
|
|
30
28
|
expect(MyTestTypeKlass).not_to be_is :not_you
|
31
29
|
expect(MyTestTypeKlass).to be_is :one_thing
|
32
30
|
expect(MyTestTypeKlass).to be_is :other_thing
|
33
|
-
expect(MyTestTypeKlass).to be_is
|
34
|
-
expect(MyTestTypeKlass).to be_is :
|
31
|
+
expect(MyTestTypeKlass).to be_is SomeType
|
32
|
+
expect(MyTestTypeKlass).to be_is :SomeType
|
35
33
|
end
|
36
34
|
end
|
37
35
|
|
@@ -51,8 +49,8 @@ RSpec.describe Eapi do
|
|
51
49
|
expect(obj).not_to be_is :not_you
|
52
50
|
expect(obj).to be_is :one_thing
|
53
51
|
expect(obj).to be_is :other_thing
|
54
|
-
expect(obj).to be_is
|
55
|
-
expect(obj).to be_is :
|
52
|
+
expect(obj).to be_is SomeType
|
53
|
+
expect(obj).to be_is :SomeType
|
56
54
|
end
|
57
55
|
end
|
58
56
|
|
@@ -83,4 +81,20 @@ RSpec.describe Eapi do
|
|
83
81
|
end
|
84
82
|
end
|
85
83
|
end
|
84
|
+
|
85
|
+
context 'using symbol as type' do
|
86
|
+
class MyTestTypeKlassSymbol
|
87
|
+
include Eapi::Common
|
88
|
+
is :SomeType
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'works the same' do
|
92
|
+
expect(MyTestTypeKlassSymbol).to be_is SomeType
|
93
|
+
expect(MyTestTypeKlassSymbol).to be_is :SomeType
|
94
|
+
|
95
|
+
obj = MyTestTypeKlassSymbol.new
|
96
|
+
expect(obj).to be_is SomeType
|
97
|
+
expect(obj).to be_is :SomeType
|
98
|
+
end
|
99
|
+
end
|
86
100
|
end
|
data/spec/validations_spec.rb
CHANGED
@@ -90,27 +90,81 @@ RSpec.describe Eapi do
|
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
class
|
94
|
-
|
93
|
+
describe 'using a class as type' do
|
94
|
+
class MyTestClassValType
|
95
|
+
include Eapi::Common
|
95
96
|
|
96
|
-
|
97
|
-
|
97
|
+
property :something, type: Hash
|
98
|
+
end
|
98
99
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
100
|
+
it 'invalid if value is not of that type' do
|
101
|
+
eapi = MyTestClassValType.new something: 1
|
102
|
+
expect(eapi).not_to be_valid
|
103
|
+
expect(eapi.errors.full_messages).to eq ["Something must be a Hash"]
|
104
|
+
expect(eapi.errors.messages).to eq({something: ["must be a Hash"]})
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'valid if value is of the given type' do
|
108
|
+
eapi = MyTestClassValType.new something: {}
|
109
|
+
expect(eapi).to be_valid
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'valid if value is not an instance of the given type but responds true to `.is?(type)`' do
|
113
|
+
eapi = MyTestClassValType.new something: SimilarToHash.new
|
114
|
+
expect(eapi).to be_valid
|
115
|
+
end
|
104
116
|
end
|
105
117
|
|
106
|
-
|
107
|
-
|
108
|
-
|
118
|
+
|
119
|
+
describe 'using a symbol as type' do
|
120
|
+
class MyTestClassValTypeSymbol
|
121
|
+
include Eapi::Common
|
122
|
+
|
123
|
+
property :something, type: :Hash
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'invalid if value is not of that type' do
|
127
|
+
eapi = MyTestClassValTypeSymbol.new something: 1
|
128
|
+
expect(eapi).not_to be_valid
|
129
|
+
expect(eapi.errors.full_messages).to eq ["Something must be a Hash"]
|
130
|
+
expect(eapi.errors.messages).to eq({something: ["must be a Hash"]})
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'valid if value is of the given type' do
|
134
|
+
eapi = MyTestClassValTypeSymbol.new something: {}
|
135
|
+
expect(eapi).to be_valid
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'valid if value is not an instance of the given type but responds true to `.is?(type)`' do
|
139
|
+
eapi = MyTestClassValTypeSymbol.new something: SimilarToHash.new
|
140
|
+
expect(eapi).to be_valid
|
141
|
+
end
|
109
142
|
end
|
110
143
|
|
111
|
-
|
112
|
-
|
113
|
-
|
144
|
+
|
145
|
+
describe 'using a string as type' do
|
146
|
+
class MyTestClassValTypeString
|
147
|
+
include Eapi::Common
|
148
|
+
|
149
|
+
property :something, type: 'Hash'
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'invalid if value is not of that type' do
|
153
|
+
eapi = MyTestClassValTypeString.new something: 1
|
154
|
+
expect(eapi).not_to be_valid
|
155
|
+
expect(eapi.errors.full_messages).to eq ["Something must be a Hash"]
|
156
|
+
expect(eapi.errors.messages).to eq({something: ["must be a Hash"]})
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'valid if value is of the given type' do
|
160
|
+
eapi = MyTestClassValTypeString.new something: {}
|
161
|
+
expect(eapi).to be_valid
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'valid if value is not an instance of the given type but responds true to `.is?(type)`' do
|
165
|
+
eapi = MyTestClassValTypeString.new something: SimilarToHash.new
|
166
|
+
expect(eapi).to be_valid
|
167
|
+
end
|
114
168
|
end
|
115
169
|
end
|
116
170
|
end
|