Class: Rstruct::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/rstruct/registry.rb

Constant Summary collapse

DEFAULT_REGISTRY =
new(:default)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *inherits) ⇒ Registry

Returns a new instance of Registry.



13
14
15
16
17
18
19
20
21
22
# File 'lib/rstruct/registry.rb', line 13

def initialize(name, *inherits)
  @name = name.to_sym
  @registry = Hash.new()

  @inherits = []
  if @name != :default
    @inherits << DEFAULT_REGISTRY 
  end
  @inherits.concat(inherits).uniq!
end

Instance Attribute Details

#inheritsObject (readonly)

Returns the value of attribute inherits.



11
12
13
# File 'lib/rstruct/registry.rb', line 11

def inherits
  @inherits
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/rstruct/registry.rb', line 11

def name
  @name
end

Instance Method Details

#get(typ) ⇒ Object Also known as: []



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rstruct/registry.rb', line 32

def get(typ)
  if t=@registry[typ] 
    return t
  else
    @inherits.each do |inc|
      if t=inc.get(typ)
        return t
      end
    end
    return nil
  end
end

#register(typ, *names) ⇒ Object



58
59
60
61
62
# File 'lib/rstruct/registry.rb', line 58

def register(typ, *names)
  names.each do |n|
    set(n.to_sym,typ)
  end
end

#set(n, typ) ⇒ Object Also known as: []=



47
48
49
50
51
52
53
54
# File 'lib/rstruct/registry.rb', line 47

def set(n,typ)
  if n.nil?
    raise(TypeConflictError, "can't register nil type")
  elsif v=get(n)
    raise(TypeConflictError, "type already registered: #{n} => #{v.inspect}" ) 
  end
  @registry[n]=typ
end

#typedef(p, t, opts = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rstruct/registry.rb', line 24

def typedef(p,t,opts={})
  if (pt = get(p))
    set(t, pt)
  else
    raise(InvalidTypeError, "unknown type: #{p}")
  end
end