Class: KatakataIrb::Types::UnionType

Inherits:
Object
  • Object
show all
Defined in:
lib/katakata_irb/types.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*types) ⇒ UnionType

Returns a new instance of UnionType.



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/katakata_irb/types.rb', line 252

def initialize(*types)
  @types = []
  singletons = []
  instances = {}
  collect = -> type do
    case type
    in UnionType
      type.types.each(&collect)
    in InstanceType
      params = (instances[type.klass] ||= {})
      type.params.each do |k, v|
        (params[k] ||= []) << v
      end
    in SingletonType
      singletons << type
    end
  end
  types.each(&collect)
  @types = singletons.uniq + instances.map do |klass, params|
    InstanceType.new(klass, params.transform_values { |v| UnionType[*v] })
  end
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



250
251
252
# File 'lib/katakata_irb/types.rb', line 250

def types
  @types
end

Class Method Details

.[](*types) ⇒ Object



287
288
289
290
291
292
293
294
295
296
# File 'lib/katakata_irb/types.rb', line 287

def self.[](*types)
  type = new(*types)
  if type.types.empty?
    OBJECT
  elsif type.types.size == 1
    type.types.first
  else
    type
  end
end

Instance Method Details

#all_methodsObject



299
# File 'lib/katakata_irb/types.rb', line 299

def all_methods() = @types.flat_map(&:all_methods).uniq

#constantsObject



300
# File 'lib/katakata_irb/types.rb', line 300

def constants() = @types.flat_map(&:constants).uniq

#inspectObject



301
# File 'lib/katakata_irb/types.rb', line 301

def inspect() = @types.map(&:inspect).join(' | ')

#methodsObject



298
# File 'lib/katakata_irb/types.rb', line 298

def methods() = @types.flat_map(&:methods).uniq

#nillable?Boolean

Returns:

  • (Boolean)


279
280
281
# File 'lib/katakata_irb/types.rb', line 279

def nillable?
  types.any?(&:nillable?)
end

#nonnillableObject



283
284
285
# File 'lib/katakata_irb/types.rb', line 283

def nonnillable
  UnionType[*types.reject { _1.is_a?(InstanceType) && _1.klass == NilClass }]
end

#transform(&block) ⇒ Object



275
276
277
# File 'lib/katakata_irb/types.rb', line 275

def transform(&block)
  UnionType[*types.map(&block)]
end