value_semantics 3.0.0 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -0
- data/README.md +37 -7
- data/lib/value_semantics.rb +36 -4
- data/lib/value_semantics/version.rb +1 -1
- metadata +19 -22
- data/.gitignore +0 -12
- data/.rspec +0 -2
- data/.travis.yml +0 -24
- data/Gemfile +0 -6
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/bin/test +0 -19
- data/value_semantics.gemspec +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9917cde0bb66e10e931d39308a1d968973e10f5de4b60b15898cb3d390f77fb9
|
4
|
+
data.tar.gz: b9ab7670cc254252d357a8276c1fb98305e9c3e60634354883e078179bad7b03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40c1a0ef2a51f2e18465013660a71ca3dbd50fd615dd1e0e7dd4e6e0a65ba3fc5f742132b62467178dad75f5e14452d8ebc38742fa8928254baad8709e89dfd6
|
7
|
+
data.tar.gz: 9d1eebc8605d6aed14be4eb0fec9fb78f7eca69894e3ee3d79c34a8c41763542b4a8ffc8fd57eba2848735b0732286d8cacbb46a64d509c5fe89a519c06afbe3
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
Notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [3.3.0] - 2020-07-17
|
9
|
+
### Added
|
10
|
+
- Added support for pattern matching in Ruby 2.7
|
11
|
+
|
12
|
+
## [3.2.1] - 2020-07-11
|
13
|
+
### Fixed
|
14
|
+
- Fix warnings new to Ruby 2.7 about keyword arguments
|
15
|
+
|
16
|
+
## [3.2.0] - 2019-09-30
|
17
|
+
### Added
|
18
|
+
- `ValueSemantics::Struct`, a convenience for creating a new class and mixing
|
19
|
+
in ValueSemantics in a single step.
|
20
|
+
|
21
|
+
## [3.1.0] - 2019-06-30
|
22
|
+
### Added
|
23
|
+
- Built-in PP support for value classes
|
24
|
+
|
25
|
+
## [3.0.0] - 2019-01-27
|
26
|
+
|
27
|
+
First public release
|
data/README.md
CHANGED
@@ -1,19 +1,24 @@
|
|
1
1
|
[![Gem Version](https://badge.fury.io/rb/value_semantics.svg)](https://badge.fury.io/rb/value_semantics)
|
2
2
|
[![Build Status](https://travis-ci.org/tomdalling/value_semantics.svg?branch=master)](https://travis-ci.org/tomdalling/value_semantics)
|
3
|
-
![Mutation Coverage](https://img.shields.io/badge/mutation%20coverage-
|
3
|
+
![Mutation Coverage](https://img.shields.io/badge/mutation%20coverage-to%20the%20MAX-brightgreen.svg)
|
4
4
|
|
5
5
|
ValueSemantics
|
6
6
|
==============
|
7
7
|
|
8
|
-
|
8
|
+
A gem for making value classes.
|
9
9
|
|
10
|
-
Generates modules that provide value semantics for a given set of attributes.
|
11
|
-
|
12
|
-
|
10
|
+
Generates modules that provide [conventional value semantics](https://github.com/zverok/good-value-object) for a given set of attributes.
|
11
|
+
The behaviour is similar to an immutable `Struct` class,
|
12
|
+
plus extensible, lightweight validation and coercion.
|
13
13
|
|
14
14
|
These are intended for internal use, as opposed to validating user input like ActiveRecord.
|
15
|
-
Invalid or missing attributes cause an exception
|
16
|
-
not an error message intended for
|
15
|
+
Invalid or missing attributes cause an exception for developers,
|
16
|
+
not an error message intended for application users.
|
17
|
+
|
18
|
+
See the [announcement blog post][] for some of the rationale behind the gem, and some [discussion on Reddit].
|
19
|
+
|
20
|
+
[announcement blog post]: https://www.rubypigeon.com/posts/value-semantics-gem-for-making-value-classes/
|
21
|
+
[discussion on Reddit]: https://www.reddit.com/r/ruby/comments/akz4fs/valuesemanticsa_gem_for_making_value_classes/
|
17
22
|
|
18
23
|
|
19
24
|
Defining and Creating Value Objects
|
@@ -96,6 +101,15 @@ other_tom = Person.new(name: 'Tom', age: 31)
|
|
96
101
|
tom == other_tom #=> true
|
97
102
|
tom.eql?(other_tom) #=> true
|
98
103
|
tom.hash == other_tom.hash #=> true
|
104
|
+
|
105
|
+
|
106
|
+
#
|
107
|
+
# Ruby 2.7+ pattern matching
|
108
|
+
#
|
109
|
+
case tom
|
110
|
+
in { name: "Tom", age: }
|
111
|
+
puts age # outputs: 31
|
112
|
+
end
|
99
113
|
```
|
100
114
|
|
101
115
|
|
@@ -299,6 +313,21 @@ For example, the default value could be a string,
|
|
299
313
|
which would then be coerced into an `IPAddr` object.
|
300
314
|
|
301
315
|
|
316
|
+
## ValueSemantics::Struct
|
317
|
+
|
318
|
+
This is a convenience for making a new class and including ValueSemantics in
|
319
|
+
one step, similar to how `Struct` works from the Ruby standard library. For
|
320
|
+
example:
|
321
|
+
|
322
|
+
```ruby
|
323
|
+
Cat = ValueSemantics::Struct.new do
|
324
|
+
name String, default: "Mittens"
|
325
|
+
end
|
326
|
+
|
327
|
+
Cat.new.name #=> "Mittens"
|
328
|
+
```
|
329
|
+
|
330
|
+
|
302
331
|
## Installation
|
303
332
|
|
304
333
|
Add this line to your application's Gemfile:
|
@@ -321,6 +350,7 @@ Or install it yourself as:
|
|
321
350
|
Bug reports and pull requests are welcome on GitHub at:
|
322
351
|
https://github.com/tomdalling/value_semantics
|
323
352
|
|
353
|
+
Keep in mind that this gem aims to be as close to 100% backwards compatible as possible.
|
324
354
|
|
325
355
|
## License
|
326
356
|
|
data/lib/value_semantics.rb
CHANGED
@@ -139,6 +139,20 @@ module ValueSemantics
|
|
139
139
|
|
140
140
|
"#<#{self.class} #{attrs}>"
|
141
141
|
end
|
142
|
+
|
143
|
+
def pretty_print(pp)
|
144
|
+
pp.object_group(self) do
|
145
|
+
to_h.each do |attr, value|
|
146
|
+
pp.breakable
|
147
|
+
pp.text("#{attr}=")
|
148
|
+
pp.pp(value)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def deconstruct_keys(_)
|
154
|
+
to_h
|
155
|
+
end
|
142
156
|
end
|
143
157
|
|
144
158
|
#
|
@@ -284,13 +298,13 @@ module ValueSemantics
|
|
284
298
|
ArrayOf.new(element_validator)
|
285
299
|
end
|
286
300
|
|
287
|
-
def def_attr(*args)
|
288
|
-
__attributes << Attribute.define(*args)
|
301
|
+
def def_attr(*args, **kwargs)
|
302
|
+
__attributes << Attribute.define(*args, **kwargs)
|
289
303
|
end
|
290
304
|
|
291
|
-
def method_missing(name, *args)
|
305
|
+
def method_missing(name, *args, **kwargs)
|
292
306
|
if respond_to_missing?(name)
|
293
|
-
def_attr(name, *args)
|
307
|
+
def_attr(name, *args, **kwargs)
|
294
308
|
else
|
295
309
|
super
|
296
310
|
end
|
@@ -356,4 +370,22 @@ module ValueSemantics
|
|
356
370
|
end
|
357
371
|
end
|
358
372
|
|
373
|
+
#
|
374
|
+
# ValueSemantics equivalent of the Struct class from the Ruby standard
|
375
|
+
# library
|
376
|
+
#
|
377
|
+
class Struct
|
378
|
+
#
|
379
|
+
# Creates a new Class with ValueSemantics mixed in
|
380
|
+
#
|
381
|
+
# @yield a block containing ValueSemantics DSL
|
382
|
+
# @return [Class] the newly created class
|
383
|
+
#
|
384
|
+
def self.new(&block)
|
385
|
+
klass = Class.new
|
386
|
+
klass.include(ValueSemantics.for_attributes(&block))
|
387
|
+
klass
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
359
391
|
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: value_semantics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.3.0
|
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: 2020-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.15'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.15'
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 3.7
|
33
|
+
version: '3.7'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 3.7
|
40
|
+
version: '3.7'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: mutant-rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,32 +94,28 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
-
description: "\n
|
98
|
-
|
99
|
-
|
100
|
-
validation and coercion.\n "
|
97
|
+
description: "\n Generates modules that provide conventional value semantics for
|
98
|
+
a given set of attributes.\n The behaviour is similar to an immutable `Struct`
|
99
|
+
class,\n plus extensible, lightweight validation and coercion.\n "
|
101
100
|
email:
|
102
101
|
- tom@tomdalling.com
|
103
102
|
executables: []
|
104
103
|
extensions: []
|
105
104
|
extra_rdoc_files: []
|
106
105
|
files:
|
107
|
-
-
|
108
|
-
- ".rspec"
|
109
|
-
- ".travis.yml"
|
110
|
-
- Gemfile
|
106
|
+
- CHANGELOG.md
|
111
107
|
- LICENSE.txt
|
112
108
|
- README.md
|
113
|
-
- bin/console
|
114
|
-
- bin/setup
|
115
|
-
- bin/test
|
116
109
|
- lib/value_semantics.rb
|
117
110
|
- lib/value_semantics/version.rb
|
118
|
-
- value_semantics.gemspec
|
119
111
|
homepage: https://github.com/tomdalling/value_semantics
|
120
112
|
licenses:
|
121
113
|
- MIT
|
122
|
-
metadata:
|
114
|
+
metadata:
|
115
|
+
bug_tracker_uri: https://github.com/tomdalling/value_semantics/issues
|
116
|
+
changelog_uri: https://github.com/tomdalling/value_semantics/blob/master/CHANGELOG.md
|
117
|
+
documentation_uri: https://github.com/tomdalling/value_semantics/blob/v3.3.0/README.md
|
118
|
+
source_code_uri: https://github.com/tomdalling/value_semantics
|
123
119
|
post_install_message:
|
124
120
|
rdoc_options: []
|
125
121
|
require_paths:
|
@@ -135,8 +131,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
131
|
- !ruby/object:Gem::Version
|
136
132
|
version: '0'
|
137
133
|
requirements: []
|
138
|
-
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.7.7
|
139
136
|
signing_key:
|
140
137
|
specification_version: 4
|
141
|
-
summary:
|
138
|
+
summary: Makes value classes, with lightweight validation and coercion.
|
142
139
|
test_files: []
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
@@ -1,24 +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.5
|
8
|
-
- 2.5.3
|
9
|
-
env: MUTATION_TEST=false
|
10
|
-
|
11
|
-
# test the latest Ruby version WITH mutation testing
|
12
|
-
matrix:
|
13
|
-
include:
|
14
|
-
- rvm: 2.6.0
|
15
|
-
env: MUTATION_TEST=true
|
16
|
-
|
17
|
-
# deploy gem on tagged commits, on the latest Ruby version only
|
18
|
-
deploy:
|
19
|
-
provider: rubygems
|
20
|
-
on:
|
21
|
-
tags: true
|
22
|
-
env: MUTATION_TEST=true
|
23
|
-
api_key:
|
24
|
-
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,19 +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
|
-
# Mutant 0.8.24 introduces new mutations that cause infinite recursion inside
|
15
|
-
# #method_missing. These --ignore-subject lines prevent that from happening
|
16
|
-
#--ignore-subject "ValueSemantics::DSL#method_missing" \
|
17
|
-
#--ignore-subject "ValueSemantics::DSL#respond_to_missing?" \
|
18
|
-
#--ignore-subject "ValueSemantics::DSL#def_attr" \
|
19
|
-
fi
|
data/value_semantics.gemspec
DELETED
@@ -1,36 +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{Create value classes quickly, with all the proper conventions.}
|
13
|
-
spec.description = %q{
|
14
|
-
Create value classes quickly, with all the proper conventions.
|
15
|
-
|
16
|
-
Generates modules that provide value semantics for a given set of attributes.
|
17
|
-
Provides the behaviour of an immutable struct-like value class,
|
18
|
-
with light-weight validation and coercion.
|
19
|
-
}
|
20
|
-
spec.homepage = "https://github.com/tomdalling/value_semantics"
|
21
|
-
spec.license = "MIT"
|
22
|
-
|
23
|
-
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
24
|
-
f.match(%r{^(test|spec|features)/})
|
25
|
-
end
|
26
|
-
spec.bindir = "exe"
|
27
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
-
spec.require_paths = ["lib"]
|
29
|
-
|
30
|
-
spec.add_development_dependency "bundler", "~> 1.15"
|
31
|
-
spec.add_development_dependency "rspec", "~> 3.7.0"
|
32
|
-
spec.add_development_dependency "mutant-rspec"
|
33
|
-
spec.add_development_dependency "yard"
|
34
|
-
spec.add_development_dependency "byebug"
|
35
|
-
spec.add_development_dependency "gem-release"
|
36
|
-
end
|