dry-struct 0.3.0 → 0.3.1

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: 0cc9fef4b32f2b57bac889a7f5ca759795b650dd
4
- data.tar.gz: be8ff9b280bcc8d0afdccf28d39453b7060e13d3
3
+ metadata.gz: 223d319c97aced13759a063d7d19d0afc7eeff56
4
+ data.tar.gz: '09b4aecf6acb6981e71e9e98a8f3049e9e5b4ff6'
5
5
  SHA512:
6
- metadata.gz: f8c76b9683ad51451060a3c388741598aedcac78946b0f979c8928fef1a3e6f4300c211438f3d3ca6490c23525730d051ffb7c2a808fd5e1b322474e91b20e9a
7
- data.tar.gz: 0d8dc1bc47204f422ee104d60ee732e7bb276e3441ec255303f3a794f2718be0eb63bea2beda822c08a40a1a04a864d097171c4becaede57d7ffa701794a17fd
6
+ metadata.gz: 52ef521bf80136eb363dc53adb98e21f1b379fa7697edadf9e4f882e32a2334948beb316f997fa5c11fabb871674b40f86fd373b254253bb879e6a0db516e63e
7
+ data.tar.gz: f2fac5903962ee469f009a566fabd77fc8c0e9470222ef9a78fd63db6916a1b0e36958e34c0c083e5e146e11398dfdf567c7dfbefd18f82bfd03338cba8822bd
@@ -11,15 +11,11 @@ rvm:
11
11
  - 2.2.7
12
12
  - 2.3.3
13
13
  - 2.4.1
14
- - rbx-3
15
- - jruby-9.1.8.0
14
+ - jruby-9.1.10.0
16
15
  env:
17
16
  global:
18
17
  - COVERAGE=true
19
18
  - JRUBY_OPTS='--dev -J-Xmx1024M'
20
- matrix:
21
- allow_failures:
22
- - rvm: rbx-3
23
19
  notifications:
24
20
  email: false
25
21
  webhooks:
