Sha256: 14c3864437b3aeeb872c826b80318503da6645b127e3c0e0bd1261659959c4b4

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 KB

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
          value = super
          value = value.b if ::String === value && value.encoding != Encoding::BINARY
          value
        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
          value = value.b unless value.encoding == Encoding::BINARY
          @value = value
        end

        def to_s
          @value
        end
        alias_method :to_str, :to_s

        def hex
          @value.unpack1("H*")
        end

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

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
activemodel-7.0.8.6 lib/active_model/type/binary.rb
activemodel-7.0.8.5 lib/active_model/type/binary.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activemodel-7.0.8.4/lib/active_model/type/binary.rb
activemodel-7.0.8.4 lib/active_model/type/binary.rb
activemodel-7.0.8.1 lib/active_model/type/binary.rb
activemodel-7.0.8 lib/active_model/type/binary.rb
activemodel-7.0.7.2 lib/active_model/type/binary.rb
activemodel-7.0.7.1 lib/active_model/type/binary.rb
activemodel-7.0.7 lib/active_model/type/binary.rb
activemodel-7.0.6 lib/active_model/type/binary.rb