File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 22require 'dry/equalizer'
33
44require 'dry/struct/errors'
5+ require 'dry/struct/constructor'
56
67module Dry
78 class Struct
@@ -120,6 +121,14 @@ def call(attributes = default_attributes)
120121 end
121122 alias_method :[] , :call
122123
124+ # @param [#call,nil] constructor
125+ # @param [Hash] options
126+ # @param [#call,nil] block
127+ # @return [Dry::Struct::Constructor]
128+ def constructor ( constructor = nil , **_options , &block )
129+ Struct ::Constructor . new ( self , fn : constructor || block )
130+ end
131+
123132 # Retrieves default attributes from defined {.schema}.
124133 # Used in a {Struct} constructor if no attributes provided to {.new}
125134 #
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 9090 end
9191 end
9292
93+ describe '.constructor' do
94+ it 'uses constructor function to process input' do
95+ expect ( type . constructor ( &:to_h ) [ jane . to_a ] ) . to be_eql type [ jane ]
96+ end
97+ end
98+
9399 describe '.default?' do
94100 it 'is not a default' do
95101 expect ( type ) . not_to be_default
You can’t perform that action at this time.
0 commit comments