123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Persistable::Minable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: lib/mongoid/persistable/minable.rb

Overview

Defines behavior for setting a field (or fields) to the smaller of either it’s current value, or a given value.

Instance Method Summary

Instance Method Details

#clamp_upper_bound(fields)

Alias for #set_min.

[ GitHub ]

  
# File 'lib/mongoid/persistable/minable.rb', line 34

alias :clamp_upper_bound :set_min

#set_min(fields) ⇒ Document Also known as: #clamp_upper_bound

::Set the given field or fields to the smaller of either it’s current value, or a given value.

Examples:

::Set a field to be no more than 100.

document.min(field: 100)

Parameters:

  • fields (Hash<Symbol | String, Comparable>)

    The fields to set, with corresponding maximum values.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/persistable/minable.rb', line 22

def set_min(fields)
  prepare_atomic_operation do |ops|
    process_atomic_operations(fields) do |field, value|
      current_value = attributes[field]
      if value < current_value
        process_attribute field, value
        ops[atomic_attribute_name(field)] = value
      end
    end
    { "$min" => ops } unless ops.empty?
  end
end