Sha256: 658d6757bde6e963a4b72241d01bdfa426ec926055bf280083d6b558018fa269

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

Contents

module Mutest
  # 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 # Mutest

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mutest-0.0.9 lib/mutest/registry.rb
mutest-0.0.8 lib/mutest/registry.rb
mutest-0.0.7 lib/mutest/registry.rb
mutest-0.0.6 lib/mutest/registry.rb
mutest-0.0.5 lib/mutest/registry.rb
mutest-0.0.4 lib/mutest/registry.rb
mutest-0.0.3 lib/mutest/registry.rb
mutest-0.0.2 lib/mutest/registry.rb