Class: ActiveRecord::ConnectionAdapters::AmalgaliteColumn

Inherits:
Column
  • Object
show all
Defined in:
lib/active_record/connection_adapters/amalgalite_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.binary_to_string(value) ⇒ Object

since the type is blog, the amalgalite drive extracts it as a blob and we need to convert back into a string and do the substitution



56
57
58
59
60
61
62
63
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 56

def self.binary_to_string(value)
  value.to_s.gsub(/%00|%25/n) do |b|
    case b
      when "%00" then "\0"
      when "%25" then "%"
    end
  end
end

.datetime_to_time(dt) ⇒ Object

AR asks to convert a datetime column to a time and then passes in a string… WTF ?



67
68
69
70
71
72
73
74
75
76
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 67

def self.datetime_to_time( dt )
  case dt
  when String
    return nil if dt.empty?
  when DateTime
    return dt.to_time
  when Time
    return dt.to_time
  end
end

.from_amalgalite(am_col) ⇒ Object



35
36
37
38
39
40
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 35

def self.from_amalgalite( am_col )
  new( am_col.name,
      am_col.default_value,
      am_col.declared_data_type,
      am_col.nullable? )
end

.string_to_binary(value) ⇒ Object

unfortunately, not able to use the Blob interface as that requires knowing what column the blob is going to be stored in. Use the approach in the sqlite3 driver.



45
46
47
48
49
50
51
52
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 45

def self.string_to_binary( value )
  value.gsub(/\0|\%/n) do |b|
    case b
      when "\0" then "%00"
      when "%"  then "%25"
    end
  end
end

Instance Method Details

#type_cast_code(var_name) ⇒ Object

active record assumes that type casting is from a string to a value, and it might not be. It might be something appropriate for the field in question, like say a DateTime for a :datetime field?.



81
82
83
84
85
86
87
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 81

def type_cast_code( var_name )
  case type 
  when :datetime   then "#{self.class.name}.datetime_to_time(#{var_name})"
  else
    super
  end
end