Module: ChronicTree::Operation

Included in:
ChronicTree
Defined in:
lib/chronic_tree/operation.rb

Defined Under Namespace

Classes: ChangeParent, ReplaceBy

Instance Method Summary collapse

Instance Method Details

#add_as_rootObject



97
98
99
100
101
102
103
104
105
# File 'lib/chronic_tree/operation.rb', line 97

def add_as_root
  as_tree(Time.now, current_scope_name || 'default')

  raise_if_tree_is_not_empty

  ChronicTree::Command::AddRootElement.new(self).do

  self
end

#add_child(object) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chronic_tree/operation.rb', line 107

def add_child(object)
  as_tree(Time.now, current_scope_name || 'default') && object.as_tree(current_time_at, current_scope_name)

  raise_if_object_unmatched(object)
  raise_if_object_is_in_the_tree(object)
  raise_if_self_is_not_in_the_tree

  ::ActiveRecord::Base.transaction do
    ChronicTree::Command::AddChildElement.new(self, object).do
    ChronicTree::Command::AddChildElementToOrigAncestors.new(self, object).do unless self == root
  end

  self
end

#change_parent(object) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/chronic_tree/operation.rb', line 145

def change_parent(object)
  as_tree(Time.now, current_scope_name || 'default') && object.as_tree(current_time_at, current_scope_name)
  return self if self != root && parent == object

  raise_if_object_unmatched(object)
  raise_if_object_is_not_in_the_tree(object)
  raise_if_object_equals_to_self(object)
  raise_if_object_is_a_child_of_self(object)
  raise_if_self_is_not_in_the_tree

  ChangeParent.new(self, object).act
end

#remove_descendantsObject



135
136
137
138
139
140
141
142
143
# File 'lib/chronic_tree/operation.rb', line 135

def remove_descendants
  as_tree(Time.now, current_scope_name || 'default')

  raise_if_self_is_not_in_the_tree

  ::ActiveRecord::Base.transaction { ChronicTree::Command::RemoveDescendantElements.new(self).do }

  self
end

#remove_selfObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chronic_tree/operation.rb', line 122

def remove_self
  as_tree(Time.now, current_scope_name || 'default')

  raise_if_self_is_not_in_the_tree

  ::ActiveRecord::Base.transaction do
    ChronicTree::Command::RemoveSelfElement.new(self).do
    ChronicTree::Command::RemoveDescendantElements.new(self).do
  end

  self
end

#replace_by(object) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/chronic_tree/operation.rb', line 158

def replace_by(object)
  as_tree(Time.now, current_scope_name || 'default') && object.as_tree(current_time_at, current_scope_name)

  raise_if_object_unmatched(object)
  raise_if_object_is_in_the_tree(object)
  raise_if_self_is_not_in_the_tree

  ReplaceBy.new(self, object).act
end