Skip to content

Commit 4d92208

Browse files
committed
Start refactoring of QC
1 parent 9acf3c0 commit 4d92208

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

app/models/name/quality_checks.rb

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class QcWarning
77

88
# Attributes supported for warnings
99
@@attributes = %i[
10+
scope validity_test
1011
message link_text link_to rules recommendations rule_notes
1112
can_endorse link_public checklist area
1213
]
@@ -44,7 +45,8 @@ class QcWarning
4445
},
4546
candidatus_modifier: {
4647
message: 'The name has a Candidatus modifier that should be removed',
47-
area: :nomenclature
48+
area: :nomenclature,
49+
validity_test: lambda { |w| w.name.candidatus? }
4850
}.merge(@@link_to_edit_spelling),
4951
inconsistent_syllabification: {
5052
message: 'The syllabification does not correspond to the proposed ' \
@@ -780,14 +782,18 @@ class QcWarning
780782
attr_writer *@@attributes
781783

782784
@@attributes.each do |k|
785+
define_method("variable_#{k}") do
786+
instance_variable_get("@#{k}")
787+
end
783788
define_method(k) do
784-
v = instance_variable_get("@#{k}")
789+
v = send("variable_#{k}")
785790
v.is_a?(Proc) ? v.call(self) : v
786791
end
787792
end
788793

789794
def initialize(type, opts)
790795
@type = type.to_sym
796+
@scope = true # always in-scope unless explicitly defined
791797
defaults.each { |k, v| send("#{k}=", v) }
792798
opts.each { |k, v| send("#{k}=", v) }
793799
end
@@ -851,8 +857,9 @@ def initialize(name)
851857
@bypassed_h = {}
852858
end
853859

854-
def add(type, opts = {})
855-
qc = QcWarning.new(type, opts.merge(name: name))
860+
def add(type_or_qc, opts = {})
861+
qc = type_or_qc.is_a?(QcWarning) ? type_or_qc :
862+
QcWarning.new(type, opts.merge(name: name))
856863
@checks_h[qc.type] = qc if qc.checklist
857864
if (!qc.checklist && !qc.check) || (qc.check && qc.check.fail?)
858865
@set_h[qc.type] = qc
@@ -861,6 +868,25 @@ def add(type, opts = {})
861868
end
862869
end
863870

871+
def <<(qc)
872+
add(qc)
873+
end
874+
875+
##
876+
# Uses the quality check definition to evaluate it in the current name and
877+
# returns +false+ if it's out of scope or the test is valid (i.e., not a
878+
# concern for the present name), +true+ if it's in scope and the test is not
879+
# valid (i.e., a concern), or +nil+ if no validity test is defined
880+
def evaluate(type, opts = {})
881+
qc = QcWarning.new(type, opts.merge(name: name))
882+
return unless qc.variable_validity_test
883+
return false unless qc.scope
884+
return false unless qc.validity_test
885+
886+
self << qc
887+
true
888+
end
889+
864890
def set
865891
return @set_h.values unless name.qc_for_tutorial
866892
@set_h.values.select(&:is_tutorial_error?)
@@ -927,7 +953,8 @@ def qc_warnings
927953
@qc_warnings = QcWarningSet.new(self)
928954
return @qc_warnings if inferred_rank == 'domain'
929955

930-
@qc_warnings.add(:candidatus_modifier) if candidatus?
956+
#@qc_warnings.add(:candidatus_modifier) if candidatus?
957+
@qc_warnings.evaluate(:candidatus_modifier)
931958
@qc_warnings.add(:missing_rank) unless rank?
932959
@qc_warnings.add(:identical_base_name) unless identical_base_name.nil?
933960
@qc_warnings.add(:identical_external_name) unless external_homonyms.empty?

0 commit comments

Comments
 (0)