Class: Rstruct::Registry
- Inherits:
-
Object
- Object
- Rstruct::Registry
- Defined in:
- lib/rstruct/registry.rb
Constant Summary collapse
- DEFAULT_REGISTRY =
new(:default)
Instance Attribute Summary collapse
-
#inherits ⇒ Object
readonly
Returns the value of attribute inherits.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #get(typ) ⇒ Object (also: #[])
-
#initialize(name, *inherits) ⇒ Registry
constructor
A new instance of Registry.
- #register(typ, *names) ⇒ Object
- #set(n, typ) ⇒ Object (also: #[]=)
- #typedef(p, t, opts = {}) ⇒ Object
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
#inherits ⇒ Object (readonly)
Returns the value of attribute inherits.
11 12 13 |
# File 'lib/rstruct/registry.rb', line 11 def inherits @inherits end |
#name ⇒ Object (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 |