eapi 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.
@@ -5,18 +5,18 @@ RSpec.describe Eapi do
5
5
  context 'validations' do
6
6
  describe '#valid?' do
7
7
  it 'true if no validations' do
8
- class Eapi::MyTestClassVal
8
+ class MyTestClassVal
9
9
  include Eapi::Common
10
10
 
11
11
  property :something
12
12
  end
13
13
 
14
- eapi = Eapi::MyTestClassVal.new something: :hey
14
+ eapi = MyTestClassVal.new something: :hey
15
15
  expect(eapi).to be_valid
16
16
  end
17
17
 
18
18
  it 'false if validations not met' do
19
- class Eapi::MyTestClassVal2
19
+ class MyTestClassVal2
20
20
  include Eapi::Common
21
21
 
22
22
  property :something
@@ -24,7 +24,7 @@ RSpec.describe Eapi do
24
24
  validates_presence_of :something
25
25
  end
26
26
 
27
- eapi = Eapi::MyTestClassVal2.new
27
+ eapi = MyTestClassVal2.new
28
28
  expect(eapi).not_to be_valid
29
29
  expect(eapi.errors.full_messages).to eq ["Something can't be blank"]
30
30
  expect(eapi.errors.messages).to eq({something: ["can't be blank"]})
@@ -32,33 +32,20 @@ RSpec.describe Eapi do
32
32
  end
33
33
 
34
34
  it 'if required, same as validate presence' do
35
- class Eapi::MyTestClassVal3
35
+ class MyTestClassVal3
36
36
  include Eapi::Common
37
37
 
38
38
  property :something, required: true
39
39
  end
40
40
 
41
- eapi = Eapi::MyTestClassVal3.new
41
+ eapi = MyTestClassVal3.new
42
42
  expect(eapi).not_to be_valid
43
43
  expect(eapi.errors.full_messages).to eq ["Something can't be blank"]
44
44
  expect(eapi.errors.messages).to eq({something: ["can't be blank"]})
45
45
  end
46
46
 
47
- it 'if type specified with a class, validates it' do
48
- class Eapi::MyTestClassVal3
49
- include Eapi::Common
50
-
51
- property :something, type: Hash
52
- end
53
-
54
- eapi = Eapi::MyTestClassVal3.new something: 1
55
- expect(eapi).not_to be_valid
56
- expect(eapi.errors.full_messages).to eq ["Something must be a Hash"]
57
- expect(eapi.errors.messages).to eq({something: ["must be a Hash"]})
58
- end
59
-
60
47
  it 'if validate_with: specified with a class, uses it to validate the property' do
61
- class Eapi::MyTestClassVal4
48
+ class MyTestClassVal4
62
49
  include Eapi::Common
63
50
 
64
51
  property :something, validate_with: ->(record, attr, value) do
@@ -66,7 +53,7 @@ RSpec.describe Eapi do
66
53
  end
67
54
  end
68
55
 
69
- eapi = Eapi::MyTestClassVal4.new something: 1
56
+ eapi = MyTestClassVal4.new something: 1
70
57
 
71
58
  expect(eapi).not_to be_valid
72
59
  expect(eapi.errors.full_messages).to eq ["Something must pass my custom validation"]
@@ -75,6 +62,57 @@ RSpec.describe Eapi do
75
62
  eapi.something :valid_val
76
63
  expect(eapi).to be_valid
77
64
  end
65
+
66
+ it 'normal ActiveModel::Validations can be used' do
67
+ class MyTestClassVal5
68
+ include Eapi::Common
69
+
70
+ property :something
71
+
72
+ validates :something, numericality: true
73
+
74
+ end
75
+
76
+ eapi = MyTestClassVal5.new something: 'something'
77
+
78
+ expect(eapi).not_to be_valid
79
+ expect(eapi.errors.full_messages).to eq ["Something is not a number"]
80
+ expect(eapi.errors.messages).to eq({something: ["is not a number"]})
81
+
82
+ eapi.something 1
83
+ expect(eapi).to be_valid
84
+ end
85
+
86
+ context 'type is specified with a class' do
87
+ class SimilarToHash
88
+ def is?(type)
89
+ [:SimilarToHash, :Hash].include? type.to_s.to_sym
90
+ end
91
+ end
92
+
93
+ class MyTestClassValType
94
+ include Eapi::Common
95
+
96
+ property :something, type: Hash
97
+ end
98
+
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"]})
104
+ end
105
+
106
+ it 'valid if value is of the given type' do
107
+ eapi = MyTestClassValType.new something: {}
108
+ expect(eapi).to be_valid
109
+ end
110
+
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
114
+ end
115
+ end
78
116
  end
