Class: ActiveRecord::Type::Json

Inherits:
ActiveModel::Type::Value show all
Includes:
ActiveModel::Type::Helpers::Mutable
Defined in:
activerecord/lib/active_record/type/json.rb

Instance Attribute Summary

Attributes inherited from ActiveModel::Type::Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods included from ActiveModel::Type::Helpers::Mutable

#cast, #immutable_value

Methods inherited from ActiveModel::Type::Value

#==, #as_json, #assert_valid_value, #binary?, #cast, #changed?, #force_equality?, #hash, #immutable_value, #initialize, #map, #serializable?, #type_cast_for_schema, #value_constructed_by_mass_assignment?

Constructor Details

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

Instance Method Details

#accessorObject



25
26
27
# File 'activerecord/lib/active_record/type/json.rb', line 25

def accessor
  ActiveRecord::Store::StringKeyedHashAccessor
end

#changed_in_place?(raw_old_value, new_value) ⇒ Boolean

Returns:



21
22
23
# File 'activerecord/lib/active_record/type/json.rb', line 21

def changed_in_place?(raw_old_value, new_value)
  deserialize(raw_old_value) != new_value
end

#deserialize(value) ⇒ Object



12
13
14
15
# File 'activerecord/lib/active_record/type/json.rb', line 12

def deserialize(value)
  return value unless value.is_a?(::String)
  ActiveSupport::JSON.decode(value) rescue nil
end

#serialize(value) ⇒ Object



17
18
19
# File 'activerecord/lib/active_record/type/json.rb', line 17

def serialize(value)
  ActiveSupport::JSON.encode(value) unless value.nil?
end

#typeObject



8
9
10
# File 'activerecord/lib/active_record/type/json.rb', line 8

def type
  :json
end