lib/ncs_navigator/mdes/variable.rb in ncs_mdes-0.4.0 vs lib/ncs_navigator/mdes/variable.rb in ncs_mdes-0.4.1

- old
+ new

@@ -29,17 +29,32 @@ ## # @return [VariableType] the type of this variable. attr_accessor :type ## - # Is the variable mandatory for a valid submission? + # If the variable does not have a value, should it be completely + # omitted when submitting to the VDR? # # @return [Boolean] - attr_accessor :required - alias :required? :required + attr_accessor :omittable + alias :omittable? :omittable ## + # May the variable be submitted as a null value? + # + # @return [Boolean] + attr_accessor :nillable + alias :nillable? :nillable + + ## + # Allows for an override of the default logic. Mostly intended for + # testing. Set to `nil` to restore default logic. + # + # @return [Boolean,nil] + attr_writer :required + + ## # If this variable is a foreign key, this is the {table # TransmissionTable} to which it refers. # # @return [TransmissionTable,nil] the parent table. attr_accessor :table_reference @@ -55,16 +70,12 @@ # @return [Variable] a new variable instance def from_element(element, options={}) log = options[:log] || NcsNavigator::Mdes.default_logger new(element['name']).tap do |var| - var.required = - if element['minOccurs'] - element['minOccurs'] != '0' - else - element['nillable'] == 'false' - end + var.nillable = element['nillable'] == 'true' + var.omittable = element['minOccurs'] == '0' var.pii = case element['pii'] when 'Y'; true; when 'P'; :possible; when nil; :unknown; @@ -101,9 +112,22 @@ end def constraints @constraints ||= [] end + + ## + # Is a value for the variable mandatory for a valid submission? + # + # @return [Boolean] + def required? + if @required.nil? + !(omittable? || nillable?) + else + @required + end + end + alias :required :required? ## # If the {#type} of this instance is a reference to an NCS type, # attempts to replace it with the full version from the given list # of types.