aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/types/unknown.rb
blob: f832c7b3045e21c6a2e278ba882898b131471068 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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