Module: Mongoid::Extensions::Array

Defined in:
build/mongoid-8.1/lib/mongoid/extensions/array.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#__evolve_object_id__Array<BSON::ObjectId>

Evolve the array into an array of object ids.

Examples:

Evolve the array to object ids.

[ id ].__evolve_object_id__

Returns:



13
14
15
16
# File 'build/mongoid-8.1/lib/mongoid/extensions/array.rb', line 13

def __evolve_object_id__
  map!(&:__evolve_object_id__)
  self
end

#__find_args__Array

Get the array of args as arguments for a find query.

Examples:

Get the array as find args.

[ 1, 2, 3 ].__find_args__

Returns:

  • (Array)

    The array of args.



24
25
26
# File 'build/mongoid-8.1/lib/mongoid/extensions/array.rb', line 24

def __find_args__
  flat_map{ |a| a.__find_args__ }.uniq{ |a| a.to_s }
end

#__mongoize_object_id__Array<BSON::ObjectId>

Mongoize the array into an array of object ids.

Examples:

Evolve the array to object ids.

[ id ].__mongoize_object_id__

Returns:



34
35
36
37
# File 'build/mongoid-8.1/lib/mongoid/extensions/array.rb', line 34

def __mongoize_object_id__
  map!(&:__mongoize_object_id__).compact!
  self
end

#__mongoize_time__Time | ActiveSupport::TimeWithZone

Note:

Returns a local time in the default time zone.

Converts the array for storing as a time.

Examples:

Convert the array to a time.

[ 2010, 1, 1 ].__mongoize_time__
# => 2010-01-01 00:00:00 -0500

Returns:

  • (Time | ActiveSupport::TimeWithZone)

    Local time in the configured default time zone corresponding to date/time components in this array.



50
51
52
# File 'build/mongoid-8.1/lib/mongoid/extensions/array.rb', line 50

def __mongoize_time__
  ::Time.configured.local(*self)
end

#blank_criteria?false

Deprecated.

Checks whether conditions given in this array are known to be unsatisfiable, i.e., querying with this array will always return no documents.

This method used to assume that the array is the list of criteria to be used with an $and operator. This assumption is no longer made; therefore, since the interpretation of conditions in the array differs between $and, $or and $nor operators, this method now always returns false.

This method is deprecated. Mongoid now uses _mongoid_unsatisfiable_criteria? internally; this method is retained for backwards compatibility only. It always returns false.

Returns:

  • (false)

    Always false.



70
71
72
# File 'build/mongoid-8.1/lib/mongoid/extensions/array.rb', line 70

def blank_criteria?
  false
end

#delete_one(object) ⇒ Object

Delete the first object in the array that is equal to the supplied object and return it. This is much faster than performing a standard delete for large arrays since it does not perform multiple deletes.

Examples:

Delete the first object.

[ "1", "2", "1" ].delete_one("1")

Parameters:

  • object (Object)

    The object to delete.

Returns:

  • (Object)

    The deleted object.



105
106
107
108
# File 'build/mongoid-8.1/lib/mongoid/extensions/array.rb', line 105

def delete_one(object)
  position = index(object)
  position ? delete_at(position) : nil
end

#mongoizeArray | nil

Turn the object from the ruby type we deal with to a Mongo friendly type.

Examples:

Mongoize the object.

object.mongoize

Returns:

  • (Array | nil)

    The object or nil.



91
92
93
# File 'build/mongoid-8.1/lib/mongoid/extensions/array.rb', line 91

def mongoize
  ::Array.mongoize(self)
end

#multi_arged?true | false

Is the array a set of multiple arguments in a method?

Examples:

Is this multi args?

[ 1, 2, 3 ].multi_arged?

Returns:

  • (true | false)

    If the array is multi args.



80
81
82
# File 'build/mongoid-8.1/lib/mongoid/extensions/array.rb', line 80

def multi_arged?
  !first.is_a?(Hash) && first.resizable? || size > 1
end

#resizable?true

Returns whether the object’s size can be changed.

Examples:

Is the object resizable?

object.resizable?

Returns:

  • (true)

    true.



116
117
118
# File 'build/mongoid-8.1/lib/mongoid/extensions/array.rb', line 116

def resizable?
  true
end