diff options
Diffstat (limited to 'activerecord/lib/active_record/type/binary.rb')
-rw-r--r-- | activerecord/lib/active_record/type/binary.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/type/binary.rb b/activerecord/lib/active_record/type/binary.rb new file mode 100644 index 0000000000..3bf29b5026 --- /dev/null +++ b/activerecord/lib/active_record/type/binary.rb @@ -0,0 +1,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 |