Class: ActiveRecord::ConnectionAdapters::SimpleDBColumn

Inherits:
Column
  • Object
show all
Includes:
Quoting
Defined in:
lib/active_record/connection_adapters/simpledb_adapter/column.rb

Constant Summary collapse

DEFAULT_NUMBER_LIMIT =
4
DEFAULT_FLOAT_PRECISION =
4

Instance Method Summary collapse

Constructor Details

#initialize(name, type, limit = nil, pricision = nil, to = nil, default = nil) ⇒ SimpleDBColumn

Returns a new instance of SimpleDBColumn.



10
11
12
13
14
15
16
# File 'lib/active_record/connection_adapters/simpledb_adapter/column.rb', line 10

def initialize(name, type, limit = nil, pricision = nil, to = nil, default = nil)
  super name, nil, type, true
  @limit = limit if limit.present?
  @precision = precision if precision.present?
  @default = default
  @to = to
end

Instance Method Details

#convert(value) ⇒ Object

convert to sdb



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_record/connection_adapters/simpledb_adapter/column.rb', line 19

def convert value
  return nil if value.nil?
  case sql_type
    when :float then
      sprintf("%.#{number_precision}f", number_shift + value.to_f)
    when :integer then
      (number_shift + value.to_i).to_s
    when :json then
      ActiveSupport::JSON.encode(value)
    else
      if value.acts_like?(:date) || value.acts_like?(:time)
        quoted_date(value)
      else
        value.to_s
      end
  end
end

#db_column_nameObject



53
54
55
# File 'lib/active_record/connection_adapters/simpledb_adapter/column.rb', line 53

def db_column_name
  @to || name
end

#unconvert(value) ⇒ Object

unconvert to sdb



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_record/connection_adapters/simpledb_adapter/column.rb', line 38

def unconvert value
  return nil if value.nil?
  case sql_type
    when :integer then
      value.to_i - number_shift
    when :float then
      precision_part = 10 ** number_precision
      ((value.to_f - number_shift) * precision_part).round / precision_part.to_f
    when :json then
      ActiveSupport::JSON.decode(value)
    else
      value
  end
end