Class: RBS::Types::Record

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all_fields: nil, fields: nil, location:) ⇒ Record

Returns a new instance of Record.



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/rbs/types.rb', line 521

def initialize(all_fields: nil, fields: nil, location:)
  case
  when fields && all_fields.nil?
    @all_fields = fields.map { |k, v| [k, [v, true]] }.to_h
    @fields = fields
    @optional_fields = {}
  when all_fields && fields.nil?
    @all_fields = all_fields
    @fields = all_fields.filter_map { |k, (v, required)| [k, v] if required }.to_h
    @optional_fields = all_fields.filter_map { |k, (v, required)| [k, v] unless required }.to_h
  else
    raise ArgumentError, "only one of `:fields` or `:all_fields` is requireds"
  end

  @location = location
end

Instance Attribute Details

#all_fieldsObject (readonly)

Returns the value of attribute all_fields.



518
519
520
# File 'lib/rbs/types.rb', line 518

def all_fields
  @all_fields
end

#fieldsObject (readonly)

Returns the value of attribute fields.



518
519
520
# File 'lib/rbs/types.rb', line 518

def fields
  @fields
end

#locationObject (readonly)

Returns the value of attribute location.



519
520
521
# File 'lib/rbs/types.rb', line 519

def location
  @location
end

#optional_fieldsObject (readonly)

Returns the value of attribute optional_fields.



518
519
520
# File 'lib/rbs/types.rb', line 518

def optional_fields
  @optional_fields
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



538
539
540
# File 'lib/rbs/types.rb', line 538

def ==(other)
  other.is_a?(Record) && other.fields == fields && other.optional_fields == optional_fields
end

#each_type(&block) ⇒ Object



586
587
588
589
590
591
592
593
# File 'lib/rbs/types.rb', line 586

def each_type(&block)
  if block
    fields.each_value(&block)
    optional_fields.each_value(&block)
  else
    enum_for :each_type
  end
end

#free_variables(set = Set.new) ⇒ Object



548
549
550
551
552
553
554
555
556
557
# File 'lib/rbs/types.rb', line 548

def free_variables(set = Set.new)
  set.tap do
    fields.each_value do |type|
      type.free_variables set
    end
    optional_fields.each_value do |type|
      type.free_variables set
    end
  end
end

#has_classish_type?Boolean

Returns:

  • (Boolean)


617
618
619
# File 'lib/rbs/types.rb', line 617

def has_classish_type?
  each_type.any? {|type| type.has_classish_type? }
end

#has_self_type?Boolean

Returns:

  • (Boolean)


613
614
615
# File 'lib/rbs/types.rb', line 613

def has_self_type?
  each_type.any? {|type| type.has_self_type? }
end

#hashObject



544
545
546
# File 'lib/rbs/types.rb', line 544

def hash
  self.class.hash ^ all_fields.hash
end

#map_type(&block) ⇒ Object



602
603
604
605
606
607
608
609
610
611
# File 'lib/rbs/types.rb', line 602

def map_type(&block)
  if block
    Record.new(
      all_fields: all_fields.transform_values {|type, required| [yield(type), required] },
      location: location
    )
  else
    enum_for :map_type
  end
end

#map_type_name(&block) ⇒ Object



595
596
597
598
599
600
# File 'lib/rbs/types.rb', line 595

def map_type_name(&block)
  Record.new(
    all_fields: all_fields.transform_values {|ty, required| [ty.map_type_name(&block), required] },
    location: location
  )
end

#sub(s) ⇒ Object



563
564
565
566
567
568
# File 'lib/rbs/types.rb', line 563

def sub(s)
  self.class.new(
    all_fields: all_fields.transform_values {|ty, required| [ty.sub(s), required] },
    location: location
  )
end

#to_json(state = _ = nil) ⇒ Object



559
560
561
# File 'lib/rbs/types.rb', line 559

def to_json(state = _ = nil)
  { class: :record, fields: fields, optional_fields: optional_fields, location: location }.to_json(state)
end

#to_s(level = 0) ⇒ Object



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/rbs/types.rb', line 570

def to_s(level = 0)
  return "{ }" if all_fields.empty?

  fields = all_fields.map do |key, (type, required)|
    field = if key.is_a?(Symbol) && key.match?(/\A[A-Za-z_][A-Za-z_0-9]*\z/)
      "#{key}: #{type}"
    else
      "#{key.inspect} => #{type}"
    end

    field = "?#{field}" unless required
    field
  end
  "{ #{fields.join(", ")} }"
end

#with_nonreturn_void?Boolean

Returns:

  • (Boolean)


621
622
623
# File 'lib/rbs/types.rb', line 621

def with_nonreturn_void?
  each_type.any? {|type| type.with_nonreturn_void? }
end