123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Extensions::Array

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/extensions/array.rb

Overview

Adds type-casting behavior to Array class.

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#multi_arged?true | false (readonly)

Deprecated.

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.

[ GitHub ]

  
# File 'lib/mongoid/extensions/array.rb', line 65

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

#resizable?true (readonly)

Returns whether the object’s size can be changed.

Examples:

Is the object resizable?

object.resizable?

Returns:

  • (true)

    true.

[ GitHub ]

  
# File 'lib/mongoid/extensions/array.rb', line 102

def resizable?
  true
end

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:

[ GitHub ]

  
# File 'lib/mongoid/extensions/array.rb', line 15

def __evolve_object_id__
  map!(&:__evolve_object_id__)
  self
end

#__find_args__Array

Deprecated.

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.

[ GitHub ]

  
# File 'lib/mongoid/extensions/array.rb', line 27

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:

[ GitHub ]

  
# File 'lib/mongoid/extensions/array.rb', line 38

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:

[ GitHub ]

  
# File 'lib/mongoid/extensions/array.rb', line 54

def __mongoize_time__
  ::Time.zone.local(*self)
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.

[ GitHub ]

  
# File 'lib/mongoid/extensions/array.rb', line 91

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.

[ GitHub ]

  
# File 'lib/mongoid/extensions/array.rb', line 77

def mongoize
  ::Array.mongoize(self)
end