lib/amee-data-abstraction/input.rb in amee-data-abstraction-1.2.0 vs lib/amee-data-abstraction/input.rb in amee-data-abstraction-1.3.0

- old
+ new

@@ -9,10 +9,12 @@ # Subclass of <tt>Term</tt> providing methods and attributes appropriate for # representing calculation inputs specifically # class Input < Term + attr_accessor :dirty + # Returns the valid choices for this input # (Abstract, implemented only for subclasses of input.) def choices raise NotImplementedError end @@ -27,10 +29,11 @@ # def initialize(options={},&block) @validation = nil validation_message {"#{name} is invalid."} super + @dirty = false end # Configures the value of <tt>self</tt> to be fixed to <tt>val</tt>, i.e. # the value is read-only. # @@ -65,10 +68,11 @@ def value(*args) unless args.empty? if args.first.to_s != @value.to_s raise Exceptions::FixedValueInterference if fixed? parent.dirty! if parent and parent.is_a? OngoingCalculation + mark_as_dirty end end super end @@ -153,10 +157,20 @@ # def compulsory?(usage=nil) !optional?(usage) end + # Manually set the term as optional + def optional! + @optional=true + end + + # Manually set the term as compuslory + def compulsory! + @optional=false + end + # Check that the value of <tt>self</tt> is valid. If invalid, and is defined # as part of a calculation, add the invalidity message to the parent # calculation's error list. Otherwise, raise a <i>ChoiceValidation</i> # exception. # @@ -183,15 +197,25 @@ # def disabled? super || fixed? end + def dirty? + @dirty + end + protected # Returns <tt>true</tt> if the value set for <tt>self</tt> is either blank # or passes custom validation criteria. Otherwise, returns <tt>false</tt>. # def valid? validation.blank? || validation === @value_before_cast end + + def mark_as_dirty + @dirty = true + parent.dirty! if parent and parent.is_a? OngoingCalculation + end + end end end \ No newline at end of file