Sha256: e8256dfad1fc0744264893c62f8710de32adda4fdcc5dfb682f57d0b0c4d02e5

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 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 self.serialized?
      @serialized_attrs ||= Hash.new { |h, k| h[k] = Hash.new }
    end

    def convert(attr, val)
      column_type = column_type(attr)
      return val if self.class.serialized?[model][attr] ||
                    !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

6 entries across 6 versions & 1 rubygems

Version Path
hyper-mesh-1.0.0.lap27 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-1.0.0.lap26 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-1.0.0.lap25 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-1.0.0.lap24 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-1.0.0.lap23 lib/reactive_record/active_record/reactive_record/column_types.rb
hyper-mesh-1.0.0.lap22 lib/reactive_record/active_record/reactive_record/column_types.rb