# Copyright (C) 2011 AMEE UK Ltd. - http://www.amee.com
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
# :title: Class: AMEE::DataAbstraction::Drill
module AMEE
module DataAbstraction
# Subclass of Input providing methods and attributes appropriate for
# representing AMEE drill down choices and selections specifically
#
class Drill < Input
public
# Returns true if the UI element of self is disabled.
# Otherwise, returns false.
#
# A drill is considered disabled if it either (1) explicitly set using the
# #disable! method; (2) has a fixed value; or (3) is not the
# next drill (because drill should be chosen in order).
#
def disabled?
super || (!set? && !next?)
end
# Initialization of Drill objects follows that of the parent
# Input class. The interface attribute of self is
# set to :drop_down by default for Drill instances, but
# can be manually configured if required.
#
def initialize(options={},&block)
interface :drop_down
super
choice_validation_message
end
# Returns the list of available choices for self. A custom list of
# choices can be provided as an argument, in which case these will override
# the list provided by the AMEE platform
#
def choices(*args)
if args.empty?
if @choices.blank?
c=parent.amee_drill(:before=>label).choices
c.length==1 ? [value] : c
else
@choices
end
else
@choices = [args].flatten
end
end
private
# Returns true if the value set for self is one of the
# available choices. Otherwise, returns false.
#
def valid?
super && (choices.include? value)
end
# Returns true if self is the "first unset" drill, i.e.
# the one which should be set next
#
def next?
unset? && parent.drills.before(label).all?(&:set?)
end
end
end
end