Class: ActiveRecordSurvey::NodeMap

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/active_record_survey/node_map.rb

Instance Method Summary collapse

Instance Method Details

#ancestors_until_node_not_ancestor_of(klass) ⇒ Object

Gets all the ancestor nodes until one is not an ancestor of klass



56
57
58
59
60
61
62
# File 'lib/active_record_survey/node_map.rb', line 56

def ancestors_until_node_not_ancestor_of(klass)
	if !self.parent || !self.node.class.ancestors.include?(klass)
		return []
	end

	[self] + self.parent.ancestors_until_node_not_ancestor_of(klass)
end

#as_map(options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_record_survey/node_map.rb', line 20

def as_map(options)
	node_maps = options[:node_maps]

	c = (node_maps.nil?)? self.children : node_maps.select { |i|
		i.parent == self && !i.marked_for_destruction?
	}.sort { |a,b| a.left<=>b.left }

	result = {}
	result.merge!({ :id => self.id, :node_id => ((self.node.respond_to?(:id))? self.node.id : "") }) if !options[:no_ids] && !self.node.nil?
	result.merge!({
		:type => ((!self.node.nil?)? self.node.class.to_s : ""),
		:children => c.collect { |i|
			i.as_map(options)
		}
	})
	

	result
end

#children_until_node_not_ancestor_of(klass) ⇒ Object

Gets all the child nodes until one is not an ancestor of klass



65
66
67
68
69
70
71
72
73
# File 'lib/active_record_survey/node_map.rb', line 65

def children_until_node_not_ancestor_of(klass)
	if !self.node.class.ancestors.include?(klass)
		return []
	end

	[self] + self.children.collect { |i|
		i.children_until_node_not_ancestor_of(klass)
	}
end

#has_infinite_loop?(path = []) ⇒ Boolean

Check to see whether there is an infinite loop from this node_map

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
# File 'lib/active_record_survey/node_map.rb', line 76

def has_infinite_loop?(path = [])
	self.survey.node_maps.select { |i| i.parent == self && !i.marked_for_destruction? }.each { |i|
		# Detect infinite loop
		if path.include?(self.node) || i.has_infinite_loop?(path.clone.push(self.node))
			return true
		end
	}
	path.include?(self.node)
end

#is_decendant_of?(node_map) ⇒ Boolean

Whether decendant of a particular node_map

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_record_survey/node_map.rb', line 41

def is_decendant_of?(node_map)
	# Hit ourselves
	if node_map == self
		return true
	end

	# Recurse
	if self.parent
		return self.parent.is_decendant_of?(node_map)
	end

	false
end

#mark_self_and_children_for_destructionObject



86
87
88
89
90
91
92
93
# File 'lib/active_record_survey/node_map.rb', line 86

def mark_self_and_children_for_destruction
	removed = [self]
	self.mark_for_destruction
	self.children.each { |i|
		removed.concat(i.mark_self_and_children_for_destruction)
	}
	removed
end

#recursive_cloneObject

Recursively creates a copy of this entire node_map



11
12
13
14
15
16
17
18
# File 'lib/active_record_survey/node_map.rb', line 11

def recursive_clone
	node_map = self.survey.node_maps.build(:survey => self.survey, :node => self.node)
	self.survey.node_maps.select { |i| i.parent == self && !i.marked_for_destruction? }.each { |child_node|
		child_node.survey = self.survey # required due to voodoo - we want to use the same survey with the same object_id
		node_map.children << child_node.recursive_clone
	}
	node_map
end