Sha256: 33360a8e159283b7cba28d9c6d28ea2863e5bbcb53569e4233cac5cc868858b6

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

module Qrb
  #
  # A System is a collection of named Q types.
  #
  class System

    def initialize(types = {}, main = nil)
      @types = types
      @main  = main
    end
    attr_accessor :main

    DSL_METHODS.each do |dsl_method|
      define_method(dsl_method){|*args, &bl|
        factory.public_send(dsl_method, *args, &bl)
      }
    end

    def add_type(type)
      unless type.is_a?(Type)
        raise ArgumentError, "Qrb::Type expected, got `#{type}`"
      end

      if @types.has_key?(type.name)
        raise Error, "Duplicate type name `#{type.name}`"
      end

      @types[type.name] = type
    end

    def get_type(name)
      @types[name]
    end
    alias :[] :get_type

    def fetch(name, &bl)
      @types.fetch(name, &bl)
    end

    def factory
      @factory ||= TypeFactory.new
    end

    def dress(*args, &bl)
      raise Error, "No main type." unless main
      main.dress(*args, &bl)
    end

    def parse(source)
      require "qrb/syntax"
      Syntax.compile(source, self.dup)
    end

    def inspect
      @types.each_pair.map{|k,v| "#{k} = #{v}" }.join("\n")
    end

    def dup
      System.new(@types.dup, @main)
    end

  end # class System
end # module Qrb

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qrb-0.3.0 lib/qrb/system.rb
qrb-0.2.0 lib/qrb/system.rb
qrb-0.1.0 lib/qrb/system.rb