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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ce4210341572090ddcab6a924eef6512c26790a8
4
- data.tar.gz: 1773ccd9aa977cd193f4efd802cc1a11abe6d2b4
3
+ metadata.gz: f8a6da6f094409d07aa648318c1134b587bee426
4
+ data.tar.gz: c9d3e9715481dc3d1be43324fef7f81e860f3f06
5
5
  SHA512:
6
- metadata.gz: 2c9beaa7358443dee5acdc658dd422a679376c2a29af2db1d6efdc780c47020ea8fc69c2aa2bdeb2e8d5c9b098544fd1a6bf1cb785eb18cc38ce85c9b588f682
7
- data.tar.gz: f4d071de2e5e10e4300509088e3f60fcd6d173a1492766f95eab6600bb991ae28e77eda3b2d0484ed003a51432c5f2480700bec076b17804c85934d7c1856f78
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
@@ -1,5 +1,5 @@
1
1
  module Eapi
2
- class TypeChecker < Struct.new(:type)
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
@@ -1,3 +1,3 @@
1
1
  module Eapi
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
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 OtherType
34
- expect(MyTestTypeKlass).to be_is :OtherType
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 OtherType
55
- expect(obj).to be_is :OtherType
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
@@ -90,27 +90,81 @@ RSpec.describe Eapi do
90
90
  end
91
91
  end
92
92
 
93
- class MyTestClassValType
94
- include Eapi::Common
93
+ describe 'using a class as type' do
94
+ class MyTestClassValType
95
+ include Eapi::Common
95
96
 
96
- property :something, type: Hash
97
- end
97
+ property :something, type: Hash
98
+ end
98
99
 
99
- it 'invalid if value is not of that type' do
100
- eapi = MyTestClassValType.new something: 1
101
- expect(eapi).not_to be_valid
102
- expect(eapi.errors.full_messages).to eq ["Something must be a Hash"]
103
- expect(eapi.errors.messages).to eq({something: ["must be a Hash"]})
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
- it 'valid if value is of the given type' do
107
- eapi = MyTestClassValType.new something: {}
108
- expect(eapi).to be_valid
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
- it 'valid if value is not an instance of the given type but responds true to `.is?(type)`' do
112
- eapi = MyTestClassValType.new something: SimilarToHash.new
113
- expect(eapi).to be_valid
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño