objection 1.3.1 → 1.4.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 +4 -4
- data/README.md +17 -3
- data/lib/objection/version.rb +1 -1
- data/lib/objection.rb +8 -0
- data/spec/objection/objection_spec.rb +25 -4
- data/spec/support/demo_leasurely.rb +3 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aff7ba240a5dab954c6fd26b32283011979c2c4d
|
4
|
+
data.tar.gz: 3d3a10950c6cf4039ca23550f68b1e900ed11e0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eae990b088391861e2818d3576e872992e80b53261c1bd02765c5fcf14684b41612f059fa1f21a55c4ea7b901e1b1324c303fbb80f6fd7a92d0b21441d8a9db
|
7
|
+
data.tar.gz: 3ede58e66e4557b9ca78f9dbd80d4152f1d378c77cd76ab9e841b7145fec7a4b615818b23ec54b033ed646bf649a7cbd724eba4019869fcd8487c2f657f652ac
|
data/README.md
CHANGED
@@ -85,13 +85,27 @@ obj.car.is_a?(DemoNestedCar)
|
|
85
85
|
obj.car.car_model == "<value given via ['car']['car_model']>"
|
86
86
|
```
|
87
87
|
|
88
|
-
This also works for arrays. When an array is given where an object is suspected, then each item within the array will be
|
89
|
-
|
88
|
+
This also works for arrays. When an array is given where an object is suspected, then each item within the array will be converted into the declared object.
|
89
|
+
|
90
|
+
## Leasurely mode
|
91
|
+
When Objection is instantiated with unknown fields in the parameters (a field not declared as optional or required), then an error will be thrown, because the contract is broken.
|
92
|
+
But sometimes that is just how the world works, and the contract has to be breakable.
|
93
|
+
Introduce the Leasure-mode, which can be set per contract by inheriting from the Leasurely-module:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
class DemoLeasurely < Objection::Leasurely::Base
|
97
|
+
requires :required_1
|
98
|
+
end
|
99
|
+
|
100
|
+
obj = DemoLeasurely.new(required_1: 'dummy', unknown_1: 'dummy')
|
101
|
+
```
|
102
|
+
The instantiation above will not raise an error.
|
103
|
+
**Beware**: the field 'unknown_1' will not be referencable via a getter-function.
|
90
104
|
|
91
105
|
## To hash
|
92
106
|
|
93
107
|
For better connection with other services, objection can convert its values to an hash, with the `to_hash` function.
|
94
|
-
This operation
|
108
|
+
This operation works recursivly with nested objects and arrays of objects.
|
95
109
|
|
96
110
|
## Contributing
|
97
111
|
|
data/lib/objection/version.rb
CHANGED
data/lib/objection.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
require_relative '../support/demo_basic'
|
3
|
+
require_relative '../support/demo_leasurely'
|
3
4
|
require_relative '../support/demo_requires'
|
4
5
|
require_relative '../support/demo_optionals'
|
5
6
|
require_relative '../support/demo_typed'
|
@@ -80,6 +81,26 @@ describe Objection do
|
|
80
81
|
end
|
81
82
|
end
|
82
83
|
|
84
|
+
context 'unknown fields in leasurely-mode' do
|
85
|
+
it 'can be defined and fetched' do
|
86
|
+
obj = DemoLeasurely.new(required_1: 'dummy')
|
87
|
+
|
88
|
+
expect(obj).to be_kind_of(DemoLeasurely)
|
89
|
+
expect(obj).to be_kind_of(Objection::Leasurely::Base)
|
90
|
+
expect(obj.send(:required_fields)).to eq([:required_1])
|
91
|
+
expect(obj.send(:optional_fields)).to eq([])
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'raises no error, if an unknown field is given' do
|
95
|
+
obj = DemoLeasurely.new(required_1: 'dummy', unknown_1: 'dummy')
|
96
|
+
|
97
|
+
expect(obj).to be_kind_of(DemoLeasurely)
|
98
|
+
expect(obj).to be_kind_of(Objection::Leasurely::Base)
|
99
|
+
expect(obj.send(:required_fields)).to eq([:required_1])
|
100
|
+
expect(obj.send(:optional_fields)).to eq([])
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
83
104
|
context 'typed_input' do
|
84
105
|
let(:obj) { build_object_for_known_fields }
|
85
106
|
|
@@ -159,19 +180,19 @@ describe Objection do
|
|
159
180
|
end
|
160
181
|
|
161
182
|
it 'raises an error. when unknown fields are supplied' do
|
162
|
-
expect{DemoOptionals.new(unknown_field: 'dummy')}.to raise_error(Objection::Base::UnknownFieldGiven, 'unknown_field')
|
183
|
+
expect{DemoOptionals.new(unknown_field: 'dummy')}.to raise_error(Objection::Base::UnknownFieldGiven, 'unknown_field for class: DemoOptionals')
|
163
184
|
end
|
164
185
|
|
165
186
|
it 'raises an error. when not all required fields are supplied' do
|
166
|
-
expect{DemoRequires.new(required_2: 'dummy')}.to raise_error(Objection::Base::RequiredFieldMissing, 'required_1')
|
187
|
+
expect{DemoRequires.new(required_2: 'dummy')}.to raise_error(Objection::Base::RequiredFieldMissing, 'required_1 for class: DemoRequires')
|
167
188
|
end
|
168
189
|
|
169
190
|
it 'raises an error, when a required field is blank' do
|
170
|
-
expect{DemoRequires.new(required_1: 'value', required_2: '')}.to raise_error(Objection::Base::RequiredFieldEmpty, 'required_2')
|
191
|
+
expect{DemoRequires.new(required_1: 'value', required_2: '')}.to raise_error(Objection::Base::RequiredFieldEmpty, 'required_2 for class: DemoRequires')
|
171
192
|
end
|
172
193
|
|
173
194
|
it 'raises an error, when a required field is nil' do
|
174
|
-
expect{DemoRequires.new(required_1: nil, required_2: 'value')}.to raise_error(Objection::Base::RequiredFieldEmpty, 'required_1')
|
195
|
+
expect{DemoRequires.new(required_1: nil, required_2: 'value')}.to raise_error(Objection::Base::RequiredFieldEmpty, 'required_1 for class: DemoRequires')
|
175
196
|
end
|
176
197
|
end
|
177
198
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: objection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ewout Quax
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- spec/objection/objection_spec.rb
|
100
100
|
- spec/spec_helper.rb
|
101
101
|
- spec/support/demo_basic.rb
|
102
|
+
- spec/support/demo_leasurely.rb
|
102
103
|
- spec/support/demo_nested.rb
|
103
104
|
- spec/support/demo_optionals.rb
|
104
105
|
- spec/support/demo_requires.rb
|
@@ -123,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
124
|
version: '0'
|
124
125
|
requirements: []
|
125
126
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
127
|
+
rubygems_version: 2.4.6
|
127
128
|
signing_key:
|
128
129
|
specification_version: 4
|
129
130
|
summary: Declare a predefined contract for use with services
|
@@ -131,6 +132,7 @@ test_files:
|
|
131
132
|
- spec/objection/objection_spec.rb
|
132
133
|
- spec/spec_helper.rb
|
133
134
|
- spec/support/demo_basic.rb
|
135
|
+
- spec/support/demo_leasurely.rb
|
134
136
|
- spec/support/demo_nested.rb
|
135
137
|
- spec/support/demo_optionals.rb
|
136
138
|
- spec/support/demo_requires.rb
|