dry-struct 0.5.1 → 0.6.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
  SHA256:
3
- metadata.gz: 61dba90344018c1b2a9cbb58de0a235438958fbbdd5ed3c3effcd075eeae9055
4
- data.tar.gz: dd79a872121c6fd07c2cc3635651e11e5073f50b29dca40e041d2bd9ba614458
3
+ metadata.gz: 33cd05dd51a3310d7ede685ae1ce8631a0955d7563afca7633a7625b9b34dd17
4
+ data.tar.gz: 182c58865ccb3e74595aa22d6c9730ce8ca0e078d1b6ecb5e8b69e026ce6409d
5
5
  SHA512:
6
- metadata.gz: ee05a93c33c0dfe02b373cb57cb06fee2c1740c80260ef977ebbf9b182279229705a693c59044e449064472b8f980d9141424575495330bd09b263f6ab47c12b
7
- data.tar.gz: 5391764853bbdf1effb65dbc83b7c17c9142fd2b29c877c5eb280b2fbd83b06f65d835251b0126ed62afb0b9f28dc86bbf174b07a362ce797cecd3bd46bba269
6
+ metadata.gz: 3f5e4f6609b93e80ae775ab556812cc872883eaf423a74ae2e5191d2bef6907152653fa7fe441bb9aab945b7cf9f4ec62dd4d359ffcb88f4e976b8b53fb42ac8
7
+ data.tar.gz: 4b76e94533ff98f1c1d9d427967a5c19af3d58484a5e785c7eac15d50211d70e4718d35572bb6e89fbafeb9b9f092a73f68e517244676f28d4c7e4854f1a055a
data/.gitignore CHANGED
@@ -7,5 +7,6 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.log
10
11
 
11
12
  .DS_Store
@@ -9,11 +9,11 @@ before_install:
9
9
  after_success:
10
10
  - '[ -d coverage ] && bundle exec codeclimate-test-reporter'
11
11
  rvm:
12
- - 2.2.9
13
- - 2.3.6
14
- - 2.4.3
15
- - 2.5.0
16
- - jruby-9.1.15.0
12
+ - 2.2.10
13
+ - 2.3.7
14
+ - 2.4.4
15
+ - 2.5.1
16
+ - jruby-9.2.0.0
17
17
  env:
18
18
  global:
19
19
  - COVERAGE=true