79
117
 
80
118
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eapi
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
  - Eduardo Turiño
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-28 00:00:00.000000000 Z
11
+ date: 2014-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -136,20 +136,6 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
- - !ruby/object:Gem::Dependency
140
- name: coveralls
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - ">="
144
- - !ruby/object:Gem::Version
145
- version: '0'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - ">="
151
- - !ruby/object:Gem::Version
152
- version: '0'
153
139
  - !ruby/object:Gem::Dependency
154
140
  name: activesupport
155
141
  requirement: !ruby/object:Gem::Requirement
@@ -185,7 +171,6 @@ executables: []
185
171
  extensions: []
186
172
  extra_rdoc_files: []
187
173
  files:
188
- - ".coveralls.yml"
189
174
  - ".gitignore"
190
175
  - ".rspec"
191
176
  - ".travis.yml"
@@ -195,7 +180,9 @@ files:
195
180
  - Rakefile
196
181
  - eapi.gemspec
197
182
  - lib/eapi.rb
183
+ - lib/eapi/children.rb
198
184
  - lib/eapi/common.rb
185
+ - lib/eapi/definition_runner.rb
199
186
  - lib/eapi/errors.rb
200
187
  - lib/eapi/methods.rb
201
188
  - lib/eapi/methods/accessor.rb
@@ -203,12 +190,17 @@ files:
203
190
  - lib/eapi/methods/properties.rb
204
191
  - lib/eapi/methods/types.rb
205
192
  - lib/eapi/multiple.rb
193
+ - lib/eapi/type_checker.rb
194
+ - lib/eapi/value_converter.rb
206
195
  - lib/eapi/version.rb
207
- - spec/eapi_basic_spec.rb
208
- - spec/eapi_list_spec.rb
209
- - spec/eapi_to_h_spec.rb
210
- - spec/eapi_validations_spec.rb
196
+ - spec/basic_spec.rb
197
+ - spec/definition_spec.rb
198
+ - spec/function_spec.rb
199
+ - spec/list_spec.rb
211
200
  - spec/spec_helper.rb
201
+ - spec/to_h_spec.rb
202
+ - spec/type_spec.rb
203
+ - spec/validations_spec.rb
212
204
  homepage: ''
213
205
  licenses:
214
206
  - MIT
@@ -235,9 +227,12 @@ specification_version: 4
235
227
  summary: ruby gem for building complex structures that will end up in hashes (initially
236
228
  devised for ElasticSearch search requests)
237
229
  test_files:
238
- - spec/eapi_basic_spec.rb
239
- - spec/eapi_list_spec.rb
240
- - spec/eapi_to_h_spec.rb
241
- - spec/eapi_validations_spec.rb
230
+ - spec/basic_spec.rb
231
+ - spec/definition_spec.rb
232
+ - spec/function_spec.rb
233
+ - spec/list_spec.rb
242
234
  - spec/spec_helper.rb
235
+ - spec/to_h_spec.rb
236
+ - spec/type_spec.rb
237
+ - spec/validations_spec.rb
243
238
  has_rdoc:
