Class: CsvToSqlite::SQL::Column

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

Instance Method Summary collapse

Constructor Details

#initialize(csv_table:) ⇒ Column

Returns a new instance of Column.



5
6
7
# File 'lib/sql/column.rb', line 5

def initialize csv_table:
  @csv_table = csv_table
end

Instance Method Details

#boolean?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/sql/column.rb', line 42

def boolean?
  match_cases = @data.map do |value|
    return true if value == 't' or value == 'f' or value == 'true' or value == 'false'
    return false
  end
  match_cases.all { |value| value == true }
end

#date?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sql/column.rb', line 50

def date?
  match_cases = @data.map do |value|
    begin
      Date.parse value
      return true unless value.include? ":"
      return false
    rescue ArgumentError
      return false
    end
  end
  match_cases.all { |value| value == true }
end

#datetime?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sql/column.rb', line 63

def datetime?
  match_cases = @data.map do |value|
    begin
      DateTime.parse value
      return true if value.include? ":"
      return false
    rescue ArgumentError
      return false
    end
  end
  match_cases.all { |value| value == true }
end

#float?Boolean

Returns:

  • (Boolean)


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

def float?
  meta_comparing :Float
end

#int?Boolean

Returns:

  • (Boolean)


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

def int?
  meta_comparing :Integer
end

#meta_comparing(type) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sql/column.rb', line 76

def meta_comparing type
  match_cases = @data.map do |value|
    begin
      method = Kernel.method(type)
      method.call value
      return true
    rescue ArgumentError
      return false
    end
  end
  match_cases.all { |value| value == true }
end

#null?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/sql/column.rb', line 29

def null?
  match_cases = @data.map { |value| value == nil }
  match_cases.any? { |value| value == true }
end

#sql_for(column) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sql/column.rb', line 9

def sql_for column
  @data = @csv_table[column][0..2]
  if null?
    type = "TEXT DEFAULT NULL"
  elsif int?
    type = "INTEGER"
  elsif float?
    type = "FLOAT"
  elsif boolean?
    type = "BOOLEAN"
  elsif datetime?
    type = "DATETIME"
  elsif date?
    type = "DATE"
  else
    type = "TEXT"
  end
  "#{column} #{type},"
end