Class: Column

Inherits:
Object
  • Object
show all
Includes:
MDBTools
Defined in:
lib/active_mdb/column.rb

Constant Summary

Constants included from MDBTools

MDBTools::BACKENDS, MDBTools::DELIMITER, MDBTools::LINEBREAK, MDBTools::SANITIZER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MDBTools

#arrays_to_hashes, #backends, #check_file, #check_table, #compile_conditions, #delimited_to_arrays, #describe_table, #faked_count, #field_names_for, #mdb_export, #mdb_schema, #mdb_sql, #mdb_tables, #mdb_truth, #mdb_version, #methodize, #sanitize!, #sql_select_where, #table_to_csv, #valid_file?

Constructor Details

#initialize(name, type, size) ⇒ Column

Returns a new instance of Column.



6
7
8
9
# File 'lib/active_mdb/column.rb', line 6

def initialize(name, type, size)
  @name, @type = name, type
  @method_name, @size = methodize(name), size.to_i
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



4
5
6
# File 'lib/active_mdb/column.rb', line 4

def method_name
  @method_name
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/active_mdb/column.rb', line 4

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



4
5
6
# File 'lib/active_mdb/column.rb', line 4

def size
  @size
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/active_mdb/column.rb', line 4

def type
  @type
end

Class Method Details

.new_from_describe(describe_hash) ⇒ Object

returns a new Column from a hash with the keys: ‘Column Name’, ‘Type’, and ‘Size’



13
14
15
# File 'lib/active_mdb/column.rb', line 13

def self.new_from_describe(describe_hash)
  self.new(describe_hash["Column Name"], describe_hash["Type"], describe_hash["Size"])
end

.string_to_time(string) ⇒ Object



49
50
51
# File 'lib/active_mdb/column.rb', line 49

def self.string_to_time(string)
  string
end

.value_to_boolean(value) ⇒ Object

provided any argument, returns the mdb-tools version of truth (which is to say, 1 or 0)



55
56
57
58
59
60
61
# File 'lib/active_mdb/column.rb', line 55

def self.value_to_boolean(value)
  if value == true || value == false
    value
  else
    %w(true t 1).include?(value.to_s.downcase)
  end
end

Instance Method Details

#boolean?Boolean

Are you a Boolean?

Returns:

  • (Boolean)


64
65
66
# File 'lib/active_mdb/column.rb', line 64

def boolean?
  self.type == 'Boolean'
end

#klassObject

return Ruby class corresponding to data type.

Borrowed from ActiveRecord



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_mdb/column.rb', line 19

def klass
  case type
    when 'Text', 'Character'  then String
    when 'Long Integer'       then Fixnum
    when 'Double'             then Float
    when 'Currency', 'Float'  then Float
    when 'DateTime (Short)'   then Time
    when 'Boolean'            then Object
    when 'Decimal'            then BigDecimal
    when 'Binary'             then String
  end
end

#type_cast(value) ⇒ Object

Casts value (which is a String) to an appropriate instance. Totally borrowed from ActiveRecord



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_mdb/column.rb', line 34

def type_cast(value)
  return nil if value.nil?
  case type
    when 'Text', 'Character'  then value
    when 'Long Integer'       then value.to_i rescue value ? 1 : 0
    when 'Currency', 'Float'  then value.to_f
    when 'Double'             then value.to_f
    when 'DateTime (Short)'   then self.class.string_to_time(value)
    when 'Boolean'            then self.class.value_to_boolean(value)
    when 'Decimal'            then self.class.value_to_decimal(value)
    when 'Binary'             then self.class.binary_to_string(value)
    else value
  end
end