@@ -1,4 +1,14 @@
1
- master
1
+ # v0.3.1 to-be-released
2
+
3
+ ## Added
4
+
5
+ * `Struct.constructor` that makes dry-struct more aligned with dry-types; now you can have a struct with a custom constructor that will be called _before_ calling the `new` method (v-kolesnikov)
6
+ * `Struct.attribute?` and `Struct.attribute_names` for introspecting struct attributes (flash-gordon)
7
+ * `Struct#__new__` is a safe-to-use-in-gems alias for `Struct#new` (flash-gordon)
8
+
9
+ [Compare v0.3.0...master](https://github.com/dry-rb/dry-struct/compare/v0.3.0...master)
10
+
11
+ # v0.3.0 2017-05-05
2
12
 
3
13
  ## Added
4
14
 
@@ -13,7 +23,7 @@ master
13
23
 
14
24
  * `.new` without arguments doesn't use nil as an input for non-default types anymore (flash-gordon)
15
25
 
16
- [Compare v0.2.1...master](https://github.com/dry-rb/dry-struct/compare/v0.2.1...master)
26
+ [Compare v0.2.1...v0.3.0](https://github.com/dry-rb/dry-struct/compare/v0.2.1...v0.3.0)
17
27
 
18
28
  # v0.2.1 2017-02-27
19
29
 
@@ -0,0 +1,29 @@
1
+ # Issue Guidelines
2
+
3
+ ## Reporting bugs
4
+
5
+ If you found a bug, report an issue and describe what's the expected behavior versus what actually happens. If the bug causes a crash, attach a full backtrace. If possible, a reproduction script showing the problem is highly appreciated.
6
+
7
+ ## Reporting feature requests
8
+
9
+ Report a feature request **only after discussing it first on [discuss.dry-rb.org](https://discuss.dry-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discussion thread, and instead summarize what was discussed.
10
+
11
+ ## Reporting questions, support requests, ideas, concerns etc.
12
+
13
+ **PLEASE DON'T** - use [discuss.dry-rb.org](http://discuss.dry-rb.org) instead.
14
+
15
+ # Pull Request Guidelines
16
+
17
+ A Pull Request will only be accepted if it addresses a specific issue that was reported previously, or fixes typos, mistakes in documentation etc.
18
+
19
+ Other requirements:
20
+
21
+ 1) Do not open a pull request if you can't provide tests along with it. If you have problems writing tests, ask for help in the related issue.
22
+ 2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
23
+ 3) Add API documentation if it's a new feature
24
+ 4) Update API documentation if it changes an existing feature
25
+ 5) Bonus points for sending a PR to [github.com/dry-rb/dry-rb.org](github.com/dry-rb/dry-rb.org) which updates user documentation and guides
26
+
27
+ # Asking for help
28
+
29
+ If these guidelines aren't helpful, and you're stuck, please post a message on [discuss.dry-rb.org](https://discuss.dry-rb.org).
@@ -1,3 +1,4 @@
1
+ require 'dry/core/constants'
1
2
  require 'dry-types'
2
3
 
3
4
  require 'dry/struct/version'
@@ -56,6 +57,7 @@ module Dry
56
57
  # refactoring.title #=> 'Refactoring'
57
58
  # refactoring.subtitle #=> 'Improving the Design of Existing Code'
58
59
  class Struct
60
+ include Dry::Core::Constants
59
61
  extend ClassInterface
60
62
 
61
63
  # {Dry::Types::Hash} subclass with specific behaviour defined for
@@ -64,6 +66,10 @@ module Dry
64
66
  defines :input
65
67
  input Types['coercible.hash']
66
68
 
69
+ # @return [Hash{Symbol => Dry::Types::Definition, Dry::Struct}]
70
+ defines :schema
71
+ schema EMPTY_HASH
72
+
67
73
  # Sets or retrieves {#constructor} type as a symbol
68
74
  #
69
75
  # @note All examples below assume that you have defined {Struct} with
@@ -227,9 +233,30 @@ module Dry
227
233
  end
228
234
  alias_method :to_h, :to_hash
229
235
 
236
+ # Create a copy of {Dry::Struct} with overriden attributes
237
+ #
238
+ # @param [Hash{Symbol => Object}] changeset
239
+ #
240
+ # @return [Struct]
241
+ #
242
+ # @example
243
+ # class Book < Dry::Struct
244
+ # attribute :title, Types::Strict::String
245
+ # attribute :subtitle, Types::Strict::String.optional
246
+ # end
247
+ #
248
+ # rom_n_roda = Book.new(
249
+ # title: 'Web Development with ROM and Roda',
250
+ # subtitle: '2nd edition'
251
+ # )
252
+ # #=> #<Book title="Web Development with ROM and Roda" subtitle="2nd edition">
253
+ #
254
+ # rom_n_roda.new(subtitle: '3nd edition')
255
+ # #=> #<Book title="Web Development with ROM and Roda" subtitle="3nd edition">
230
256
  def new(changeset)
231
257
  self.class[to_hash.merge(changeset)]
232
258
  end
259
+ alias_method :__new__, :new
233
260
  end
234
261
  end
235
262
 
@@ -2,6 +2,7 @@ require 'dry/core/class_attributes'
2
2
  require 'dry/equalizer'
3
3
 
4
4
  require 'dry/struct/errors'
5
+ require 'dry/struct/constructor'
5
6
 
6
7
  module Dry
7
8
  class Struct
@@ -11,20 +12,12 @@ module Dry
11
12
 
12
13
  include Dry::Types::Builder
13
14
 
14
- # @param [Module] base
15
- def self.extended(base)
16
- base.instance_variable_set(:@schema, EMPTY_HASH)
17
- end
18
-
19
15
  # @param [Class] klass
20
16
  def inherited(klass)
21
17
  super
22
18
 
23
- klass.instance_variable_set(:@schema, EMPTY_HASH)
24
19
  klass.equalizer Equalizer.new(*schema.keys)
25
20
  klass.send(:include, klass.equalizer)
26
-
27
- klass.attributes(EMPTY_HASH) unless equal?(Struct)
28
21
  end
29
22
 
30
23
  # Adds an attribute for this {Struct} with given `name` and `type`
@@ -69,9 +62,7 @@ module Dry
69
62
  def attributes(new_schema)
70
63
  check_schema_duplication(new_schema)
71
64
 
72
- prev_schema = schema
73
-
74
- @schema = prev_schema.merge(new_schema)
65
+ schema schema.merge(new_schema)
75
66
  input Types['coercible.hash'].public_send(constructor_type, schema)
76
67
 
77
68
  attr_reader(*new_schema.keys)
@@ -90,12 +81,6 @@ module Dry
90
81
  end
91
82
  private :check_schema_duplication
92
83
 
93
- # @return [Hash{Symbol => Dry::Types::Definition, Dry::Struct}]
94
- def schema
95
- super_schema = superclass.respond_to?(:schema) ? superclass.schema : EMPTY_HASH
96
- super_schema.merge(@schema)
97
- end
98
-
99
84
  # @param [Hash{Symbol => Object},Dry::Struct] attributes
100
85
  # @raise [Struct::Error] if the given attributes don't conform {#schema}
101
86
  # with given {#constructor_type}
@@ -110,7 +95,7 @@ module Dry
110
95
  end
111
96
 
112
97
  # Calls type constructor. The behavior is identical to `.new` but returns
113
- # returns the input back if it's a subclass of the struct.
98
+ # the input back if it's a subclass of the struct.
114
99
  #
115
100
  # @param [Hash{Symbol => Object},Dry::Struct] attributes
116
101
  # @return [Dry::Struct]
@@ -120,6 +105,14 @@ module Dry
120
105
  end
121
106
  alias_method :[], :call
122
107
 
108
+ # @param [#call,nil] constructor
109
+ # @param [Hash] options
110
+ # @param [#call,nil] block
111
+ # @return [Dry::Struct::Constructor]
112
+ def constructor(constructor = nil, **_options, &block)
113
+ Struct::Constructor.new(self, fn: constructor || block)
114
+ end
115
+
123
116
  # Retrieves default attributes from defined {.schema}.
124
117
  # Used in a {Struct} constructor if no attributes provided to {.new}
125
118
  #
@@ -194,6 +187,21 @@ module Dry
194
187
  def optional?
195
188
  false
196
189
  end
190
+
191
+ # Checks if this {Struct} has the given attribute
192
+ #
193
+ # @param [Symbol] key Attribute name
194
+ # @return [Boolean]
195
+ def attribute?(key)
196
+ schema.key?(key)
197
+ end
198
+
199
+ # Gets the list of attribute names
200
+ #
201
+ # @return [Array<Symbol>]
202
+ def attribute_names
203
+ schema.keys
204
+ end
197
205
  end
198
206
  end
199
207
  end
@@ -0,0 +1,28 @@
1
+ module Dry
2
+ class Struct
3
+ class Constructor
4
+ include Dry::Equalizer(:type)
5
+
6
+ # @return [#call]
7
+ attr_reader :fn
8
+
9
+ # @return [#call]
10
+ attr_reader :type
11
+
12
+ # @param [Struct] type
13
+ # @param [Hash] options
14
+ # @param [#call, nil] block
15
+ def initialize(type, options = {}, &block)
16
+ @type = type
17
+ @fn = options.fetch(:fn, block)
18
+ end
19
+
20
+ # @param [Object] input
21
+ # @return [Object]
22
+ def call(input)
23
+ type[fn[input]]
24
+ end
25
+ alias_method :[], :call
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  class Struct
3
- VERSION = '0.3.0'.freeze
3
+ VERSION = '0.3.1'.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.0
4
+ version: 0.3.1
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-05-05 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-equalizer
@@ -154,6 +154,7 @@ files:
154
154
  - ".travis.yml"
155
155
  - ".yardopts"
156
156
  - CHANGELOG.md
157
+ - CONTRIBUTING.md
157
158
  - Gemfile
158
159
  - LICENSE
159
160
  - README.md
@@ -166,6 +167,7 @@ files:
166
167
  - lib/dry-struct.rb
167
168
  - lib/dry/struct.rb
168
169
  - lib/dry/struct/class_interface.rb
170
+ - lib/dry/struct/constructor.rb
169
171
  - lib/dry/struct/errors.rb
170
172
  - lib/dry/struct/hashify.rb
171
173
  - lib/dry/struct/value.rb