dry-struct 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 223d319c97aced13759a063d7d19d0afc7eeff56
4
- data.tar.gz: '09b4aecf6acb6981e71e9e98a8f3049e9e5b4ff6'
3
+ metadata.gz: 33a4358e2a11c38b8816778acc5722b6d08a4c4d
4
+ data.tar.gz: 24bebcf82ec2e1dcb7536216acc397ae9aa6a3c7
5
5
  SHA512:
6
- metadata.gz: 52ef521bf80136eb363dc53adb98e21f1b379fa7697edadf9e4f882e32a2334948beb316f997fa5c11fabb871674b40f86fd373b254253bb879e6a0db516e63e
7
- data.tar.gz: f2fac5903962ee469f009a566fabd77fc8c0e9470222ef9a78fd63db6916a1b0e36958e34c0c083e5e146e11398dfdf567c7dfbefd18f82bfd03338cba8822bd
6
+ metadata.gz: 332a625be1cde86fefd74efe78e79b375c6c66a15f5ad062f26e736055b9a9e9aef9234299bd0a7ff0272ce2e80218060d0ddb52d0074a5bc7d1ed12a03e1971
7
+ data.tar.gz: 757d33e5151cf3f30ffbe28798f1c86ab634f12612d4a7603431aae72513eedf088a3cf483603809b10ac6da870da2dc2f39ef1136e6fe99c42b25ee979ea681
@@ -7,7 +7,6 @@ script:
7
7
  after_success:
8
8
  - '[ -d coverage ] && bundle exec codeclimate-test-reporter'
9
9
  rvm:
10
- - 2.1.10
11
10
  - 2.2.7
12
11
  - 2.3.3
13
12
  - 2.4.1
