Sha256: 815f936f3015da9ee419cce90f8bde7701ff02fa2979bd74db36320cf387aa80
Contents?: true
Size: 1.11 KB
Versions: 3
Compression:
Stored size: 1.11 KB
Contents
# @see https://www.rubydoc.info/docs/rails/ActiveModel/Type/Value class Anchormodel::ActiveModelTypeValue < ActiveModel::Type::Value def initialize(attribute) super() @attribute = attribute end def cast(value) serialize value end # Implementing this instead of cast to force key validation in any case def serialize(value) return case value when Symbol, String unless @attribute.anchor_class.valid_keys.include?(value.to_sym) fail("Attempt to set #{@attribute.attribute_name} to unsupported key #{value.inspect}.") end value.to_s when @attribute.anchor_class value.key.to_s when nil nil else fail "Attempt to set #{@attribute.attribute_name} to unsupported type #{value.class}" end end def deserialize(value) return value if value.is_a?(@attribute.anchor_class) return @attribute.anchor_class.find(value) end def changed?(old_value, new_value, _new_value_before_type_cast) return deserialize(old_value) != deserialize(new_value) end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
anchormodel-0.1.0 | lib/anchormodel/active_model_type_value.rb |
anchormodel-0.0.2 | lib/anchormodel/active_model_type_value.rb |
anchormodel-0.0.1 | lib/anchormodel/active_model_type_value.rb |