value_semantics 3.2.1 → 3.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +82 -0
- data/README.md +242 -72
- data/lib/value_semantics.rb +48 -333
- data/lib/value_semantics/anything.rb +11 -0
- data/lib/value_semantics/array_coercer.rb +23 -0
- data/lib/value_semantics/array_of.rb +18 -0
- data/lib/value_semantics/attribute.rb +100 -0
- data/lib/value_semantics/bool.rb +11 -0
- data/lib/value_semantics/class_methods.rb +34 -0
- data/lib/value_semantics/dsl.rb +112 -0
- data/lib/value_semantics/either.rb +18 -0
- data/lib/value_semantics/hash_coercer.rb +30 -0
- data/lib/value_semantics/hash_of.rb +20 -0
- data/lib/value_semantics/instance_methods.rb +170 -0
- data/lib/value_semantics/monkey_patched.rb +3 -0
- data/lib/value_semantics/range_of.rb +18 -0
- data/lib/value_semantics/recipe.rb +17 -0
- data/lib/value_semantics/struct.rb +19 -0
- data/lib/value_semantics/value_object_coercer.rb +48 -0
- data/lib/value_semantics/version.rb +1 -1
- metadata +81 -14
- data/.gitignore +0 -12
- data/.rspec +0 -2
- data/.travis.yml +0 -25
- data/Gemfile +0 -8
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/bin/test +0 -15
- data/value_semantics.gemspec +0 -34
@@ -0,0 +1,18 @@
|
|
1
|
+
module ValueSemantics
|
2
|
+
class RangeOf
|
3
|
+
attr_reader :subvalidator
|
4
|
+
|
5
|
+
def initialize(subvalidator)
|
6
|
+
@subvalidator = subvalidator
|
7
|
+
end
|
8
|
+
|
9
|
+
def ===(obj)
|
10
|
+
return false unless Range === obj
|
11
|
+
|
12
|
+
# begin or end can be nil, if the range is beginless or endless
|
13
|
+
[obj.begin, obj.end].compact.all? do |element|
|
14
|
+
subvalidator === element
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ValueSemantics
|
2
|
+
#
|
3
|
+
# Contains all the configuration necessary to bake a ValueSemantics module
|
4
|
+
#
|
5
|
+
# @see ValueSemantics.bake_module
|
6
|
+
# @see ClassMethods#value_semantics
|
7
|
+
# @see DSL.run
|
8
|
+
#
|
9
|
+
class Recipe
|
10
|
+
attr_reader :attributes
|
11
|
+
|
12
|
+
def initialize(attributes:)
|
13
|
+
@attributes = attributes
|
14
|
+
freeze
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ValueSemantics
|
2
|
+
#
|
3
|
+
# ValueSemantics equivalent of the Struct class from the Ruby standard
|
4
|
+
# library
|
5
|
+
#
|
6
|
+
class Struct
|
7
|
+
#
|
8
|
+
# Creates a new Class with ValueSemantics mixed in
|
9
|
+
#
|
10
|
+
# @yield a block containing ValueSemantics DSL
|
11
|
+
# @return [Class] the newly created class
|
12
|
+
#
|
13
|
+
def self.new(&block)
|
14
|
+
klass = Class.new
|
15
|
+
klass.include(ValueSemantics.for_attributes(&block))
|
16
|
+
klass
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ValueSemantics
|
2
|
+
#
|
3
|
+
# A coercer for converting hashes into instances of value classes
|
4
|
+
#
|
5
|
+
# The coercer will coerce hash-like values into an instance of the value
|
6
|
+
# class, using the hash for attribute values. It will return non-hash-like
|
7
|
+
# values unchanged. It will also return hash-like values unchanged if they do
|
8
|
+
# not contain all required attributes of the value class.
|
9
|
+
#
|
10
|
+
class ValueObjectCoercer
|
11
|
+
attr_reader :value_class
|
12
|
+
|
13
|
+
def initialize(value_class)
|
14
|
+
@value_class = value_class
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(obj)
|
18
|
+
attrs = coerce_to_attr_hash(obj)
|
19
|
+
if attrs
|
20
|
+
value_class.new(attrs)
|
21
|
+
else
|
22
|
+
obj
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
NOT_FOUND = Object.new.freeze
|
28
|
+
|
29
|
+
def coerce_to_attr_hash(obj)
|
30
|
+
# TODO: this coerces nil to an empty hash, which is probably not ideal.
|
31
|
+
# It should not coerce nil so that Either(X, nil) validator works as
|
32
|
+
# expected. This is a backwards-incomptible change, so save it for a
|
33
|
+
# major version bump
|
34
|
+
return nil unless obj.respond_to?(:to_h)
|
35
|
+
obj_hash = obj.to_h
|
36
|
+
|
37
|
+
{}.tap do |attrs|
|
38
|
+
value_class.value_semantics.attributes.each do |attr_def|
|
39
|
+
name = attr_def.name
|
40
|
+
value = obj_hash.fetch(name) do
|
41
|
+
obj_hash.fetch(name.to_s, NOT_FOUND)
|
42
|
+
end
|
43
|
+
attrs[name] = value unless value.equal?(NOT_FOUND)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: value_semantics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Dalling
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: super_diff
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: mutant-rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +108,48 @@ dependencies:
|
|
94
108
|
- - ">="
|
95
109
|
- !ruby/object:Gem::Version
|
96
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: eceval
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: benchmark-ips
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: allocation_tracer
|
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'
|
97
153
|
description: "\n Generates modules that provide conventional value semantics for
|
98
154
|
a given set of attributes.\n The behaviour is similar to an immutable `Struct`
|
99
155
|
class,\n plus extensible, lightweight validation and coercion.\n "
|
@@ -103,23 +159,35 @@ executables: []
|
|
103
159
|
extensions: []
|
104
160
|
extra_rdoc_files: []
|
105
161
|
files:
|
106
|
-
- ".gitignore"
|
107
|
-
- ".rspec"
|
108
|
-
- ".travis.yml"
|
109
162
|
- CHANGELOG.md
|
110
|
-
- Gemfile
|
111
163
|
- LICENSE.txt
|
112
164
|
- README.md
|
113
|
-
- bin/console
|
114
|
-
- bin/setup
|
115
|
-
- bin/test
|
116
165
|
- lib/value_semantics.rb
|
166
|
+
- lib/value_semantics/anything.rb
|
167
|
+
- lib/value_semantics/array_coercer.rb
|
168
|
+
- lib/value_semantics/array_of.rb
|
169
|
+
- lib/value_semantics/attribute.rb
|
170
|
+
- lib/value_semantics/bool.rb
|
171
|
+
- lib/value_semantics/class_methods.rb
|
172
|
+
- lib/value_semantics/dsl.rb
|
173
|
+
- lib/value_semantics/either.rb
|
174
|
+
- lib/value_semantics/hash_coercer.rb
|
175
|
+
- lib/value_semantics/hash_of.rb
|
176
|
+
- lib/value_semantics/instance_methods.rb
|
177
|
+
- lib/value_semantics/monkey_patched.rb
|
178
|
+
- lib/value_semantics/range_of.rb
|
179
|
+
- lib/value_semantics/recipe.rb
|
180
|
+
- lib/value_semantics/struct.rb
|
181
|
+
- lib/value_semantics/value_object_coercer.rb
|
117
182
|
- lib/value_semantics/version.rb
|
118
|
-
- value_semantics.gemspec
|
119
183
|
homepage: https://github.com/tomdalling/value_semantics
|
120
184
|
licenses:
|
121
185
|
- MIT
|
122
|
-
metadata:
|
186
|
+
metadata:
|
187
|
+
bug_tracker_uri: https://github.com/tomdalling/value_semantics/issues
|
188
|
+
changelog_uri: https://github.com/tomdalling/value_semantics/blob/master/CHANGELOG.md
|
189
|
+
documentation_uri: https://github.com/tomdalling/value_semantics/blob/v3.6.1/README.md
|
190
|
+
source_code_uri: https://github.com/tomdalling/value_semantics
|
123
191
|
post_install_message:
|
124
192
|
rdoc_options: []
|
125
193
|
require_paths:
|
@@ -135,8 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
203
|
- !ruby/object:Gem::Version
|
136
204
|
version: '0'
|
137
205
|
requirements: []
|
138
|
-
|
139
|
-
rubygems_version: 2.7.7
|
206
|
+
rubygems_version: 3.0.8
|
140
207
|
signing_key:
|
141
208
|
specification_version: 4
|
142
209
|
summary: Makes value classes, with lightweight validation and coercion.
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
script: bin/test
|
3
|
-
|
4
|
-
# test old Ruby versions WITHOUT mutation testing
|
5
|
-
rvm:
|
6
|
-
- 2.3.8
|
7
|
-
- 2.4.10
|
8
|
-
- 2.5.8
|
9
|
-
- 2.6.6
|
10
|
-
env: MUTATION_TEST=false
|
11
|
-
|
12
|
-
# test the latest Ruby version WITH mutation testing
|
13
|
-
matrix:
|
14
|
-
include:
|
15
|
-
- rvm: 2.7.1
|
16
|
-
env: MUTATION_TEST=true
|
17
|
-
|
18
|
-
# deploy gem on tagged commits, on the latest Ruby version only
|
19
|
-
deploy:
|
20
|
-
provider: rubygems
|
21
|
-
on:
|
22
|
-
tags: true
|
23
|
-
env: MUTATION_TEST=true
|
24
|
-
api_key:
|
25
|
-
secure: nL74QuUczEpA0qbhSBN2zjGdviWgKB3wR6vFvwervv1MZNWmwOQUYe99Oq9kPeyc8/x2MR/H6PQm5qbrk/WAfRede01WxlZ/EBUW+9CYGrxcBsGONx9IULO8A0I8/yN/YJHW2vjo3dfR66EwVsXTVWq8U63PRRcwJIyTqnIiUm2sxauMQoPRBbXG+pD9v/EJSn3ugpdtxp0lVYDn8LDKk5Ho4/wbpY4ML11XUJa9mz9CyR/GsAzdy5FTXaDMOwuWOVEx9cab7m4qPOBhmlJY4TrmooFpxTxRwChcvByjq1IboEd2M3RT5on7Q/xDTlHSOuT0OS8mnS2AocGT4a1gC+W/xOlghgEcN+xs2V5mfucR6+iUYlCy32uz1w3ey7T2X5xN4ubut09r1xLi7eu1NisAoAc+GOJ4TIxQNqkeRhY4X/fs8j7SMfOEMDr6pPxSLKZxgSvExt+IbdcZD/uQ7rTBQkadYCbc9MX5dHazBievmar3ZsFffbIf+n13FVDXsaPgRt7DlFM5dqGrEwVwt1jFRhdFuDCjkj4QWOLn7E1uY3XqgrqGvgUBlF8Znwc6qicW8zxV4SIWhqIzCOH6L9WIZGLHNq0remoCd9sq9Ter9av3jL+6UmZRRAr+JceeZfZmsYIXKomECzleM9FXMx7FXlpjJKOlf3JnrfeCTwI=
|
data/Gemfile
DELETED
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "value_semantics"
|
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
DELETED
data/bin/test
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
#!/bin/bash
|
2
|
-
set -ue
|
3
|
-
|
4
|
-
MUTANT_PATTERN=${1:-ValueSemantics*}
|
5
|
-
|
6
|
-
# if $MUTATION_TEST is false, just run RSpec
|
7
|
-
if [[ "${MUTATION_TEST:-true}" == "false" ]] ; then
|
8
|
-
bundle exec rspec
|
9
|
-
else
|
10
|
-
bundle exec mutant \
|
11
|
-
--include lib \
|
12
|
-
--require value_semantics \
|
13
|
-
--use rspec "$MUTANT_PATTERN" \
|
14
|
-
--ignore-subject "ValueSemantics::Struct.new" # causes unfixable failure I don't want to deal with
|
15
|
-
fi
|
data/value_semantics.gemspec
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "value_semantics/version"
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "value_semantics"
|
8
|
-
spec.version = ValueSemantics::VERSION
|
9
|
-
spec.authors = ["Tom Dalling"]
|
10
|
-
spec.email = [["tom", "@", "tomdalling.com"].join]
|
11
|
-
|
12
|
-
spec.summary = %q{Makes value classes, with lightweight validation and coercion.}
|
13
|
-
spec.description = %q{
|
14
|
-
Generates modules that provide conventional value semantics for a given set of attributes.
|
15
|
-
The behaviour is similar to an immutable `Struct` class,
|
16
|
-
plus extensible, lightweight validation and coercion.
|
17
|
-
}
|
18
|
-
spec.homepage = "https://github.com/tomdalling/value_semantics"
|
19
|
-
spec.license = "MIT"
|
20
|
-
|
21
|
-
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
22
|
-
f.match(%r{^(test|spec|features)/})
|
23
|
-
end
|
24
|
-
spec.bindir = "exe"
|
25
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
-
spec.require_paths = ["lib"]
|
27
|
-
|
28
|
-
spec.add_development_dependency "bundler", ">= 1.15"
|
29
|
-
spec.add_development_dependency "rspec", "~> 3.7"
|
30
|
-
spec.add_development_dependency "mutant-rspec"
|
31
|
-
spec.add_development_dependency "yard"
|
32
|
-
spec.add_development_dependency "byebug"
|
33
|
-
spec.add_development_dependency "gem-release"
|
34
|
-
end
|