Module: Dag::Edges::EdgeInstanceMethods

Defined in:
lib/dag/edges.rb

Overview

Instance methods included into the link model for polymorphic and non-polymorphic DAGs

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#do_not_perpetuateObject

Returns the value of attribute do_not_perpetuate.



123
124
125
# File 'lib/dag/edges.rb', line 123

def do_not_perpetuate
  @do_not_perpetuate
end

Instance Method Details

#ancestor_idObject

Id of the ancestor



163
164
165
# File 'lib/dag/edges.rb', line 163

def ancestor_id
  self[ancestor_id_column_name]
end

#countObject

Count of the edge, ie the edge exists in X ways



173
174
175
# File 'lib/dag/edges.rb', line 173

def count
  self[count_column_name]
end

#descendant_idObject

Id of the descendant



168
169
170
# File 'lib/dag/edges.rb', line 168

def descendant_id
  self[descendant_id_column_name]
end

#destroyable!Object

Raises an exception if the edge is not destroyable. Otherwise makes the edge indirect before destruction to cleanup graph.

Raises:

  • (ActiveRecord::ActiveRecordError)


137
138
139
140
141
142
143
144
# File 'lib/dag/edges.rb', line 137

def destroyable!
  raise ActiveRecord::ActiveRecordError, 'ERROR: cannot destroy this edge' unless destroyable?
  #this triggers rewiring on destruction via perpetuate
  if self.direct?
    self[direct_column_name] = false
  end
  true
end

#destroyable?Boolean

Whether the edge can be destroyed

Returns:

  • (Boolean)


132
133
134
# File 'lib/dag/edges.rb', line 132

def destroyable?
  (self.count == 0) || (self.direct? && self.count == 1)
end

#direct?Boolean

Whether the link is direct, ie manually created

Returns:

  • (Boolean)


183
184
185
# File 'lib/dag/edges.rb', line 183

def direct?
  self[direct_column_name]
end

#edge?Boolean

Whether the link is an edge?

Returns:

  • (Boolean)


188
189
190
# File 'lib/dag/edges.rb', line 188

def edge?
  self[direct_column_name]
end

#fill_defaultsObject

Fill default direct and count values if necessary. In place of after_initialize method



126
127
128
129
# File 'lib/dag/edges.rb', line 126

def fill_defaults
  self[direct_column_name] = true if self[direct_column_name].nil?
  self[count_column_name] = 0 if self[count_column_name].nil?
end

#internal_count=(val) ⇒ Object

Changes the count of the edge. DO NOT CALL THIS OUTSIDE THE PLUGIN



178
179
180
# File 'lib/dag/edges.rb', line 178

def internal_count=(val)
  self[count_column_name] = val
end

all links that start from the sink



220
221
222
# File 'lib/dag/edges.rb', line 220

def links_from_sink
  self.class.with_ancestor_point(self.sink)
end

All links that end at the source



215
216
217
# File 'lib/dag/edges.rb', line 215

def links_to_source
  self.class.with_descendant_point(self.source)
end

#make_directObject

Makes the link direct, ie an edge



193
194
195
# File 'lib/dag/edges.rb', line 193

def make_direct
  self[direct_column_name] = true
end

#make_indirectObject

Makes an edge indirect, ie a link.



198
199
200
# File 'lib/dag/edges.rb', line 198

def make_indirect
  self[direct_column_name] = false
end

#perpetuateObject

Analyzes the changes in a model instance and rewires as necessary.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/dag/edges.rb', line 147

def perpetuate
  #flag set by links that were modified in association
  return true if self.do_not_perpetuate

  #if edge changed this was manually altered
  if direct_changed?
    if self.direct?
      self[count_column_name] += 1
    else
      self[count_column_name] -= 1
    end
    self.wiring
  end
end

#sinkObject

Sink (destination) of the edge, creates if necessary



209
210
211
212
# File 'lib/dag/edges.rb', line 209

def sink
  @sink = self.class::Sink.from_edge(self) if @sink.nil?
  @sink
end

#sourceObject

Source of the edge, creates if necessary



203
204
205
206
# File 'lib/dag/edges.rb', line 203

def source
  @source = self.class::Source.from_edge(self) if @source.nil?
  @source
end