Sha256: 804f6ef1e06bdc032bf90ad3c527997cac77eab31729c25a8f920adb8b638333

Contents?: true

Size: 1.79 KB

Versions: 9

Compression:

Stored size: 1.79 KB

Contents

module Steep
  module AST
    module Types
      class Record
        attr_reader :elements, :required_keys

        def initialize(elements:, required_keys:)
          @elements = elements
          @required_keys = required_keys
        end

        def ==(other)
          other.is_a?(Record) && other.elements == elements && other.required_keys == required_keys
        end

        def hash
          self.class.hash ^ elements.hash ^ required_keys.hash
        end

        alias eql? ==

        def subst(s)
          self.class.new(
            elements: elements.transform_values {|type| type.subst(s) },
            required_keys: required_keys
          )
        end

        def to_s
          strings = elements.keys.sort_by(&:to_s).map do |key|
            if optional?(key)
              "?#{key.inspect} => #{elements[key]}"
            else
              "#{key.inspect} => #{elements[key]}"
            end
          end
          "{ #{strings.join(", ")} }"
        end

        def free_variables()
          @fvs ||= Set.new.tap do |set|
            elements.each_value do |type|
              set.merge(type.free_variables)
            end
          end
        end

        include Helper::ChildrenLevel

        def each_child(&block)
          if block
            elements.each_value(&block)
          else
            elements.each_value
          end
        end

        def map_type(&block)
          self.class.new(
            elements: elements.transform_values(&block),
            required_keys: required_keys
          )
        end

        def level
          [0] + level_of_children(elements.values)
        end

        def required?(key)
          required_keys.include?(key)
        end

        def optional?(key)
          !required_keys.include?(key)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
steep-1.9.1 lib/steep/ast/types/record.rb
steep-1.9.0 lib/steep/ast/types/record.rb
steep-1.9.0.dev.2 lib/steep/ast/types/record.rb
steep-1.9.0.dev.1 lib/steep/ast/types/record.rb
steep-1.8.3 lib/steep/ast/types/record.rb
steep-1.8.2 lib/steep/ast/types/record.rb
steep-1.8.1 lib/steep/ast/types/record.rb
steep-1.8.0 lib/steep/ast/types/record.rb
steep-1.8.0.pre.2 lib/steep/ast/types/record.rb