Sha256: ec75efede741221f8c28ce06668c214b60f2a09410f3ad72ab86db6300bf83dc

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 KB

Contents

module ReactiveRecord
  # ActiveRecord column access and conversion helpers
  class Base
    def columns_hash
      model.columns_hash
    end

    def column_type(attr)
      column_hash = columns_hash[attr]
      return nil unless column_hash
      column_hash[:sql_type_metadata] && column_hash[:sql_type_metadata][:type]
    end

    def convert_datetime(val)
      if val.is_a?(Numeric)
        Time.at(val)
      elsif val.is_a?(Time)
        val
      else
        Time.parse(val)
      end
    end

    alias convert_time convert_datetime
    alias convert_timestamp convert_datetime

    def convert_date(val)
      if val.is_a?(Time)
        Date.parse(val.strftime('%d/%m/%Y'))
      elsif val.is_a?(Date)
        val
      else
        Date.parse(val)
      end
    end

    def convert_boolean(val)
      !['false', false, nil, 0].include?(val)
    end

    def convert_integer(val)
      Integer(`parseInt(#{val})`)
    end

    alias convert_bigint convert_integer

    def convert_float(val)
      Float(val)
    end

    alias convert_decimal convert_float

    def convert_text(val)
      val.to_s
    end

    alias convert_string convert_text

    def convert(attr, val)
      column_type = column_type(attr)
      return val if !column_type || val.loading? || (!val && column_type != :boolean)
      conversion_method = "convert_#{column_type}"
      return send(conversion_method, val) if respond_to? conversion_method
      val
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hyper-mesh-0.6.0 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-0.5.4 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-0.5.3 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-0.5.2 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-0.5.1 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-0.5.0 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-0.4.0 lib/reactive_record/active_record/reactive_record/column_types.rb