Sha256: e17ea046495acdb20ffdd8bec481809d8b62a1dc434220571e5ec42c731a4ffb

Contents?: true

Size: 914 Bytes

Versions: 4

Compression:

Stored size: 914 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, :hash, :array, :decimal
          value
        when :integer, :float, :datetime, :date, :boolean
          TYPES[type].new.type_cast_from_user(value)
        else value
          # Nothing.
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hstore_accessor-0.9.3 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor-0.9.2 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor-0.9.1 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor-0.9.0 lib/hstore_accessor/active_record_4.2/type_helpers.rb