Module: Vidibus::Inheritance::Mongoid

Extended by:
ActiveSupport::Concern
Defined in:
lib/vidibus/inheritance/mongoid.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ACQUIRED_ATTRIBUTES =
%w[_id _type uuid ancestor_uuid root_ancestor_uuid mutated_attributes mutated created_at updated_at version versions]

Instance Method Summary collapse

Instance Method Details

#acquired_attributesObject

Returns acquired attributes. Overwrite this method to define custom ones.



160
161
162
# File 'lib/vidibus/inheritance/mongoid.rb', line 160

def acquired_attributes
  ACQUIRED_ATTRIBUTES
end

#ancestorObject

Returns ancestor object by uuid.



89
90
91
92
93
# File 'lib/vidibus/inheritance/mongoid.rb', line 89

def ancestor
  @ancestor ||= begin
    self.class.where(:uuid => ancestor_uuid).first if ancestor_uuid
  end
end

#ancestor=(obj) ⇒ Object

Setter for ancestor.



83
84
85
86
# File 'lib/vidibus/inheritance/mongoid.rb', line 83

def ancestor=(obj)
  self.ancestor_uuid = obj ? obj.uuid : nil
  @ancestor = obj
end

#ancestorsObject

Returns a list of all ancestors ordered by inheritance distance.



96
97
98
99
100
101
102
103
104
# File 'lib/vidibus/inheritance/mongoid.rb', line 96

def ancestors
  @ancestors ||= [].tap do |bloodline|
    obj = self
    while true do
      break unless obj = obj.ancestor
      bloodline << obj
    end
  end
end

#clone!Object

Creates a sibling with identical inheritable attributes. First it inherits from self and then applies ancestry of self.



148
149
150
151
152
153
154
155
156
# File 'lib/vidibus/inheritance/mongoid.rb', line 148

def clone!
  clone = self.class.new
  clone.inherit_from!(self)
  clone.ancestor = ancestor
  clone.mutated_attributes = mutated_attributes
  clone.inherited_attributes = inherited_attributes
  clone.save!
  clone
end

#inherit!(options = {}) ⇒ Object

Performs inheritance and saves instance with force. Accepts :reset option to overwrite mutated attributes.

Examples:

inherit!(:reset => true)          => # Overwrites all mutated attributes
inherit!(:reset => :name)         => # Overwrites name only
inherit!(:reset => [:name, :age]) => # Overwrites name and age


122
123
124
125
# File 'lib/vidibus/inheritance/mongoid.rb', line 122

def inherit!(options = {})
  inherit_attributes(options)
  self.save!
end

#inherit_from!(obj, options = {}) ⇒ Object

Performs inheritance from given object and returns self. It sets the ancestor and then calls #inherit! with given options.



129
130
131
132
133
# File 'lib/vidibus/inheritance/mongoid.rb', line 129

def inherit_from!(obj, options = {})
  self.ancestor = obj
  self.inherit!(options)
  self
end

#inheritable_documents(options = {}) ⇒ Object

Returns embedded documents. See ClassMethods.inheritable_documents for options.



142
143
144
# File 'lib/vidibus/inheritance/mongoid.rb', line 142

def inheritable_documents(options = {})
  self.class.inheritable_documents(self, options)
end

#inheritorsObject

Returns inheritors of this ancestor.



136
137
138
# File 'lib/vidibus/inheritance/mongoid.rb', line 136

def inheritors
  self.class.where(:ancestor_uuid => uuid)
end

#root_ancestorObject

Returns root ancestor object by uuid.



107
108
109
110
111
# File 'lib/vidibus/inheritance/mongoid.rb', line 107

def root_ancestor
  @root_ancestor ||= begin
    self.class.where(:uuid => root_ancestor_uuid).first if root_ancestor_uuid
  end
end