Module: Mongoid::Extensions::Object

Defined in:
lib/mongoid/extensions/object.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#__evolve_object_id__Object Also known as: __mongoize_object_id__

Evolve a plain object into an object id.

Examples:

Evolve the object.

object.__evolve_object_id__

Returns:

Since:

  • 3.0.0



14
15
16
# File 'lib/mongoid/extensions/object.rb', line 14

def __evolve_object_id__
  self
end

#__find_args__Object

Convert the object to args for a find query.

Examples:

Convert the object to args.

object.__find_args__

Returns:

Since:

  • 3.0.0



27
28
29
# File 'lib/mongoid/extensions/object.rb', line 27

def __find_args__
  self
end

#__mongoize_time__Object

Mongoize a plain object into a time.

Examples:

Mongoize the object.

object.__mongoize_time__

Returns:

Since:

  • 3.0.0



39
40
41
# File 'lib/mongoid/extensions/object.rb', line 39

def __mongoize_time__
  self
end

#__sortable__Object

Get the value of the object as a mongo friendy sort value.

Examples:

Get the object as sort criteria.

object.__sortable__

Returns:

Since:

  • 3.0.0



51
52
53
# File 'lib/mongoid/extensions/object.rb', line 51

def __sortable__
  self
end

#do_or_do_not(name, *args) ⇒ Object?

Do or do not, there is no try. – Yoda.

Examples:

Do or do not.

object.do_or_do_not(:use, "The Force")

Parameters:

Returns:

  • (Object, nil)

    The result of the method call or nil if the method does not exist.

Since:

  • 2.0.0.rc.1



67
68
69
# File 'lib/mongoid/extensions/object.rb', line 67

def do_or_do_not(name, *args)
  send(name, *args) if name && respond_to?(name)
end

#ivar(name) ⇒ Object?

Get the value for an instance variable or nil if it doesn’t exist.

Examples:

Get the value for an instance var.

document.ivar("person")

Parameters:

  • name (String)

    The name of the variable.

Returns:

  • (Object, nil)

    The value or nil.

Since:

  • 2.0.0.rc.1



81
82
83
84
85
86
87
# File 'lib/mongoid/extensions/object.rb', line 81

def ivar(name)
  if instance_variable_defined?("@#{name}")
    return instance_variable_get("@#{name}")
  else
    false
  end
end

#mongoizeObject

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

Examples:

Mongoize the object.

object.mongoize

Returns:

Since:

  • 3.0.0



98
99
100
# File 'lib/mongoid/extensions/object.rb', line 98

def mongoize
  self
end

#multi_arged?false

Is the object multi args.

Examples:

Is the object multi args?

object.multi_arged?

Returns:

  • (false)

    false.

Since:

  • 3.0.0



110
111
112
# File 'lib/mongoid/extensions/object.rb', line 110

def multi_arged?
  false
end

#numeric?false

Is the object a number?

Examples:

Is the object a number?.

object.numeric?

Returns:

  • (false)

    Always false.

Since:

  • 3.0.0



122
123
124
# File 'lib/mongoid/extensions/object.rb', line 122

def numeric?
  false
end

#remove_ivar(name) ⇒ true, false

Remove the instance variable for the provided name.

Examples:

Remove the instance variable

document.remove_ivar("person")

Parameters:

  • name (String)

    The name of the variable.

Returns:

  • (true, false)

    If the variable was defined.

Since:

  • 2.1.0



136
137
138
139
140
141
142
# File 'lib/mongoid/extensions/object.rb', line 136

def remove_ivar(name)
  if instance_variable_defined?("@#{name}")
    return remove_instance_variable("@#{name}")
  else
    false
  end
end

#resizable?false

Is the object’s size changable? Only returns true for arrays and hashes currently.

Examples:

Is the object resizable?

object.resizable?

Returns:

  • (false)

    false.

Since:

  • 3.0.0



153
154
155
# File 'lib/mongoid/extensions/object.rb', line 153

def resizable?
  false
end

#substitutableObject

Get the substitutable version of an object.

Examples:

Get the substitutable.

object.substitutable

Returns:

Since:

  • 2.0.0



165
166
167
# File 'lib/mongoid/extensions/object.rb', line 165

def substitutable
  self
end

#you_must(name, *args) ⇒ Object?

You must unlearn what you have learned. – Yoda

Examples:

You must perform this execution.

object.you_must(:use, "The Force")

Parameters:

Returns:

  • (Object, nil)

    The result of the method call or nil if the method does not exist. Nil if the object is frozen.

Since:

  • 2.2.1



181
182
183
# File 'lib/mongoid/extensions/object.rb', line 181

def you_must(name, *args)
  frozen? ? nil : do_or_do_not(name, *args)
end