Sha256: 2217ea2f4c6686382e5fd85b704df18ab26a5d3ee070aafe92a90fbaad3405bb

Contents?: true

Size: 899 Bytes

Versions: 8

Compression:

Stored size: 899 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.type_cast_from_user(value)
        else value
          # Nothing.
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
hstore_accessor_moi_solutions-1.0.4 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor-1.1.1 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor-1.1.0 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor_rails5-1.0.4 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor-1.0.3 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor-1.0.2 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor-1.0.1 lib/hstore_accessor/active_record_4.2/type_helpers.rb
hstore_accessor-1.0.0 lib/hstore_accessor/active_record_4.2/type_helpers.rb