@@ -1,3 +1,27 @@
1
+ # v0.6.0 2018-10-24
2
+
3
+ ## BREAKING CHANGES
4
+
5
+ * `Struct.attribute?` in the old sense is deprecated, use `has_attribute?` as a replacement
6
+
7
+ ## Added
8
+
9
+ * `Struct.attribute?` is an easy way to define omittable attributes (flash-gordon):
10
+
11
+ ```ruby
12
+ class User < Dry::Struct
13
+ attribute :name, Types::Strict::String
14
+ attribute? :email, Types::Strict::String
15
+ end
16
+ # User.new(name: 'John') # => #<User name="John">
17
+ ```
18
+
19
+ ## Fixed
20
+
21
+ * `Struct#to_h` recursively converts hash values to hashes, this was done to be consistent with current behavior for arrays (oeoeaio + ZimbiX)
22
+
23
+ [Compare v0.5.1...v0.6.0](https://github.com/dry-rb/dry-struct/compare/v0.5.1...v0.6.0)
24
+
1
25
  # v0.5.1 2018-08-11
2
26
 
3
27
  ## Fixed
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
17
17
  # delete this section to allow pushing this gem to any host.
18
18
  if spec.respond_to?(:metadata)
19
19
  spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/dry-rb/dry-struct/blob/master/CHANGELOG.md'
21
+ spec.metadata['source_code_uri'] = 'https://github.com/dry-rb/dry-struct'
20
22
  else
21
23
  raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
22
24
  end
@@ -167,8 +167,8 @@ module Dry
167
167
  # )
168
168
  # #=> #<Book title="Web Development with ROM and Roda" subtitle="2nd edition">
169
169
  #
170
- # rom_n_roda.new(subtitle: '3nd edition')
171
- # #=> #<Book title="Web Development with ROM and Roda" subtitle="3nd edition">
170
+ # rom_n_roda.new(subtitle: '3rd edition')
171
+ # #=> #<Book title="Web Development with ROM and Roda" subtitle="3rd edition">
172
172
  def new(changeset)
173
173
  self.class[__attributes__.merge(changeset)]
174
174
  end
@@ -100,17 +100,37 @@ module Dry
100
100
  # ruby.celebrities[1].name #=> 'Aaron Patterson'
101
101
  # ruby.celebrities[1].pseudonym #=> 'tenderlove'
102
102
  def attribute(name, type = nil, &block)
103
- if block
104
- type = Dry::Types[type] if type.is_a?(String)
105
- type = struct_builder.(name, type, &block)
106
- elsif type.nil?
107
- raise(
108
- ArgumentError,
109
- 'you must supply a type or a block to `Dry::Struct.attribute`'
103
+ attributes(name => build_type(name, type, &block))
104
+ end
105
+
106
+ # Adds an omittable (key is not required on initialization) attribute for this {Struct}
107
+ #
108
+ # @example
109
+ # class User < Dry::Struct
110
+ # attribute :name, Types::Strict::String
111
+ # attribute? :email, Types::Strict::String
112
+ # end
113
+ #
114
+ # User.new(name: 'John') # => #<User name="John">
115
+ #
116
+ # @param [Symbol] name name of the defined attribute
117
+ # @param [Dry::Types::Definition, nil] type or superclass of nested type
118
+ # @return [Dry::Struct]
119
+ #
120
+ def attribute?(*args, &block)
121
+ if args.size == 1 && block.nil?
122
+ Dry::Core::Deprecations.warn(
123
+ 'Dry::Struct.attribute? is deprecated for checking attribute presence, '\
124
+ 'use has_attribute? instead',
125
+ tag: :'dry-struct'
110
126
  )
111
- end
112
127
 
113
- attributes(name => type)
128
+ has_attribute?(args[0])
129
+ else
130
+ name, type = args
131
+
132
+ attribute(name, build_type(name, type, &block).meta(omittable: true))
133
+ end
114
134
  end
115
135
 
116
136
  # @param [Hash{Symbol => Dry::Types::Definition}] new_schema
@@ -297,7 +317,7 @@ module Dry
297
317
  #
298
318
  # @param [Symbol] key Attribute name
299
319
  # @return [Boolean]
300
- def attribute?(key)
320
+ def has_attribute?(key)
301
321
  schema.key?(key)
302
322
  end
303
323
 
@@ -361,6 +381,30 @@ module Dry
361
381
  type.is_a?(Class) && type <= Struct
362
382
  end
363
383
  private :struct?
384
+
385
+ # Constructs a type
386
+ #
387
+ # @return [Dry::Types::Type, Dry::Struct]
388
+ def build_type(name, type, &block)
389
+ type_object =
390
+ if type.is_a?(String)
391
+ Dry::Types[type]
392
+ elsif block.nil? && type.nil?
393
+ raise(
394
+ ArgumentError,
395
+ 'you must supply a type or a block to `Dry::Struct.attribute`'
396
+ )
397
+ else
398
+ type
399
+ end
400
+
401
+ if block
402
+ struct_builder.(name, type_object, &block)
403
+ else
404
+ type_object
405
+ end
406
+ end
407
+ private :build_type
364
408
  end
365
409
  end
366
410
  end
@@ -7,7 +7,11 @@ module Dry
7
7
  # @return [Hash, Array]
8
8
  def self.[](value)
9
9
  if value.respond_to?(:to_hash)
10
- value.to_hash
10
+ if RUBY_VERSION >= '2.4'
11
+ value.to_hash.transform_values { |v| self[v] }
12
+ else
13
+ value.to_hash.each_with_object({}) { |(k, v), h| h[k] = self[v] }
14
+ end
11
15
  elsif value.respond_to?(:to_ary)
12
16
  value.to_ary.map { |item| self[item] }
13
17
  else
@@ -1,6 +1,6 @@
1
1
  module Dry
2
2
  class Struct
3
3
  # @private
4
- VERSION = '0.5.1'.freeze
4
+ VERSION = '0.6.0'.freeze
5
5
  end
6
6
  end
File without changes
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.5.1
4
+ version: 0.6.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: 2018-08-11 00:00:00.000000000 Z
11
+ date: 2018-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-equalizer
@@ -162,11 +162,14 @@ files:
162
162
  - lib/dry/struct/sum.rb
163
163
  - lib/dry/struct/value.rb
164
164
  - lib/dry/struct/version.rb
165
+ - log/.gitkeep
165
166
  homepage: https://github.com/dry-rb/dry-struct
166
167
  licenses:
167
168
  - MIT
168
169
  metadata:
169
170
  allowed_push_host: https://rubygems.org
171
+ changelog_uri: https://github.com/dry-rb/dry-struct/blob/master/CHANGELOG.md
172
+ source_code_uri: https://github.com/dry-rb/dry-struct
170
173
  post_install_message:
171
174
  rdoc_options: []
172
175
  require_paths: