Class: MassInsert::Builder::Adapters::Helpers::ColumnValue

Inherits:
Object
  • Object
show all
Defined in:
lib/mass_insert/builder/adapters/helpers/column_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, column, class_name) ⇒ ColumnValue

Returns a new instance of ColumnValue.



9
10
11
12
13
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 9

def initialize row, column, class_name
  @row        = row
  @column     = column
  @class_name = class_name
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



7
8
9
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 7

def class_name
  @class_name
end

#columnObject

Returns the value of attribute column.



7
8
9
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 7

def column
  @column
end

#rowObject

Returns the value of attribute row.



7
8
9
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 7

def row
  @row
end

Instance Method Details

#buildObject

Returns the valid column value to be included in the query. The value depends of the database engine.



40
41
42
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 40

def build
  column_value.nil? ? default_value : send(:"column_value_#{column_type}")
end

#column_typeObject

Returns a symbol with the column type according to the database.



16
17
18
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 16

def column_type
  class_name.columns_hash[@column.to_s].type
end

#column_valueObject

Returns the value to this column in the row hash. The value is finding by symbol or string key to be most flexible.



22
23
24
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 22

def column_value
  row.fetch(column){row[@column.to_s]}
end

#column_value_booleanObject

Returns the correct value to boolean column. This column calls the correct method according to the database adapter.



69
70
71
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 69

def column_value_boolean
  self.send(:"#{Utilities.adapter}_column_value_boolean")
end

#column_value_decimalObject Also known as: column_value_float

Returns the correct value to decimal or float column.



62
63
64
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 62

def column_value_decimal
  column_value.to_f.to_s
end

#column_value_integerObject

Returns the correct value to integer column.



57
58
59
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 57

def column_value_integer
  column_value.to_i.to_s
end

#column_value_stringObject Also known as: column_value_text, column_value_date, column_value_time, column_value_datetime, column_value_timestamp, column_value_binary

Returns the correct value when column value is string, text, date, time, datetime, timestamp.



46
47
48
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 46

def column_value_string
  "'#{column_value}'"
end

#default_db_valueObject

Returns the default database column value using methods that ActiveRecord provides.



34
35
36
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 34

def default_db_value
  class_name.columns_hash[@column.to_s].default
end

#default_valueObject

Returns the default column value and it’s added to the query if the row hash does not contains column value.



28
29
30
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 28

def default_value
  default_db_value ? default_db_value.to_s : "null"
end

#mysql2_column_value_booleanObject Also known as: postgresql_column_value_boolean

Returns the column value to boolean column to mysql, postgresql and sqlserver databases.



75
76
77
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 75

def mysql2_column_value_boolean
  column_value ? "true" : "false"
end

#sqlite3_column_value_booleanObject Also known as: sqlserver_column_value_boolean

Returns the column value to boolean column to sqlite database.



81
82
83
# File 'lib/mass_insert/builder/adapters/helpers/column_value.rb', line 81

def sqlite3_column_value_boolean
  column_value ? "1" : "0"
end