value_semantics 3.2.0 → 3.6.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.
@@ -0,0 +1,3 @@
1
+ require 'value_semantics'
2
+
3
+ ValueSemantics.monkey_patch!
@@ -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,44 @@
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
+ return nil unless obj.respond_to?(:to_h)
31
+ obj_hash = obj.to_h
32
+
33
+ {}.tap do |attrs|
34
+ value_class.value_semantics.attributes.each do |attr_def|
35
+ name = attr_def.name
36
+ value = obj_hash.fetch(name) do
37
+ obj_hash.fetch(name.to_s, NOT_FOUND)
38
+ end
39
+ attrs[name] = value unless value.equal?(NOT_FOUND)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module ValueSemantics
2
- VERSION = "3.2.0"
2
+ VERSION = "3.6.0"
3
3
  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.2.0
4
+ version: 3.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Dalling
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-30 00:00:00.000000000 Z
11
+ date: 2020-09-01 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,28 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 3.7.0
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.0
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.0/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
- rubyforge_project:
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
@@ -1,12 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
-
11
- # rspec failure tracking
12
- .rspec_status
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
@@ -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
@@ -1,8 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- source 'https://oss:fLUos7k6c7Ak7zjhwbYphPJbwBk1Uuew@gem.mutant.dev' do
4
- gem 'mutant-license'
5
- end
6
-
7
- # Specify your gem's dependencies in the gemspec
8
- gemspec
@@ -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
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
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
@@ -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.0"
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