blood_contracts-core 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/blood_contracts-core.gemspec +31 -0
- data/examples/json_response.rb +92 -0
- data/lib/blood_contracts-core.rb +1 -0
- data/lib/blood_contracts/core.rb +30 -0
- data/lib/blood_contracts/core/contract.rb +18 -0
- data/lib/blood_contracts/core/type.rb +197 -0
- data/lib/blood_contracts/core/version.rb +5 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4d50ef9d60bc174f486385734c98d18ade0fa6a21cdc809d0a3bead98c044aac
|
4
|
+
data.tar.gz: c55c10f1248097678f6feaab0bfd055494c2de7da69b663ec2458b9449b8b560
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 469b03faa75fa6a46448aee7592cbbabe2a4ad53c5dca11c7395e67a9fdfcfc6c4bc4364afde47440778befaf78344f856b65f3b6c1ad0c267d1943629152c53
|
7
|
+
data.tar.gz: 4354966854dc2cacaf189fec6531555e5f5784b915e34757db40d896fd095f9666a58b62dacd00874b41b467557abf0fd83dc6fdd7395a0d2353b154e4e5ba46
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at sclinede@evilmartians.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Sergey Dolganov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# BloodContracts::Core
|
2
|
+
|
3
|
+
Simple implementation of Refinement Types and Contract (based on types).
|
4
|
+
Would be the basement for the BloodContracts API production testing framework.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'blood_contracts-core'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install blood_contracts-core
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
TODO: Write usage instructions here
|
25
|
+
|
26
|
+
## Development
|
27
|
+
|
28
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
29
|
+
|
30
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sclinede/blood_contracts-core. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
39
|
+
|
40
|
+
## Code of Conduct
|
41
|
+
|
42
|
+
Everyone interacting in the BloodContracts::Core project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sclinede/blood_contracts-core/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "blood_contracts/core"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "blood_contracts/core/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "blood_contracts-core"
|
8
|
+
spec.version = BloodContracts::Core::VERSION
|
9
|
+
spec.authors = ["Sergey Dolganov"]
|
10
|
+
spec.email = ["sclinede@evilmartians.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Core classes for Contracts API validation}
|
13
|
+
spec.description = %q{Core classes for Contracts API validation}
|
14
|
+
spec.homepage = "https://github.com/sclinede/blood_contracts-core"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "bin/"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "dry-initializer", "~> 2.0"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'blood_contracts/core'
|
3
|
+
|
4
|
+
module Types
|
5
|
+
class JSON < BloodType
|
6
|
+
param :json_string
|
7
|
+
|
8
|
+
def match
|
9
|
+
@parsed = ::JSON.parse(unpack_refined(json_string))
|
10
|
+
self
|
11
|
+
rescue StandardError => error
|
12
|
+
@errors << error
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def unpack
|
17
|
+
@parsed
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Tariff < BloodType
|
22
|
+
param :json
|
23
|
+
|
24
|
+
def match
|
25
|
+
super do
|
26
|
+
@parsed = unpack_refined(self.json).slice("cost", "cur").compact
|
27
|
+
@errors << :not_a_tariff if @parsed.size != 2
|
28
|
+
self
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Error < BloodType
|
34
|
+
param :json
|
35
|
+
|
36
|
+
def match
|
37
|
+
super do
|
38
|
+
@parsed = unpack_refined(self.json).slice("code", "message").compact
|
39
|
+
@errors << :not_an_error if @parsed.size != 2
|
40
|
+
self
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Response < BloodType
|
46
|
+
option :raw, BC::Anything.method(:new)
|
47
|
+
option :parsed, JSON.method(:new), default: -> { raw }
|
48
|
+
option :mapped, (Tariff | Error).method(:new), default: -> { parsed }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def match_response(response)
|
53
|
+
case (match = Types::Response.match(raw: response))
|
54
|
+
when BC::ContractFailure
|
55
|
+
puts "Honeybadger.notify 'Unexpected behavior in Russian Post', context: #{match.context}"
|
56
|
+
puts "render json: { errors: 'Ooops! Not working, we've been notified. Please, try again later' }"
|
57
|
+
|
58
|
+
return unless ENV["RAISE"]
|
59
|
+
match.errors.values.flatten.find do |v|
|
60
|
+
raise v if StandardError === v
|
61
|
+
end
|
62
|
+
when Types::Tariff
|
63
|
+
# работаем с тарифом
|
64
|
+
puts "render json: { tariff: #{match.unpack.mapped} }"
|
65
|
+
when Types::Error
|
66
|
+
# работаем с ошибкой, e.g. адрес слишком длинный
|
67
|
+
puts "render json: { errors: [#{match.unpack.mapped.unpack['message']}] } }"
|
68
|
+
else
|
69
|
+
binding.irb
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
puts "#{'=' * 20}================================#{'=' * 20}"
|
74
|
+
puts "\n\n\n"
|
75
|
+
puts "#{'=' * 20} WHEN VALID RESPONSE: #{'=' * 20}"
|
76
|
+
valid_response = '{"cost": 1000, "cur": "RUB"}'
|
77
|
+
match_response(valid_response)
|
78
|
+
puts "#{'=' * 20}================================#{'=' * 20}"
|
79
|
+
|
80
|
+
|
81
|
+
puts "\n\n\n"
|
82
|
+
puts "#{'=' * 20} WHEN KNOWN API ERROR RESPONSE: #{'=' * 20}"
|
83
|
+
error_response = '{"code": 123, "message": "Too Long Address"}'
|
84
|
+
match_response(error_response)
|
85
|
+
puts "#{'=' * 20}================================#{'=' * 20}"
|
86
|
+
|
87
|
+
|
88
|
+
puts "\n\n\n"
|
89
|
+
puts "#{'=' * 20} WHEN UNEXPECTED RESPONSE: #{'=' * 20}"
|
90
|
+
invalid_response = '<xml>'
|
91
|
+
match_response(invalid_response)
|
92
|
+
puts "#{'=' * 20}================================#{'=' * 20}"
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative "./blood_contracts/core"
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "dry-initializer"
|
2
|
+
require_relative "./core/contract.rb"
|
3
|
+
require_relative "./core/type.rb"
|
4
|
+
require_relative "./core/version.rb"
|
5
|
+
|
6
|
+
BloodContract = BloodContracts::Core::Contract
|
7
|
+
BloodType = BloodContracts::Core::Type
|
8
|
+
|
9
|
+
module BloodContracts
|
10
|
+
module Core; end
|
11
|
+
|
12
|
+
class ContractFailure < Core::Type
|
13
|
+
def errors
|
14
|
+
context[:errors].to_h
|
15
|
+
end
|
16
|
+
|
17
|
+
def unpack
|
18
|
+
context
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Anything < Core::Type
|
23
|
+
param :data
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module BC
|
28
|
+
Anything = BloodContracts::Anything
|
29
|
+
ContractFailure = BloodContracts::ContractFailure
|
30
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module BloodContracts
|
2
|
+
module Core
|
3
|
+
class Contract
|
4
|
+
class << self
|
5
|
+
attr_accessor :input_type, :output_type
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.call(*args)
|
9
|
+
if (input_match = input_type.match(*args)).valid?
|
10
|
+
result = yield(input_match)
|
11
|
+
output_type.match(result, context: input_match.context)
|
12
|
+
else
|
13
|
+
input_match
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,197 @@
|
|
1
|
+
module BloodContracts
|
2
|
+
module Core
|
3
|
+
class Type
|
4
|
+
extend Dry::Initializer
|
5
|
+
|
6
|
+
# TODO: share context between Refinement Types match
|
7
|
+
class << self
|
8
|
+
def attributes
|
9
|
+
@attributes ||= []
|
10
|
+
end
|
11
|
+
|
12
|
+
def param(name, *)
|
13
|
+
@attributes = attributes | [name.to_sym]
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def option(name, type = nil, as: name, **)
|
18
|
+
@attributes = attributes | [as.to_sym]
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
def single?
|
23
|
+
attributes.size <= 1
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_reader :sum
|
27
|
+
def or_a(other_type)
|
28
|
+
c = Class.new(Anything)
|
29
|
+
c.instance_variable_set(
|
30
|
+
:@sum, Set.new([self] + self.sum.to_a << other_type)
|
31
|
+
)
|
32
|
+
c
|
33
|
+
end
|
34
|
+
alias :or_an :or_a
|
35
|
+
alias :| :or_a
|
36
|
+
|
37
|
+
def match(*args)
|
38
|
+
new(*args).match
|
39
|
+
end
|
40
|
+
alias :call :match
|
41
|
+
|
42
|
+
def ===(object)
|
43
|
+
return true if (result = super)
|
44
|
+
return result unless object.class < BloodContracts::Core::Type
|
45
|
+
if object.class.send(:single?)
|
46
|
+
return result if object.class.sum.to_a.empty?
|
47
|
+
return object.class.sum.any? { |or_type| self == or_type }
|
48
|
+
end
|
49
|
+
|
50
|
+
object.attributes.values.any? { |sub_type| self === sub_type }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
attr_accessor :context
|
55
|
+
attr_reader :errors, :value_handler
|
56
|
+
def initialize(*args, **kwargs)
|
57
|
+
@errors = []
|
58
|
+
@context = kwargs.delete(:context) || {}
|
59
|
+
@value_handler = self.class.single? ? Single.new(self) : Tuple.new(self)
|
60
|
+
|
61
|
+
super(*args, **kwargs)
|
62
|
+
end
|
63
|
+
|
64
|
+
def attributes
|
65
|
+
@attributes ||= self.class.dry_initializer.attributes(self)
|
66
|
+
end
|
67
|
+
|
68
|
+
def match
|
69
|
+
return @match if defined? @match
|
70
|
+
return @match = yield if block_given?
|
71
|
+
@match = value_handler.match
|
72
|
+
end
|
73
|
+
alias :call :match
|
74
|
+
|
75
|
+
def valid?
|
76
|
+
value_handler.valid?
|
77
|
+
end
|
78
|
+
def invalid?; !valid?; end
|
79
|
+
|
80
|
+
def unpack
|
81
|
+
value_handler.unpack
|
82
|
+
end
|
83
|
+
|
84
|
+
def single?
|
85
|
+
Single === value_handler
|
86
|
+
end
|
87
|
+
|
88
|
+
def refined?(object)
|
89
|
+
BloodContracts::Core::Type === object
|
90
|
+
end
|
91
|
+
|
92
|
+
def unpack_refined(value)
|
93
|
+
refined?(value) ? value.unpack : value
|
94
|
+
end
|
95
|
+
|
96
|
+
protected
|
97
|
+
|
98
|
+
class ValueHandler
|
99
|
+
def initialize(subject)
|
100
|
+
@subject = subject
|
101
|
+
end
|
102
|
+
|
103
|
+
def errors_by_type(matches)
|
104
|
+
Hash[
|
105
|
+
matches.map(&:class).zip(matches.map(&:errors))
|
106
|
+
].delete_if { |_, errors| errors.empty? }
|
107
|
+
end
|
108
|
+
|
109
|
+
def refined?(object)
|
110
|
+
@subject.refined?(object)
|
111
|
+
end
|
112
|
+
|
113
|
+
def unpack_refined(value)
|
114
|
+
@subject.unpack_refined(value)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class Tuple < ValueHandler
|
119
|
+
def initialize(*)
|
120
|
+
super
|
121
|
+
@wrapper = Struct.new(*@subject.class.attributes, keyword_init: true)
|
122
|
+
end
|
123
|
+
|
124
|
+
def wrap_unpacked(match)
|
125
|
+
@wrapper.new(
|
126
|
+
match.attributes.transform_values(&method(:unpack_refined))
|
127
|
+
)
|
128
|
+
end
|
129
|
+
|
130
|
+
def match
|
131
|
+
failed_match =
|
132
|
+
@subject.attributes.values.lazy.map(&:match).find(&:invalid?)
|
133
|
+
|
134
|
+
if failed_match
|
135
|
+
BloodContracts::ContractFailure.new(
|
136
|
+
context: { errors: errors_by_type([failed_match]) }
|
137
|
+
)
|
138
|
+
else
|
139
|
+
@subject
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def unpack
|
144
|
+
raise "This is not what you're looking for" if invalid?
|
145
|
+
|
146
|
+
wrap_unpacked(@subject.match)
|
147
|
+
end
|
148
|
+
|
149
|
+
def invalid?
|
150
|
+
BloodContracts::ContractFailure === @subject.match
|
151
|
+
end
|
152
|
+
def valid?; !invalid?; end
|
153
|
+
|
154
|
+
def single?
|
155
|
+
false
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
class Single < ValueHandler
|
160
|
+
def match
|
161
|
+
sum = @subject.class.sum.to_a
|
162
|
+
return refined?(value) ? value.match : @subject if sum.empty?
|
163
|
+
|
164
|
+
or_matches = sum.map { |type| type.match(value) }
|
165
|
+
if (match = or_matches.find(&:valid?))
|
166
|
+
match.context.merge!(errors: errors_by_type(or_matches))
|
167
|
+
match
|
168
|
+
else
|
169
|
+
BloodContracts::ContractFailure.new(
|
170
|
+
context: { errors: errors_by_type(or_matches) }
|
171
|
+
)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def valid?
|
176
|
+
@subject.errors.empty?
|
177
|
+
end
|
178
|
+
|
179
|
+
def unpack
|
180
|
+
raise "This is not what you're looking for" unless valid?
|
181
|
+
|
182
|
+
unpack_refined value
|
183
|
+
end
|
184
|
+
|
185
|
+
def single?
|
186
|
+
true
|
187
|
+
end
|
188
|
+
|
189
|
+
private
|
190
|
+
|
191
|
+
def value
|
192
|
+
@value ||= @subject.attributes.values.first
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blood_contracts-core
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Dolganov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin/
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-initializer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Core classes for Contracts API validation
|
70
|
+
email:
|
71
|
+
- sclinede@evilmartians.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- blood_contracts-core.gemspec
|
87
|
+
- examples/json_response.rb
|
88
|
+
- lib/blood_contracts-core.rb
|
89
|
+
- lib/blood_contracts/core.rb
|
90
|
+
- lib/blood_contracts/core/contract.rb
|
91
|
+
- lib/blood_contracts/core/type.rb
|
92
|
+
- lib/blood_contracts/core/version.rb
|
93
|
+
homepage: https://github.com/sclinede/blood_contracts-core
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubygems_version: 3.0.3
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Core classes for Contracts API validation
|
116
|
+
test_files: []
|