Module: Mongoid::Extensions::Object

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary (collapse)

Instance Method Details

- (Object) __evolve_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

- (Object) __mongoize_time__

Mongoize a plain object into a time.

Examples:

Mongoize the object.

object.__mongoize_time__

Returns:

Since:

  • 3.0.0



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

def __mongoize_time__
  self
end

- (Object?) do_or_do_not(name, *args)

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



42
43
44
# File 'lib/mongoid/extensions/object.rb', line 42

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

- (Object?) ivar(name)

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



56
57
58
59
60
61
62
# File 'lib/mongoid/extensions/object.rb', line 56

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

- (Object) mongoize

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



73
74
75
# File 'lib/mongoid/extensions/object.rb', line 73

def mongoize
  self
end

- (true, false) remove_ivar(name)

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



87
88
89
90
91
92
93
# File 'lib/mongoid/extensions/object.rb', line 87

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

- (false) resizable?

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



104
105
106
# File 'lib/mongoid/extensions/object.rb', line 104

def resizable?
  false
end

- (Object) substitutable

Get the substitutable version of an object.

Examples:

Get the substitutable.

object.substitutable

Returns:

Since:

  • 2.0.0



116
117
118
# File 'lib/mongoid/extensions/object.rb', line 116

def substitutable
  self
end

- (Object?) you_must(name, *args)

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



132
133
134
# File 'lib/mongoid/extensions/object.rb', line 132

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