Class: DataMapper::Mappings::Column

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

Overview

TODO: There are of course many more options to add here. Ordinal, Length/Size, Nullability are just a few.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, name, type, options = {}) ⇒ Column

Returns a new instance of Column.



10
11
12
13
# File 'lib/data_mapper/mappings/column.rb', line 10

def initialize(database, name, type, options = {})
  @database = database
  @name, @type, @options = name.to_sym, type, options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/data_mapper/mappings/column.rb', line 8

def name
  @name
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/data_mapper/mappings/column.rb', line 8

def options
  @options
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/data_mapper/mappings/column.rb', line 8

def type
  @type
end

Instance Method Details

#column_nameObject



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

def column_name
  @column_name || (@column_name = (@options.has_key?(:column) ? @options[:column].to_s : name.to_s.gsub(/\?$/, '')).freeze)
end

#inspectObject



71
72
73
# File 'lib/data_mapper/mappings/column.rb', line 71

def inspect
  "#<%s:0x%x @name=%s, @type=%s, @options=%s>" % [self.class.name, (object_id * 2), to_sql, type.inspect, options.inspect]
end

#instance_variable_nameObject



38
39
40
# File 'lib/data_mapper/mappings/column.rb', line 38

def instance_variable_name
  @instance_variable_name || (@instance_variable_name = "@#{@name.to_s.gsub(/\?$/, '')}".freeze)
end

#key?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/data_mapper/mappings/column.rb', line 30

def key?
  @options[:key] || false
end

#lazy=(value) ⇒ Object



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

def lazy=(value)
  @options[:lazy] = value
end

#lazy?Boolean

Determines if the field should be lazy loaded. You can set this explicitly, or accept the default, which is false for all but text fields.

Returns:

  • (Boolean)


22
23
24
# File 'lib/data_mapper/mappings/column.rb', line 22

def lazy?
  @options[:lazy] || (type == :text)
end

#nullable?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/data_mapper/mappings/column.rb', line 26

def nullable?
  @options[:nullable] || true
end

#sizeObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/data_mapper/mappings/column.rb', line 58

def size
  @size || begin
    return @size = @options[:size] if @options.has_key?(:size)
    return @size = @options[:length] if @options.has_key?(:length)
    
    @size = case type
      when :integer then 4
      when :string, :class then 50
      else nil
    end
  end
end

#to_sObject



42
43
44
# File 'lib/data_mapper/mappings/column.rb', line 42

def to_s
  @name.to_s
end

#to_sqlObject



54
55
56
# File 'lib/data_mapper/mappings/column.rb', line 54

def to_sql
  @to_sql || (@to_sql = @database.quote_column_name(column_name).freeze)
end

#to_symObject



34
35
36
# File 'lib/data_mapper/mappings/column.rb', line 34

def to_sym
  @name
end

#type_cast_value(value) ⇒ Object



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

def type_cast_value(value)
  @database.type_cast_value(type, value)
end