Class: RBS::Dynamic::Builder::Types

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/dynamic/builder/types.rb

Defined Under Namespace

Modules: RBSTypeMerger, ToRBSType

Constant Summary collapse

BOOL =
RBS::Types::Bases::Bool.new(location: nil)
VOID =
RBS::Types::Bases::Void.new(location: nil)
ANY =
RBS::Types::Bases::Any.new(location: nil)
NILCLASS_RBS_TYPE =
NilClass.to_rbs_type
NIL_RBS_TYPE =
nil.to_rbs_type
TRUECLASS_RBS_TYPE =
TrueClass.to_rbs_type
TRUE_RBS_TYPE =
true.to_rbs_type
FALSECLASS_RBS_TYPE =
FalseClass.to_rbs_type
FALSE_RBS_TYPE =
false.to_rbs_type

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Types

Returns a new instance of Types.



149
150
151
152
153
154
155
156
157
158
# File 'lib/rbs/dynamic/builder/types.rb', line 149

def initialize(type)
  @types = case type
           when nil
             [nil]
           when Hash
             [type]
           else
             Array(type)
           end.map(&:to_rbs_type).uniq.zip_type
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



147
148
149
# File 'lib/rbs/dynamic/builder/types.rb', line 147

def types
  @types
end

Instance Method Details

#buildObject



160
161
162
163
164
165
166
167
168
169
# File 'lib/rbs/dynamic/builder/types.rb', line 160

def build
  if optional?
    RBS::Types::Optional.new(
      type: (target_types - [NIL_RBS_TYPE, NILCLASS_RBS_TYPE]).union_type,
      location: nil
    )
  else
    target_types.union_type
  end
end

#in_bool?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/rbs/dynamic/builder/types.rb', line 184

def in_bool?
  0 < types.count { _1 == TRUE_RBS_TYPE || _1 == TRUECLASS_RBS_TYPE } && 0 < types.count { _1 == FALSE_RBS_TYPE || _1 == FALSECLASS_RBS_TYPE }
end

#optional?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/rbs/dynamic/builder/types.rb', line 180

def optional?
  !!types.group_by { _1 == NIL_RBS_TYPE || _1 == NILCLASS_RBS_TYPE }.values.then { |opt, other| opt&.size&.>=(1) && other&.size&.>=(1) }
end

#target_typesObject



188
189
190
191
192
# File 'lib/rbs/dynamic/builder/types.rb', line 188

def target_types
  (types + types.flat_map { Array(_1[:value]).map { |value| { type: value, args: [] } } })
    .tap { break _1 - [NIL_RBS_TYPE, NILCLASS_RBS_TYPE] if optional? }
    .tap { break _1 - [TRUE_RBS_TYPE, TRUECLASS_RBS_TYPE, FALSE_RBS_TYPE, FALSECLASS_RBS_TYPE] + [BOOL] if in_bool? }
end