definition 0.1.0.rc1 → 0.1.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/README.md +39 -1
- data/lib/definition.rb +1 -0
- data/lib/definition/types/keys.rb +4 -0
- data/lib/definition/value_object.rb +38 -0
- data/lib/definition/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3af4089033df18151f417df6667ab1f76b266d35728297abc1ead799bb25dab9
|
4
|
+
data.tar.gz: 82e3b50fc7e7bdafd0317669c6960924627808f6ae19086a383ab91b3d3ead8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee6a8ef92b6844cbe89b9e18bccb10b12b4068f7726d73c1d24296873b61cf60dc2478da1948a8350017c7be868c466dce4a6a2482e2672a0959567f8fd8e7e5
|
7
|
+
data.tar.gz: f8ed3f1933198bf18f750334fdd1662666c795dc2dbb80fbf84680c62dce436670ff8e4904bc32c77926e6ee0de0dc189eef304eac1a94f1c802c486b061f836
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[![Build Status](https://travis-ci.org/Goltergaul/definition.svg?branch=master)][travis]
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/definition.svg)][rubygems]
|
5
5
|
|
6
|
-
Simple and composable validation and coercion of data structures
|
6
|
+
Simple and composable validation and coercion of data structures. It also includes a ValueObject for convinience.
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -63,6 +63,44 @@ conform_result.value # => {title: "My first blog post", body: "Shortest one ever
|
|
63
63
|
Because definitions do not only validate input but also transform input, we use
|
64
64
|
the term `conform` which stands for validation and coercion.
|
65
65
|
|
66
|
+
### Value Objects
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class User < Definition::ValueObject
|
70
|
+
definition(Definition.Keys do
|
71
|
+
required :username, Definition.Type(String)
|
72
|
+
required :password, Definition.Type(String)
|
73
|
+
end)
|
74
|
+
end
|
75
|
+
|
76
|
+
user = User.new(username: "johndoe", password: "zg(2ds8x2/")
|
77
|
+
user.username # => "johndoe"
|
78
|
+
user[:username] # => "johndoe"
|
79
|
+
user.username = "Alice" # => NoMethodError (ValueObjects are immutable)
|
80
|
+
user[:username] = "Alice" # => FrozenError (ValueObjects are immutable)
|
81
|
+
|
82
|
+
User.new(username: "johndoe") # => Definition::InvalidValueObjectError: hash does not include :last_name
|
83
|
+
```
|
84
|
+
|
85
|
+
Value objects delegate all calls to the output value of the defined definition,
|
86
|
+
so in this example you can use all methods that are defined on `Hash` also on the
|
87
|
+
user object. If you use a `Keys` definition, the value object additionally defines
|
88
|
+
convenient accessor methods for all attributes.
|
89
|
+
|
90
|
+
Value Objects can also be used for all other data structures that can be validated
|
91
|
+
by a definition, for example arrays:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
class IntegerArray < Definition::ValueObject
|
95
|
+
definition(Definition.Each(Definition.Type(Integer)))
|
96
|
+
end
|
97
|
+
|
98
|
+
array = IntegerArray.new([1,2,3])
|
99
|
+
array.first # => 1
|
100
|
+
|
101
|
+
IntegerArray.new([1,2,"3"]) # => Definition::InvalidValueObjectError: Not all items conform with each: { Item "3" did not conform to each: { Is of type String instead of Integer } }
|
102
|
+
```
|
103
|
+
|
66
104
|
### Conforming Hashes
|
67
105
|
|
68
106
|
Hashes can be conformed by using the `Keys` definition. It allows you to configure
|
data/lib/definition.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Definition
|
4
|
+
class InvalidValueObjectError < StandardError; end
|
5
|
+
class NotConfiguredError < StandardError; end
|
6
|
+
|
7
|
+
class ValueObject < SimpleDelegator
|
8
|
+
def initialize(args = nil, **kwargs)
|
9
|
+
result = self.class.conform(args || kwargs)
|
10
|
+
raise InvalidValueObjectError.new(result.error_message) unless result.passed?
|
11
|
+
|
12
|
+
super(result.value.freeze)
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def conform(value)
|
17
|
+
unless @definition
|
18
|
+
raise Definition::NotConfiguredError.new(
|
19
|
+
"Value object has not been configured with a defintion. Use .definition to set a definition"
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
@definition.conform(value)
|
24
|
+
end
|
25
|
+
|
26
|
+
def definition(definition)
|
27
|
+
@definition = definition
|
28
|
+
define_accessor_methods if definition.is_a?(Definition::Types::Keys)
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_accessor_methods
|
32
|
+
@definition.keys.each do |key|
|
33
|
+
define_method(key) { self[key] }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/definition/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: definition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.rc2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dominik Goltermann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: approvals
|
@@ -252,6 +252,7 @@ files:
|
|
252
252
|
- lib/definition/types/lambda.rb
|
253
253
|
- lib/definition/types/or.rb
|
254
254
|
- lib/definition/types/type.rb
|
255
|
+
- lib/definition/value_object.rb
|
255
256
|
- lib/definition/version.rb
|
256
257
|
homepage: https://github.com/Goltergaul/definition
|
257
258
|
licenses:
|