Class: Perpetuity::Postgres::Table::Attribute
- Inherits:
-
Object
- Object
- Perpetuity::Postgres::Table::Attribute
- Defined in:
- lib/perpetuity/postgres/table/attribute.rb
Constant Summary collapse
- NoDefaultValue =
Module.new
- UUID =
Module.new
- SQL_TYPE_MAP =
{ String => 'TEXT', Integer => 'BIGINT', Fixnum => 'BIGINT', Bignum => 'NUMERIC', BigDecimal => 'NUMERIC', Float => 'FLOAT', UUID => 'UUID', Time => 'TIMESTAMPTZ', Date => 'DATE', TrueClass => 'BOOLEAN', FalseClass => 'BOOLEAN' }.tap{|m| m.default = 'JSON' }
Instance Attribute Summary collapse
-
#max_length ⇒ Object
readonly
Returns the value of attribute max_length.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #default ⇒ Object
-
#initialize(name, type, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #primary_key? ⇒ Boolean
- #sql_declaration ⇒ Object
- #sql_type ⇒ Object
Constructor Details
#initialize(name, type, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/perpetuity/postgres/table/attribute.rb', line 26 def initialize name, type, ={} @name = name @type = type @max_length = [:max_length] @primary_key = if @name.to_s == 'id' true else .fetch(:primary_key) { false } end @default = .fetch(:default) { NoDefaultValue } end |
Instance Attribute Details
#max_length ⇒ Object (readonly)
Returns the value of attribute max_length.
7 8 9 |
# File 'lib/perpetuity/postgres/table/attribute.rb', line 7 def max_length @max_length end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/perpetuity/postgres/table/attribute.rb', line 7 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/perpetuity/postgres/table/attribute.rb', line 7 def type @type end |
Instance Method Details
#default ⇒ Object
60 61 62 |
# File 'lib/perpetuity/postgres/table/attribute.rb', line 60 def default @default end |
#primary_key? ⇒ Boolean
56 57 58 |
# File 'lib/perpetuity/postgres/table/attribute.rb', line 56 def primary_key? !!@primary_key end |
#sql_declaration ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/perpetuity/postgres/table/attribute.rb', line 42 def sql_declaration if self.default.is_a? String default = "'#{self.default}'" else default = self.default end sql = "#{name} #{sql_type}" sql << ' PRIMARY KEY' if primary_key? sql << " DEFAULT #{default}" unless self.default == NoDefaultValue sql end |
#sql_type ⇒ Object
38 39 40 |
# File 'lib/perpetuity/postgres/table/attribute.rb', line 38 def sql_type SQL_TYPE_MAP[type] end |