Class: NuoDB::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/nuodb/column.rb

Defined Under Namespace

Modules: Format

Constant Summary collapse

TRUE_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].to_set

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default, sql_type = nil, null = true) ⇒ Column

Instantiates a new column in the table.

name is the column’s name, such as supplier_id in supplier_id int(11). default is the type-casted default value, such as new in sales_stage varchar(20) default 'new'. sql_type is used to extract the column’s length, if necessary. For example 60 in company_name varchar(60). It will be mapped to one of the standard Rails SQL types in the type attribute. null determines if this column allows NULL values.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nuodb/column.rb', line 27

def initialize(name, default, sql_type = nil, null = true)
  @name = name
  @sql_type = sql_type
  @null = null
  @limit = extract_limit(sql_type)
  @precision = extract_precision(sql_type)
  @scale = extract_scale(sql_type)
  @type = simplified_type(sql_type)
  @default = extract_default(default)
  @primary = nil
  @coder = nil
end

Instance Attribute Details

#coderObject Also known as: encoded?

Returns the value of attribute coder.



15
16
17
# File 'lib/nuodb/column.rb', line 15

def coder
  @coder
end

#defaultObject (readonly)

Returns the value of attribute default.



14
15
16
# File 'lib/nuodb/column.rb', line 14

def default
  @default
end

#limitObject (readonly)

Returns the value of attribute limit.



14
15
16
# File 'lib/nuodb/column.rb', line 14

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/nuodb/column.rb', line 14

def name
  @name
end

#nullObject (readonly)

Returns the value of attribute null.



14
15
16
# File 'lib/nuodb/column.rb', line 14

def null
  @null
end

#precisionObject (readonly)

Returns the value of attribute precision.



14
15
16
# File 'lib/nuodb/column.rb', line 14

def precision
  @precision
end

#primaryObject

Returns the value of attribute primary.



15
16
17
# File 'lib/nuodb/column.rb', line 15

def primary
  @primary
end

#scaleObject (readonly)

Returns the value of attribute scale.



14
15
16
# File 'lib/nuodb/column.rb', line 14

def scale
  @scale
end

#sql_typeObject (readonly)

Returns the value of attribute sql_type.



14
15
16
# File 'lib/nuodb/column.rb', line 14

def sql_type
  @sql_type
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/nuodb/column.rb', line 14

def type
  @type
end

Instance Method Details

#extract_default(default) ⇒ Object



81
82
83
# File 'lib/nuodb/column.rb', line 81

def extract_default(default)
  type_cast(default)
end

#has_default?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/nuodb/column.rb', line 50

def has_default?
  !default.nil?
end

#klassObject

Returns the Ruby class that corresponds to the abstract data type.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nuodb/column.rb', line 55

def klass
  case type
    when :integer then
      Fixnum
    when :float then
      Float
    when :decimal then
      BigDecimal
    when :datetime, :timestamp, :time then
      Time
    when :date then
      Date
    when :text, :string, :binary then
      String
    when :boolean then
      Object
  end
end

#number?Boolean

Returns true if the column is either of type integer, float or decimal.

Returns:

  • (Boolean)


46
47
48
# File 'lib/nuodb/column.rb', line 46

def number?
  type == :integer || type == :float || type == :decimal
end

#text?Boolean

Returns true if the column is either of type string or text.

Returns:

  • (Boolean)


41
42
43
# File 'lib/nuodb/column.rb', line 41

def text?
  type == :string || type == :text
end

#type_cast(value) ⇒ Object

Casts value (which is a String) to an appropriate instance.



75
76
77
78
79
# File 'lib/nuodb/column.rb', line 75

def type_cast(value)
  return nil if value.nil?
  return coder.load(value) if encoded?
  value
end