Sha256: 001e0aa25a087573b1c964148ec55c689a43e6a777aa2767fc349a01503a239d

Contents?: true

Size: 873 Bytes

Versions: 40

Compression:

Stored size: 873 Bytes

Contents

# frozen_string_literal: true

module ActiveModel
  module Type
    class Binary < Value # :nodoc:
      def type
        :binary
      end

      def binary?
        true
      end

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

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

      def changed_in_place?(raw_old_value, value)
        old_value = deserialize(raw_old_value)
        old_value != value
      end

      class Data # :nodoc:
        def initialize(value)
          @value = value.to_s
        end

        def to_s
          @value
        end
        alias_method :to_str, :to_s

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

        def ==(other)
          other == to_s || super
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 6 rubygems

Version Path
activemodel-5.2.8.1 lib/active_model/type/binary.rb
activemodel-5.2.8 lib/active_model/type/binary.rb
activemodel-5.2.7.1 lib/active_model/type/binary.rb
activemodel-5.2.7 lib/active_model/type/binary.rb
activemodel-5.2.6.3 lib/active_model/type/binary.rb
activemodel-5.2.6.2 lib/active_model/type/binary.rb
activemodel-5.2.6.1 lib/active_model/type/binary.rb
activemodel-5.2.6 lib/active_model/type/binary.rb
activemodel-5.2.4.6 lib/active_model/type/binary.rb
activemodel-5.2.5 lib/active_model/type/binary.rb
activemodel-5.2.4.5 lib/active_model/type/binary.rb
activemodel-5.2.4.4 lib/active_model/type/binary.rb
activemodel-5.2.4.3 lib/active_model/type/binary.rb
activemodel-5.2.4.2 lib/active_model/type/binary.rb
activemodel-5.2.4.1 lib/active_model/type/binary.rb
activemodel-5.2.4 lib/active_model/type/binary.rb
activemodel-5.2.4.rc1 lib/active_model/type/binary.rb
spiral_form-0.1.1 vendor/bundle/gems/activemodel-5.2.3/lib/active_model/type/binary.rb
spiral_form-0.1.0 vendor/bundle/gems/activemodel-5.2.3/lib/active_model/type/binary.rb
ric-0.13.0 vendor/bundle/ruby/2.5.0/gems/activemodel-5.2.3/lib/active_model/type/binary.rb