Module: ModNormalizedHash::ClassMethods

Included in:
NormalizedHash
Defined in:
lib/doodle/normalized_hash.rb,
lib/doodle/normalized_hash.rb

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Object



187
188
189
# File 'lib/doodle/normalized_hash.rb', line 187

def [](*args)
  new(Hash[*args])
end

#MultiTypedHash(*klasses, &block) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/doodle/normalized_hash.rb', line 255

def MultiTypedHash(*klasses, &block)
  typed_class = Class.new(NormalizedHash) do
    # note: cannot take a block
    if block_given?
      define_method :normalize_value, &block
    else
      define_method :normalize_value do |v|
        if !klasses.any?{ |klass| v.kind_of?(klass) }
          raise TypeError, "#{self.class}: #{v.class}(#{v.inspect}) is not a kind of #{klasses.map{ |c| c.to_s }.join(', ')}", [caller[-1]]
        end
        v
      end
    end
  end
  typed_class
end