@@ -1,4 +1,18 @@
1
- # v0.3.1 to-be-released
1
+ # v0.4.0 2017-11-04
2
+
3
+ ## Changed
4
+
5
+ * Attribute readers don't override existing instance methods (solnic)
6
+ * `Struct#new` uses raw attributes instead of method calls, this makes the behavior consistent with the change above (flash-gordon)
7
+ * `constructor_type` now actively rejects `:weak` and `:symbolized` values (GustavoCaso)
8
+
9
+ ## Fixed
10
+
11
+ * `Struct#new` doesn't call `.to_hash` recursively (flash-gordon)
12
+
13
+ [Compare v0.3.1...v0.4.0](https://github.com/dry-rb/dry-struct/compare/v0.3.1...v0.4.0)
14
+
15
+ # v0.3.1 2017-06-30
2
16
 
3
17
  ## Added
4
18
 
@@ -6,7 +20,7 @@
6
20
  * `Struct.attribute?` and `Struct.attribute_names` for introspecting struct attributes (flash-gordon)
7
21
  * `Struct#__new__` is a safe-to-use-in-gems alias for `Struct#new` (flash-gordon)
8
22
 
9
- [Compare v0.3.0...master](https://github.com/dry-rb/dry-struct/compare/v0.3.0...master)
23
+ [Compare v0.3.0...v0.3.1](https://github.com/dry-rb/dry-struct/compare/v0.3.0...v0.3.1)
10
24
 
11
25
  # v0.3.0 2017-05-05
12
26
 
data/Gemfile CHANGED
@@ -2,9 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
+ gem 'dry-types', git: 'https://github.com/dry-rb/dry-types'
6
+
5
7
  group :test do
6
8
  gem 'codeclimate-test-reporter', platform: :mri, require: false
7
9
  gem 'simplecov', require: false
10
+ gem 'warning' if RUBY_VERSION >= '2.4.0'
8
11
  end
9
12
 
10
13
  group :tools do
@@ -27,9 +27,8 @@ Gem::Specification.new do |spec|
27
27
  spec.require_paths = ['lib']
28
28
 
29
29
  spec.add_runtime_dependency 'dry-equalizer', '~> 0.2'
30
- spec.add_runtime_dependency 'dry-configurable', '~> 0.1'
31
- spec.add_runtime_dependency 'dry-types', '~> 0.9', '>= 0.9.0'
32
- spec.add_runtime_dependency 'dry-core', '~> 0.3'
30
+ spec.add_runtime_dependency 'dry-types', '~> 0.12', '>= 0.12.2'
31
+ spec.add_runtime_dependency 'dry-core', '~> 0.4', '>= 0.4.1'
33
32
  spec.add_runtime_dependency 'ice_nine', '~> 0.11'
34
33
 
35
34
  spec.add_development_dependency 'bundler', '~> 1.6'
@@ -70,6 +70,8 @@ module Dry
70
70
  defines :schema
71
71
  schema EMPTY_HASH
72
72
 
73
+ CONSTRUCTOR_TYPE = Dry::Types['symbol'].enum(:permissive, :schema, :strict, :strict_with_defaults)
74
+
73
75
  # Sets or retrieves {#constructor} type as a symbol
74
76
  #
75
77
  # @note All examples below assume that you have defined {Struct} with
@@ -107,13 +109,6 @@ module Dry
107
109
  # 3. Input contains nil for a value that specifies a default
108
110
  # 4. Input includes a key that was not specified in the schema
109
111
  #
110
- # @note Don’t use `:weak` and `:symbolized` as {#constructor_type},
111
- # and instead use [`dry-validation`][] to process and validate
112
- # attributes, otherwise your struct will behave as a data validator
113
- # which raises exceptions on invalid input (assuming your attributes
114
- # types are strict)
115
- # [`dry-validation`]: https://github.com/dry-rb/dry-validation
116
- #
117
112
  # @example `:permissive` constructor
118
113
  # class User < Dry::Struct
119
114
  # constructor_type :permissive
@@ -177,7 +172,7 @@ module Dry
177
172
  # @overload constructor_type
178
173
  # Returns the constructor type for {Struct}
179
174
  # @return [Symbol] (:strict)
180
- defines :constructor_type
175
+ defines :constructor_type, type: CONSTRUCTOR_TYPE
181
176
  constructor_type :permissive
182
177
 
183
178
  # @return [Dry::Equalizer]
@@ -254,9 +249,17 @@ module Dry
254
249
  # rom_n_roda.new(subtitle: '3nd edition')
255
250
  # #=> #<Book title="Web Development with ROM and Roda" subtitle="3nd edition">
256
251
  def new(changeset)
257
- self.class[to_hash.merge(changeset)]
252
+ self.class[__attributes__.merge(changeset)]
258
253
  end
259
254
  alias_method :__new__, :new
255
+
256
+ # @return[Hash{Symbol => Object}]
257
+ # @api private
258
+ def __attributes__
259
+ self.class.attribute_names.each_with_object({}) do |key, h|
260
+ h[key] = instance_variable_get(:"@#{ key }")
261
+ end
262
+ end
260
263
  end
261
264
  end
262
265
 
@@ -10,6 +10,7 @@ module Dry
10
10
  module ClassInterface
11
11
  include Core::ClassAttributes
12
12
 
13
+ include Dry::Types::Type
13
14
  include Dry::Types::Builder
14
15
 
15
16
  # @param [Class] klass
@@ -65,7 +66,10 @@ module Dry
65
66
  schema schema.merge(new_schema)
66
67
  input Types['coercible.hash'].public_send(constructor_type, schema)
67
68
 
68
- attr_reader(*new_schema.keys)
69
+ new_schema.each_key do |key|
70
+ attr_reader(key) unless instance_methods.include?(key)
71
+ end
72
+
69
73
  equalizer.instance_variable_get('@keys').concat(new_schema.keys)
70
74
 
71
75
  self
@@ -200,7 +204,7 @@ module Dry
200
204
  #
201
205
  # @return [Array<Symbol>]
202
206
  def attribute_names
203
- schema.keys
207
+ @attribute_names ||= schema.keys
204
208
  end
205
209
  end
206
210
  end
@@ -2,6 +2,7 @@ module Dry
2
2
  class Struct
3
3
  class Constructor
4
4
  include Dry::Equalizer(:type)
5
+ include Dry::Types::Type
5
6
 
6
7
  # @return [#call]
7
8
  attr_reader :fn
@@ -1,9 +1,5 @@
1
1
  module Dry
2
2
  class Struct
3
- extend Dry::Configurable
4
-
5
- setting :namespace, self
6
-
7
3
  # Raised when given input doesn't conform schema and constructor type
8
4
  Error = Class.new(TypeError)
9
5
 
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  class Struct
3
- VERSION = '0.3.1'.freeze
3
+ VERSION = '0.4.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-30 00:00:00.000000000 Z
11
+ date: 2017-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-equalizer
@@ -24,54 +24,46 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.2'
27
- - !ruby/object:Gem::Dependency
28
- name: dry-configurable
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '0.1'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0.1'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: dry-types
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: '0.9'
33
+ version: '0.12'
48
34
  - - ">="
49
35
  - !ruby/object:Gem::Version
50
- version: 0.9.0
36
+ version: 0.12.2
51
37
  type: :runtime
52
38
  prerelease: false
53
39
  version_requirements: !ruby/object:Gem::Requirement
54
40
  requirements:
55
41
  - - "~>"
56
42
  - !ruby/object:Gem::Version
57
- version: '0.9'
43
+ version: '0.12'
58
44
  - - ">="
59
45
  - !ruby/object:Gem::Version
60
- version: 0.9.0
46
+ version: 0.12.2
61
47
  - !ruby/object:Gem::Dependency
62
48
  name: dry-core
63
49
  requirement: !ruby/object:Gem::Requirement
64
50
  requirements:
65
51
  - - "~>"
66
52
  - !ruby/object:Gem::Version
67
- version: '0.3'
53
+ version: '0.4'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.4.1
68
57
  type: :runtime
69
58
  prerelease: false
70
59
  version_requirements: !ruby/object:Gem::Requirement
71
60
  requirements:
72
61
  - - "~>"
73
62
  - !ruby/object:Gem::Version
74
- version: '0.3'
63
+ version: '0.4'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 0.4.1
75
67
  - !ruby/object:Gem::Dependency
76
68
  name: ice_nine
77
69
  requirement: !ruby/object:Gem::Requirement