Class: Qrb::System

Inherits:
Object
  • Object
show all
Defined in:
lib/qrb/system.rb

Overview

A System is a collection of named Q types.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types = {}, main = nil) ⇒ System

Returns a new instance of System.



7
8
9
10
# File 'lib/qrb/system.rb', line 7

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

Instance Attribute Details

#mainObject

Returns the value of attribute main.



11
12
13
# File 'lib/qrb/system.rb', line 11

def main
  @main
end

Instance Method Details

#add_type(type) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/qrb/system.rb', line 19

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

#dress(*args, &bl) ⇒ Object

Raises:



44
45
46
47
# File 'lib/qrb/system.rb', line 44

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

#dupObject



58
59
60
# File 'lib/qrb/system.rb', line 58

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

#factoryObject



40
41
42
# File 'lib/qrb/system.rb', line 40

def factory
  @factory ||= TypeFactory.new
end

#fetch(name, &bl) ⇒ Object



36
37
38
# File 'lib/qrb/system.rb', line 36

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

#get_type(name) ⇒ Object Also known as: []



31
32
33
# File 'lib/qrb/system.rb', line 31

def get_type(name)
  @types[name]
end

#inspectObject



54
55
56
# File 'lib/qrb/system.rb', line 54

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

#parse(source) ⇒ Object



49
50
51
52
# File 'lib/qrb/system.rb', line 49

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