aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/binary.rb
blob: 3bf29b5026819536028b4bf83bdd3690082baa7e (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
38
39
40
module ActiveRecord
  module Type
    class Binary < Value # :nodoc:
      def type
        :binary
      end

      def binary?
        true
      end

      def type_cast(value)
        if value.is_a?(Data)
          value.to_s
        else
          super
        end
      end

      def type_cast_for_database(value)
        return if value.nil?
        Data.new(super)
      end

      class Data
        def initialize(value)
          @value = value
        end

        def to_s
          @value
        end

        def hex
          @value.unpack('H*')[0]
        end
      end
    end
  end
end