Class: ActiveModel::Type::Binary

Inherits:
Value
  • Object
show all
Defined in:
lib/active_model/type/binary.rb

Overview

Active Model Binary Type

Attribute type for representation of binary data. This type is registered under the :binary key.

Non-string values are coerced to strings using their to_s method.

Defined Under Namespace

Classes: Data

Instance Attribute Summary

Attributes inherited from Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods inherited from Value

#==, #as_json, #assert_valid_value, #changed?, #deserialize, #force_equality?, #hash, #initialize, #map, #mutable?, #serializable?, #serialized?, #type_cast_for_schema, #value_constructed_by_mass_assignment?

Methods included from SerializeCastValue

included, #initialize, #itself_if_serialize_cast_value_compatible, serialize

Constructor Details

This class inherits a constructor from ActiveModel::Type::Value

Instance Method Details

#binary?Boolean

Returns:



16
17
18
# File 'lib/active_model/type/binary.rb', line 16

def binary?
  true
end

#cast(value) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/active_model/type/binary.rb', line 20

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

#changed_in_place?(raw_old_value, value) ⇒ Boolean

Returns:



35
36
37
38
# File 'lib/active_model/type/binary.rb', line 35

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

#serialize(value) ⇒ Object



30
31
32
33
# File 'lib/active_model/type/binary.rb', line 30

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

#typeObject



12
13
14
# File 'lib/active_model/type/binary.rb', line 12

def type
  :binary
end