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.

Methods

[]   []=   default   method_missing   new  

Public Class methods

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.

[Source]

    # 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

Public Instance methods

[Source]

    # File lib/dbi/columninfo.rb, line 57
57:         def [](key)
58:             @hash[key.to_sym]
59:         end

[Source]

    # File lib/dbi/columninfo.rb, line 61
61:         def []=(key, value)
62:             @hash[key.to_sym] = value
63:         end

[Source]

    # File lib/dbi/columninfo.rb, line 65
65:         def default() # :nodoc; XXX hack to get around Hash#default
66:             method_missing(:default)
67:         end

[Source]

    # File lib/dbi/columninfo.rb, line 69
69:         def method_missing(sym, value=nil)
70:             if sym.to_s =~ /=$/
71:                 sym = sym.to_s.sub(/=$/, '').to_sym
72:                 @hash[sym] = value
73:             elsif sym.to_s =~ /\?$/
74:                 sym = sym.to_s.sub(/\?$/, '').to_sym
75:                 @hash[sym]
76:             else
77:                 @hash[sym]
78:             end
79:         end

[Validate]