Method: Mara::Model::Attributes#set

Defined in:
lib/mara/model/attributes.rb

#set(key, value, pre_formatted: false) ⇒ void

Note:

If the value is nil, the key will be deleted.

This method returns an undefined value.

Set a key, value pair on the attributes.

Parameters:

  • key (#to_s)

    The key for the attribute you’re trying to set.

    This key will be normalized before being stored as an attribute.

  • value (Any, nil)

    The value to set for the key.

  • pre_formatted (true, false) (defaults to: false)

    If the key is already normalized.

Raises:

  • (AttributeError)

    The normalized value of the key can’t be blank.

Since:

  • 1.0.0



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mara/model/attributes.rb', line 60

def set(key, value, pre_formatted: false)
  nkey = normalize_key(key, pre_formatted)

  raise AttributeError, "Can't set an attribute without a key" if nkey.blank?

  if value.nil?
    @storage.delete(nkey)
  else
    @storage[nkey] = value
  end
end