Sha256: a43b18b8ef53beb4c93f18d5722b4ce9b372d28ed021760d916c71cc15d052b3

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# @see https://www.rubydoc.info/docs/rails/ActiveModel/Type/Value
class Anchormodel::ActiveModelTypeValueSingle < ActiveModel::Type::Value
  def initialize(attribute)
    super()
    @attribute = attribute
  end

  def type
    :anchormodel
  end

  # This converts an Anchormodel instance to string for DB
  def cast(value)
    value = value.presence
    return value if value.is_a?(@attribute.anchormodel_class)
    return @attribute.anchormodel_class.find(value)
  end

  # This converts DB or input to an Anchormodel instance
  def serialize(value)
    value = value.presence
    return case value
           when Symbol, String
             unless @attribute.anchormodel_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.anchormodel_class
             value.key.to_s
           when nil
             nil
           else
             fail "Attempt to set #{@attribute.attribute_name} to unsupported type #{value.class}"
           end
  end

  def serializable?(value)
    return case value
           when Symbol, String
             @attribute.anchormodel_class.valid_keys.exclude?(value.to_sym)
           when nil, @attribute.anchormodel_class
             true
           else
             false
           end
  end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
anchormodel-0.1.5 lib/anchormodel/active_model_type_value_single.rb
anchormodel-0.1.4 lib/anchormodel/active_model_type_value_single.rb