Module: Mongoid::Stateful

Included in:
Composable
Defined in:
lib/mongoid/stateful.rb

Overview

This module contains the behaviour for getting the various states a document can transition through.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#destroyed=(value) ⇒ Object (writeonly)

Sets the attribute destroyed

Parameters:

  • value

    the value to set the attribute destroyed to.


8
9
10
# File 'lib/mongoid/stateful.rb', line 8

def destroyed=(value)
  @destroyed = value
end

#flagged_for_destroy=(value) ⇒ Object (writeonly)

Sets the attribute flagged_for_destroy

Parameters:

  • value

    the value to set the attribute flagged_for_destroy to.


8
9
10
# File 'lib/mongoid/stateful.rb', line 8

def flagged_for_destroy=(value)
  @flagged_for_destroy = value
end

#new_record=(value) ⇒ Object (writeonly)

Sets the attribute new_record

Parameters:

  • value

    the value to set the attribute new_record to.


8
9
10
# File 'lib/mongoid/stateful.rb', line 8

def new_record=(value)
  @new_record = value
end

Instance Method Details

#destroyed?true, false

Returns true if the Document has been succesfully destroyed, and false if it hasn't. This is determined by the variable @destroyed and NOT by checking the database.

Examples:

Is the document destroyed?

person.destroyed?

Returns:

  • (true, false)

    True if destroyed, false if not.


55
56
57
# File 'lib/mongoid/stateful.rb', line 55

def destroyed?
  @destroyed ||= false
end

#flagged_for_destroy?true, false Also known as: marked_for_destruction?

Returns whether or not the document has been flagged for deletion, but not destroyed yet. Used for atomic pulls of child documents.

Examples:

Is the document flagged?

document.flagged_for_destroy?

Returns:

  • (true, false)

    If the document is flagged.

Since:

  • 2.3.2


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

def flagged_for_destroy?
  @flagged_for_destroy ||= false
end

#new_record?true, false

Returns true if the Document has not been persisted to the database, false if it has. This is determined by the variable @new_record and NOT if the object has an id.

Examples:

Is the document new?

person.new_record?

Returns:

  • (true, false)

    True if new, false if not.


18
19
20
# File 'lib/mongoid/stateful.rb', line 18

def new_record?
  @new_record ||= false
end

#persisted?true, false

Checks if the document has been saved to the database. Returns false if the document has been destroyed.

Examples:

Is the document persisted?

person.persisted?

Returns:

  • (true, false)

    True if persisted, false if not.


29
30
31
# File 'lib/mongoid/stateful.rb', line 29

def persisted?
  !new_record? && !destroyed?
end

#pushable?true, false

Determine if the document can be pushed.

Examples:

Is this pushable?

person.pushable?

Returns:

  • (true, false)

    Is the document new and embedded?


65
66
67
68
69
70
# File 'lib/mongoid/stateful.rb', line 65

def pushable?
  new_record? &&
    embedded_many? &&
    _parent.persisted? &&
    !_parent.delayed_atomic_sets[atomic_path]
end

#readonly?true, false

Is the document readonly?

Examples:

Is the document readonly?

document.readonly?

Returns:

  • (true, false)

    If the document is readonly.

Since:

  • 4.0.0


80
81
82
# File 'lib/mongoid/stateful.rb', line 80

def readonly?
  __selected_fields != nil
end

#settable?true, false

Determine if the document can be set.

Examples:

Is this settable?

person.settable?

Returns:

  • (true, false)

    Is this document a new embeds one?

Since:

  • 2.1.0


92
93
94
# File 'lib/mongoid/stateful.rb', line 92

def settable?
  new_record? && embedded_one? && _parent.persisted?
end

#updateable?true, false

Is the document updateable?

Examples:

Is the document updateable?

person.updateable?

Returns:

  • (true, false)

    If the document is changed and persisted.

Since:

  • 2.1.0


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

def updateable?
  persisted? && changed?
end