value_semantics 3.5.0 → 3.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.5.0"
2
+ VERSION = "3.6.0"
3
3
  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.5.0
4
+ version: 3.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Dalling
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-17 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
@@ -122,6 +122,34 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
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'
125
153
  description: "\n Generates modules that provide conventional value semantics for
126
154
  a given set of attributes.\n The behaviour is similar to an immutable `Struct`
127
155
  class,\n plus extensible, lightweight validation and coercion.\n "
@@ -135,7 +163,22 @@ files:
135
163
  - LICENSE.txt
136
164
  - README.md
137
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
138
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
139
182
  - lib/value_semantics/version.rb
140
183
  homepage: https://github.com/tomdalling/value_semantics
141
184
  licenses:
@@ -143,7 +186,7 @@ licenses:
143
186
  metadata:
144
187
  bug_tracker_uri: https://github.com/tomdalling/value_semantics/issues
145
188
  changelog_uri: https://github.com/tomdalling/value_semantics/blob/master/CHANGELOG.md
146
- documentation_uri: https://github.com/tomdalling/value_semantics/blob/v3.5.0/README.md
189
+ documentation_uri: https://github.com/tomdalling/value_semantics/blob/v3.6.0/README.md
147
190
  source_code_uri: https://github.com/tomdalling/value_semantics
148
191
  post_install_message:
149
192
  rdoc_options: []