Class: Vmit::TypedRegistry

Inherits:
Registry show all
Defined in:
lib/vmit/registry.rb

Overview

Add types to keys You need to inherit from this class

Examples:

class MyTypes < TypedRegistry
  type :key1, Fixnum
  type :key2, Float
end

reg = MyTypes.new(backing_registry)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ TypedRegistry

Returns a new instance of TypedRegistry.



94
95
96
# File 'lib/vmit/registry.rb', line 94

def initialize(registry)
  @registry = registry
end

Class Method Details

.type(key, t = nil) ⇒ Object



87
88
89
90
91
# File 'lib/vmit/registry.rb', line 87

def type(key, t=nil)
  @type_info ||= Hash.new
  @type_info[key] = t unless t.nil?
  @type_info[key]
end

Instance Method Details

#[](key) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/vmit/registry.rb', line 102

def [](key)
  rawval = @registry[key]
  case type(key).to_s
    when 'String' then rawval.to_s
    when 'Fixnum' then rawval.to_i
    when 'Float' then rawval.to_f
    else rawval
  end
end

#[]=(key, val) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/vmit/registry.rb', line 112

def []=(key, val)
  if type(key)
    unless val.is_a?(type(key))
      raise TypeError.new("Expected #{type(key)} for #{key}")
    end
  end
  @registry[key] = val
end

#type(key) ⇒ Object



98
99
100
# File 'lib/vmit/registry.rb', line 98

def type(key)
  self.class.type(key)
end