Sha256: 1dedbb4e8390223e610d16bfcef5667f05f7f129b6aec21d6857893c5c608289

Contents?: true

Size: 1.06 KB

Versions: 16

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Mutant
  # Registry for mapping AST types to classes
  class Registry
    include Concord.new(:contents)

    # Initialize object
    #
    # @return [undefined]
    def initialize
      super({})
    end

    # Raised when the type is an invalid type
    RegistryError = Class.new(TypeError)

    # Register class for AST node class
    #
    # @param [Symbol] type
    # @param [Class] class
    #
    # @return [self]
    def register(type, klass)
      fail RegistryError, "Invalid type registration: #{type.inspect}" unless AST::Types::ALL.include?(type)
      fail RegistryError, "Duplicate type registration: #{type.inspect}" if contents.key?(type)
      contents[type] = klass
      self
    end

    # Lookup class for node
    #
    # @param [Symbol] type
    #
    # @return [Class]
    #
    # @raise [ArgumentError]
    #   raises argument error when class cannot be found
    def lookup(type)
      contents.fetch(type) do
        fail RegistryError, "No entry for: #{type.inspect}"
      end
    end

  end # Registry
end # Mutant

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mutant-0.9.7 lib/mutant/registry.rb
mutant-0.9.6 lib/mutant/registry.rb
mutant-0.9.5 lib/mutant/registry.rb
mutant-0.9.4 lib/mutant/registry.rb
mutant-0.9.3 lib/mutant/registry.rb
mutant-0.9.2 lib/mutant/registry.rb
mutant-0.9.1 lib/mutant/registry.rb
mutant-0.9.0 lib/mutant/registry.rb
mutant-0.8.24 lib/mutant/registry.rb
mutant-0.8.23 lib/mutant/registry.rb
mutant-0.8.22 lib/mutant/registry.rb
mutant-0.8.21 lib/mutant/registry.rb
mutant-0.8.20 lib/mutant/registry.rb
mutant-0.8.19 lib/mutant/registry.rb
mutant-0.8.18 lib/mutant/registry.rb
mutant-0.8.17 lib/mutant/registry.rb