File: nominal.rb

package info (click to toggle)
ruby-dry-types 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 504 kB
  • sloc: ruby: 3,059; makefile: 4
file content (210 lines) | stat: -rw-r--r-- 4,322 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# frozen_string_literal: true

require 'dry/core/deprecations'
require 'dry/types/builder'
require 'dry/types/result'
require 'dry/types/options'
require 'dry/types/meta'

module Dry
  module Types
    # Nominal types define a primitive class and do not apply any constructors or constraints
    #
    # Use these types for annotations and the base for building more complex types on top of them.
    #
    # @api public
    class Nominal
      include Type
      include Options
      include Meta
      include Builder
      include Printable
      include Dry::Equalizer(:primitive, :options, :meta, inspect: false, immutable: true)

      # @return [Class]
      attr_reader :primitive

      # @param [Class] primitive
      #
      # @return [Type]
      #
      # @api private
      def self.[](primitive)
        if primitive == ::Array
          Types::Array
        elsif primitive == ::Hash
          Types::Hash
        else
          self
        end
      end

      ALWAYS = proc { true }

      # @param [Type,Class] primitive
      # @param [Hash] options
      #
      # @api private
      def initialize(primitive, **options)
        super
        @primitive = primitive
        freeze
      end

      # @return [String]
      #
      # @api public
      def name
        primitive.name
      end

      # @return [false]
      #
      # @api public
      def default?
        false
      end

      # @return [false]
      #
      # @api public
      def constrained?
        false
      end

      # @return [false]
      #
      # @api public
      def optional?
        false
      end

      # @param [BasicObject] input
      #
      # @return [BasicObject]
      #
      # @api private
      def call_unsafe(input)
        input
      end

      # @param [BasicObject] input
      #
      # @return [BasicObject]
      #
      # @api private
      def call_safe(input)
        input
      end

      # @param [Object] input
      # @param [#call,nil] block
      #
      # @yieldparam [Failure] failure
      # @yieldreturn [Result]
      #
      # @return [Result,Logic::Result] when a block is not provided
      # @return [nil] otherwise
      #
      # @api public
      def try(input)
        success(input)
      end

      # @param (see Dry::Types::Success#initialize)
      #
      # @return [Result::Success]
      #
      # @api public
      def success(input)
        Result::Success.new(input)
      end

      # @param (see Failure#initialize)
      #
      # @return [Result::Failure]
      #
      # @api public
      def failure(input, error)
        raise ArgumentError, 'error must be a CoercionError' unless error.is_a?(CoercionError)

        Result::Failure.new(input, error)
      end

      # Checks whether value is of a #primitive class
      #
      # @param [Object] value
      #
      # @return [Boolean]
      #
      # @api public
      def primitive?(value)
        value.is_a?(primitive)
      end

      # @api private
      def coerce(input, &_block)
        if primitive?(input)
          input
        elsif block_given?
          yield
        else
          raise CoercionError, "#{input.inspect} must be an instance of #{primitive}"
        end
      end

      # @api private
      def try_coerce(input)
        result = success(input)

        coerce(input) do
          result = failure(
            input,
            CoercionError.new("#{input.inspect} must be an instance of #{primitive}")
          )
        end

        if block_given?
          yield(result)
        else
          result
        end
      end

      # Return AST representation of a type nominal
      #
      # @return [Array]
      #
      # @api public
      def to_ast(meta: true)
        [:nominal, [primitive, meta ? self.meta : EMPTY_HASH]]
      end

      # Return self. Nominal types are lax by definition
      #
      # @return [Nominal]
      #
      # @api public
      def lax
        self
      end

      # Wrap the type with a proc
      #
      # @return [Proc]
      #
      # @api public
      def to_proc
        ALWAYS
      end
    end

    extend Dry::Core::Deprecations[:'dry-types']
    Definition = Nominal
    deprecate_constant(:Definition, message: 'Nominal')
  end
end

require 'dry/types/array'
require 'dry/types/hash'
require 'dry/types/map'