Class | DBI::ColumnInfo |
In: |
lib/dbi/columninfo.rb
|
Parent: | DelegateClass(Hash) |
This represents metadata for columns within a given table, such as the data type, whether or not the the column is a primary key, etc.
ColumnInfo is a delegate of Hash, but represents its keys indifferently, coercing all strings to symbols. It also has ostruct-like features, f.e.:
h = ColumnInfo.new({ "foo" => "bar" }) h[:foo] => "bar" h["foo"] => "bar" h.foo => "bar"
All of these forms have assignment forms as well.
Create a new ColumnInfo object.
If no Hash is provided, one will be created for you. The hash will be shallow cloned for storage inside the object, and an attempt will be made to convert all string keys to symbols.
In the event that both string and symbol keys are provided in the initial hash, we cannot safely route around collisions and therefore a TypeError is raised.
# File lib/dbi/columninfo.rb, line 37 37: def initialize(hash=nil) 38: @hash = hash.dup rescue nil 39: @hash ||= Hash.new 40: 41: # coerce all strings to symbols 42: @hash.each_key do |x| 43: if x.kind_of? String 44: sym = x.to_sym 45: if @hash.has_key? sym 46: raise ::TypeError, 47: "#{self.class.name} may construct from a hash keyed with strings or symbols, but not both" 48: end 49: @hash[sym] = @hash[x] 50: @hash.delete(x) 51: end 52: end 53: 54: super(@hash) 55: end
# File lib/dbi/columninfo.rb, line 65 65: def default() # :nodoc; XXX hack to get around Hash#default 66: method_missing(:default) 67: end