data/.coveralls.yml DELETED
@@ -1 +0,0 @@
1
- service_name: travis-ci
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Eapi do
4
-
5
- context 'basic behaviour' do
6
- class Eapi::MyTestKlass
7
- include Eapi::Common
8
-
9
- property :something
10
- end
11
-
12
- describe '#something (fluent setter/getter)' do
13
- describe '#something as getter' do
14
- it 'return the value' do
15
- eapi = Eapi::MyTestKlass.new something: :hey
16
- expect(eapi.something).to eq :hey
17
- end
18
- end
19
-
20
- describe '#something("val")' do
21
- it 'set the value and return self' do
22
- eapi = Eapi::MyTestKlass.new something: :hey
23
- res = eapi.something :other
24
- expect(eapi).to be res
25
- expect(eapi.something).to eq :other
26
- end
27
- end
28
- end
29
-
30
- describe 'Eapi.MyTestKlass(...)' do
31
- it 'calls EapiTestClass.new' do
32
- eapi = Eapi.MyTestKlass(something: :hey)
33
- expect(eapi).to be_a Eapi::MyTestKlass
34
- expect(eapi.something).to eq :hey
35
- end
36
- end
37
- end
38
-
39
- end
@@ -1,92 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Eapi do
4
-
5
- context 'list' do
6
- class Eapi::MyTestClassValMult
7
- include Eapi::Common
8
-
9
- property :something, multiple: true
10
- end
11
-
12
- it '#add_something' do
13
- eapi = Eapi::MyTestClassValMult.new something: [1, 2]
14
- res = eapi.add_something 3
15
- expect(res).to be eapi
16
- expect(eapi.something).to eq [1, 2, 3]
17
- end
18
-
19
- it '#init_something called on first add if element is nil' do
20
- eapi = Eapi::MyTestClassValMult.new
21
- res = eapi.add_something :a
22
- expect(res).to be eapi
23
- expect(eapi.something.to_a).to eq [:a]
24
- end
25
-
26
- class Eapi::MyTestClassValMultImpl
27
- include Eapi::Common
28
-
29
- property :something, type: Set
30
- end
31
-
32
- class MyMultiple
33
- def self.is_multiple?
34
- true
35
- end
36
-
37
- def <<(x)
38
- @elements ||= []
39
- @elements << x
40
- end
41
-
42
- def to_a
43
- @elements.to_a
44
- end
45
- end
46
-
47
- class Eapi::MyTestClassValMultImpl2
48
- include Eapi::Common
49
-
50
- property :something, type: MyMultiple
51
- end
52
-
53
- class MyMultipleEapi
54
- include Eapi::Multiple
55
-
56
- def <<(x)
57
- @elements ||= []
58
- @elements << x
59
- end
60
-
61
- def to_a
62
- @elements.to_a
63
- end
64
- end
65
-
66
- class Eapi::MyTestClassValMultImpl3
67
- include Eapi::Common
68
-
69
- property :something, type: MyMultipleEapi
70
- end
71
-
72
- it 'if type is Array or Set, or responds true to is_multiple?, it is multiple implicitly + uses that class to initialize the property when adding' do
73
- [
74
- [Eapi::MyTestClassValMult, Array],
75
- [Eapi::MyTestClassValMultImpl, Set],
76
- [Eapi::MyTestClassValMultImpl2, MyMultiple],
77
- [Eapi::MyTestClassValMultImpl3, MyMultipleEapi],
78
- ].each do |(eapi_class, type_class)|
79
- eapi = eapi_class.new
80
- res = eapi.add_something :a
81
- expect(res).to be eapi
82
- expect(eapi.something.to_a).to eq [:a]
83
- expect(eapi.something).to be_a_kind_of type_class
84
- end
85
- end
86
-
87
- it 'validate elements' do
88
- skip 'test TBD'
89
- end
90
- end
91
-
92
- end
@@ -1,42 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Eapi do
4
-
5
- context '#to_h' do
6
- class Eapi::MyTestClassToH
7
- include Eapi::Common
8
-
9
- property :something, required: true
10
- property :other
11
- end
12
-
13
- it 'raise error if invalid' do
14
- eapi = Eapi::MyTestClassToH.new
15
- expect { eapi.to_h }.to raise_error do |error|
16
- expect(error).to be_a_kind_of Eapi::Errors::InvalidElementError
17
- expect(error.message).to be_start_with "errors: [\"Something can't be blank\"], self: #<Eapi::MyTestClassToH"
18
- end
19
-
20
- end
21
-
22
- it 'create a hash with elements (calling to_h to each element), avoiding nils' do
23
- eapi = Eapi::MyTestClassToH.new
24
- eapi.something 'hi'
25
- expected = {something: 'hi'}
26
- expect(eapi.to_h).to eq expected
27
-
28
- class MyTestObject
29
- def to_h
30
- 'hello'
31
- end
32
- end
33
-
34
- element = MyTestObject.new
35
- eapi = Eapi::MyTestClassToH.new
36
- eapi.something(element).other(true)
37
- expected = {something: 'hello', other: true}
38
-
39
- expect(eapi.to_h).to eq expected
40
- end
41
- end
42
- end