Class: Theman::Agency::Columns
- Inherits:
-
Object
- Object
- Theman::Agency::Columns
- Defined in:
- lib/theman/agency/columns.rb
Instance Attribute Summary collapse
-
#column(name, type, *args) ⇒ Object
:nodoc:.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
-
#column_to_sql(name, type, options = {}) ⇒ Object
:nodoc:.
- #include?(sym_col) ⇒ Boolean
-
#initialize(conn) ⇒ Columns
constructor
A new instance of Columns.
-
#quote_column_name(name) ⇒ Object
:nodoc:.
-
#symbolize(name) ⇒ Object
:nodoc:.
-
#to_sql ⇒ Object
:nodoc:.
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 |
#connection ⇒ Object (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, = {}) #:nodoc: sql = [quote_column_name(name)] case type when 'integer' if [:limit] case [: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 [:limit] sql << "character varying(#{[: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 [:null] == false sql << 'NOT NULL' end if [:default] sql << "DEFAULT #{[:default]}" end sql.join(' ') end |
#include?(sym_col) ⇒ 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_sql ⇒ Object
: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 |