aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/lib/active_record/types/unknown.rb
blob: f832c7b3045e21c6a2e278ba882898b131471068 (plain) (tree)




































                                                                                            
module ActiveRecord
  module Type
    # Useful for handling attributes not mapped to types. Performs some boolean typecasting,
    # but otherwise leaves the value untouched.
    class Unknown

      def cast(value)
        value
      end

      def precast(value)
        value
      end

      # Attempts typecasting to handle numeric, false and blank values.
      def boolean(value)
        empty = (numeric?(value) && value.to_i.zero?) || false?(value) || value.blank?
        !empty
      end

      def appendable?
        false
      end

      protected

      def false?(value)
        ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.include?(value)
      end

      def numeric?(value)
        Numeric === value || value !~ /[^0-9]/
      end

    end
  end
end