lib/ncs_navigator/mdes/variable.rb in ncs_mdes-0.11.0 vs lib/ncs_navigator/mdes/variable.rb in ncs_mdes-0.12.0

- old
+ new

@@ -109,14 +109,10 @@ def initialize(name) @name = name end - def constraints - @constraints ||= [] - end - ## # Is a value for the variable mandatory for a valid submission? # # @return [Boolean] def required? @@ -202,8 +198,66 @@ "#{candidates.size} possible parent tables found for foreign key #{name.inspect} " << "in #{source_table}: #{candidates.collect { |c| c.name.inspect }.join(', ')}. " << "None used due to ambiguity.") end end + end + + # @private + class EmbeddedVariableTypeCriterion + def apply(vt1, vt2, diff_options) + cvt1 = vt1 || VariableType.new + cvt2 = vt2 || VariableType.new + + + if cvt1.name && cvt2.name && cvt1.name == cvt2.name + # If they are named and have the same name, the differences will + # be reported once under the specification's entry for the named type. + nil + else + cvt1.diff(cvt2, diff_options) + end + end + end + + def diff_criteria(diff_options={}) + base = { + :name => Differences::ValueCriterion.new, + :type => EmbeddedVariableTypeCriterion.new, + :pii => Differences::ValueCriterion.new, + :omittable? => Differences::ValueCriterion.new(:comparator => :predicate), + :nillable? => Differences::ValueCriterion.new(:comparator => :predicate), + :table_reference => Differences::ValueCriterion.new( + :value_extractor => lambda { |o| o ? o.name : nil } + ) + } + + if diff_options[:strict] + base[:status] = Differences::ValueCriterion.new + else + base[:status] = Differences::ValueCriterion.new( + :comparator => lambda { |left, right| + no_change_changes = [ + [:new, :active], + [:new, :modified], + [:active, :modified], + [:modified, :active] + ] + + no_change_changes.include?([left, right]) || (left == right) + } + ) + end + + base + end + protected :diff_criteria + + ## + # Computes the differences between this variable and the other. + # + # @return [Differences::Entry,nil] + def diff(other_variable, options={}) + Differences::Entry.compute(self, other_variable, diff_criteria(options), options) end end end