Sha256: 2920680b84dbecb44a47ef2d77a6553b4714e2a2dc82a2e513bbd54907778b65

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

module ActiveRecordSurvey
	# Rank in relation to parent/children of ActiveRecordSurvey::Node::Answer::Rank
	class Node::Answer::Rank < Node::Answer
		include Answer::Chained

		# Accept integer or empty values
		# Must be within range of the number of ranking nodes
		def validate_instance_node(instance_node)
			# super - all validations on this node pass
			super &&
			(instance_node.value.to_s.empty? || !instance_node.value.to_s.match(/^\d+$/).nil?) &&
			(instance_node.value.to_s.empty? || instance_node.value.to_i >= 1) &&
			instance_node.value.to_i <= self.max_rank
		end

		# Rank answers are considered answered if they have a value of greater than "0"
		def is_answered_for_instance?(instance)
			if instance_node = self.instance_node_for_instance(instance)
				# Answered if > 0
				instance_node.value.to_i > 0
			end
		end

		protected

		# Calculate the number of Rank nodes above this one
		def num_above
			count = 0
			self.node_maps.each { |i|
				# Parent is one of us as well - include it and check its parents
				if i.parent.node.class.ancestors.include?(self.class)
					count = count + 1 + i.parent.node.num_above
				end
			}
			count
		end

		# Calculate the number of Rank nodes below this one
		def num_below
			count = 0
			self.node_maps.each { |node_map|
				node_map.children.each { |child|
					# Child is one of us as well - include it and check its children
					if child.node.class.ancestors.include?(self.class)
						count = count + 1 + child.node.num_below
					end
				}
			}
			count
		end

		# Calculate the maximum rank value that is accepted
		def max_rank
			self.num_above + self.num_below + 1
		end
	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_record_survey-0.1.28 lib/active_record_survey/node/answer/rank.rb
active_record_survey-0.1.27 lib/active_record_survey/node/answer/rank.rb
active_record_survey-0.1.26 lib/active_record_survey/node/answer/rank.rb
active_record_survey-0.1.25 lib/active_record_survey/node/answer/rank.rb