Sha256: 89ce12eb32d7b08cb101419ac0204f3c38d036ba6c95affcedd161eaeff47597

Contents?: true

Size: 884 Bytes

Versions: 4

Compression:

Stored size: 884 Bytes

Contents

module HstoreAccessor
  module TypeHelpers
    TYPES = {
      boolean: ActiveRecord::Type::Boolean,
      date: ActiveRecord::Type::Date,
      datetime: ActiveRecord::Type::DateTime,
      decimal: ActiveRecord::Type::Decimal,
      float: ActiveRecord::Type::Float,
      integer: ActiveRecord::Type::Integer,
      string: ActiveRecord::Type::String
    }

    TYPES.default = ActiveRecord::Type::Value

    class << self
      def column_type_for(attribute, data_type)
        ActiveRecord::ConnectionAdapters::Column.new(attribute.to_s, nil, TYPES[data_type].new)
      end

      def cast(type, value)
        return nil if value.nil?

        case type
        when :string, :decimal
          value
        when :integer, :float, :datetime, :date, :boolean
          TYPES[type].new.cast(value)
        else value
          # Nothing.
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 3 rubygems

Version Path
hstore_accessor_moi_solutions-1.0.4 lib/hstore_accessor/active_record_5.0/type_helpers.rb
hstore_accessor-1.1.1 lib/hstore_accessor/active_record_5.0/type_helpers.rb
hstore_accessor-1.1.0 lib/hstore_accessor/active_record_5.0/type_helpers.rb
hstore_accessor_rails5-1.0.4 lib/hstore_accessor/active_record_5.0/type_helpers.rb