Class: Theman::Agency::Columns

Inherits:
Object
  • Object
show all
Defined in:
lib/theman/agency/columns.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ Columns

Returns a new instance of Columns.



7
8
9
10
# File 'lib/theman/agency/columns.rb', line 7

def initialize(conn)
  @connection = conn
  @columns    = []
end

Instance Attribute Details

#column(name, type, *args) ⇒ Object

:nodoc:



28
29
30
# File 'lib/theman/agency/columns.rb', line 28

def column
  @column
end

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/theman/agency/columns.rb', line 5

def connection
  @connection
end

Instance Method Details

#column_to_sql(name, type, options = {}) ⇒ Object

:nodoc:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/theman/agency/columns.rb', line 43

def column_to_sql(name, type, options = {}) #:nodoc:
  sql = [quote_column_name(name)]
  case type
  when 'integer'
    if options[:limit]
      case options[:limit]
      when 1, 2;
        sql << 'smallint'
      when 3, 4;
        sql << 'integer'
      when 5..8;
        sql << 'bigint'
      else
        raise ArgumentError, "No integer type has byte size #{limit}."
      end
    else
      sql << 'integer'
    end
  when 'decimal'
    sql << 'double precision'
  when 'float'
    sql << 'double precision'
  when 'string'
    if options[:limit]
      sql << "character varying(#{options[:limit]})"
    else
      sql << 'character varying(255)'
    end
  when 'binary'
    sql << 'oid'
  when 'time'
    sql << 'time without time zone'
  when 'datetime'
    sql << 'timestamp without time zone'
  when 'timestamp'
    sql << 'timestamp without time zone'
  else
    sql << type
  end

  if options[:null] ==  false
    sql << 'NOT NULL'
  end

  if options[:default]
    sql << "DEFAULT #{options[:default]}"
  end

  sql.join(' ')
end

#include?(sym_col) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/theman/agency/columns.rb', line 39

def include?(sym_col)
  @columns.map{|column| column[0] }.include?(sym_col)
end

#quote_column_name(name) ⇒ Object

:nodoc:



94
95
96
# File 'lib/theman/agency/columns.rb', line 94

def quote_column_name(name) #:nodoc:
  @connection.quote_ident(name.to_s)
end

#symbolize(name) ⇒ Object

:nodoc:



24
25
26
# File 'lib/theman/agency/columns.rb', line 24

def symbolize(name) #:nodoc:
  name.is_a?(Symbol) ? name : name.gsub(/ /,"_").gsub(/\W/, "").downcase.to_sym
end

#to_sqlObject

:nodoc:



12
13
14
# File 'lib/theman/agency/columns.rb', line 12

def to_sql #:nodoc:
  @columns.map{|column| column_to_sql(*column)}.join(', ')
end