Method: Mara::Model::Attributes#fetch

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

#fetch(key, default = nil, pre_formatted: false) ⇒ Any?

Get an attribute value but it that value is nil, return the default passed as the second argument.

Examples:

Get a default value.

attrs.set('foo', nil)
attrs.fetch('foo', 'bar')
# => 'bar'

Parameters:

  • key (#to_s)

    The key for the attribute you’re getting.

  • default (Any, nil) (defaults to: nil)

    The default value to return if the value for the key is nil.

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

    If the key is already normalized.

Returns:

  • (Any, nil)

Since:

  • 1.0.0



107
108
109
110
111
112
# File 'lib/mara/model/attributes.rb', line 107

def fetch(key, default = nil, pre_formatted: false)
  value = get(key, pre_formatted: pre_formatted)
  return default if value.nil?

  value
end