Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Potential fix for issue #104 (see lib/validation.rb for details). E…

…ssentially, changed all use of require to require_dependency within the lib validation code and initializer. Also moved the initialization of validators into lib/validation.rb itself so that it can be reloaded.

*MatchHelper#safe_match - an error message with #{} content was single quoted.  I double quoted for interpolation, but it's possible that was incorrect and the intention is for the message to be evaled at a later date?  Hopefully not :)
  • Loading branch information...
commit 668aa38ae7797b2102fbb51d45ee672e2a93d130 1 parent 1ff9186
Josh Partlow authored
View
67 config/initializers/laika_validation.rb
@@ -1,61 +1,8 @@
-require 'xml_helper'
-require 'laika/constants'
-
-require 'validation'
-require 'validators/c62'
-require 'validators/c32_validator'
-require 'validators/schema_validator'
-require 'validators/schematron_validator'
-require 'validators/umls_validator'
-require 'validators/xds_metadata_validator'
-
-{
- 'C32 v2.1/v2.3' => [
- Validators::C32Validation::Validator.new,
- Validators::Schema::Validator.new("C32 Schema Validator",
- "resources/schemas/infrastructure/cda/C32_CDA.xsd"),
- Validators::Schematron::CompiledValidator.new("CCD Schematron Validator",
- "resources/schematron/ccd_errors.xslt"),
- Validators::Schematron::CompiledValidator.new("C32 Schematron Validator",
- "resources/schematron/c32_v2.1/c32_v2.1_errors.xslt"),
- Validators::Umls::UmlsValidator.new("warning")
- ],
- 'C32 v2.4' => [
- Validators::C32Validation::Validator.new,
- Validators::Schema::Validator.new("C32 Schema Validator",
- "resources/schemas/infrastructure/cda/C32_CDA.xsd"),
- Validators::Schematron::CompiledValidator.new("CCD Schematron Validator",
- "resources/schematron/ccd_errors.xslt"),
- Validators::Schematron::CompiledValidator.new("C32 Schematron Validator",
- "resources/schematron/c32_v2.4/c32_v2.4_errors.xslt"),
- Validators::Umls::UmlsValidator.new("warning")
- ],
- 'C32 v2.5' => [
- Validators::C32Validation::Validator.new,
- Validators::Schema::Validator.new("C32 Schema Validator",
- "resources/schemas/infrastructure/cda/C32_CDA.xsd"),
- Validators::Schematron::CompiledValidator.new("CCD Schematron Validator",
- "resources/schematron/ccd_errors.xslt"),
- Validators::Schematron::CompiledValidator.new("C32 Schematron Validator",
- "resources/schematron/c32_v2.5/c32_v2.5_errors.xslt"),
- Validators::Umls::UmlsValidator.new("warning")
- ],
- 'NHIN C32' => [
- Validators::C32Validation::Validator.new,
- Validators::Schema::Validator.new("C32 Schema Validator",
- "resources/schemas/infrastructure/cda/C32_CDA.xsd"),
- Validators::Schematron::CompiledValidator.new("CCD Schematron Validator",
- "resources/schematron/ccd_errors.xslt"),
- Validators::Schematron::CompiledValidator.new("C32 Schematron Validator",
- "resources/schematron/c32_v2.1_errors.xslt"),
- Validators::Schematron::CompiledValidator.new("NHIN Schematron Validator",
- "resources/nhin_schematron/nhin_errors.xsl"),
- Validators::Umls::UmlsValidator.new("warning")
- ],
- 'C62' => [ Validators::C62::Validator.new ]
-}.each do |type, validators|
- validators.each do |validator|
- Validation.register_validator type.to_sym, validator
- end
-end
+require_dependency 'xml_helper'
+require_dependency 'laika/constants'
+# Requiring this initializes the validators.
+# Validator initialization was moved from here
+# into lib/validation.rb to address #104
+# See lib/validation.rb comments for details.
+require_dependency 'validation'
View
4 lib/match_helper.rb
@@ -28,7 +28,7 @@ def safe_match(element,&block)
return nil
rescue
return ContentError.new(:section => section_name,
- :error_message => 'Error during validation of the #{section_name} section: #{$!}',
+ :error_message => "Error during validation of the #{section_name} section: #{$!}",
:type=>'error',
:location => element.xpath)
end
@@ -91,4 +91,4 @@ def deref(code)
end
end
end
- end
+ end
View
92 lib/validation.rb
@@ -107,6 +107,96 @@ def get_validator(type)
end
end
+# XXX Evaluate a better validator initialization strategy.
+#
+# This was moved to allow the config/initializers/laika_validation.rb
+# step to use require_dependency instead of require. Using require
+# was causing github issue #104 with config.cache_classes = false
+# because requiring validators/c32_validator would load an instance
+# of MatchHelper which Rails would not track in its dependency tracking
+# while other models would also load MatchHelper such that it would
+# be tracked for reloading by Rails, with subsequent conflict:
+#
+# http://www.ruby-forum.com/topic/153066
+# http://spacevatican.org/2008/9/28/required-or-not
+#
+# In summary: it is fine to require external sources (third party libraries
+# and such), but requiring internal code that will reference other code
+# which Rails will be doing dependency tracking on leads to pain. Use
+# require_dependency instead.
+#
+# The problem with keeping this code in the initializer even after
+# require has been switched to require_dependency, is that with
+# config.cache_classes = false, this file is reloaded with each request and
+# a new Singleton ValidationRegistry is created. Without the code below,
+# it will not be initialized with any validators.
+#
+# Possible alternatives:
+#
+# * use a global constant in the initializer?
+# * use a singleton as the global constant in the initializer?
+# * ?
+#
+# It also may be worthwhile to sort out the module naming/pathing
+# for all the validators so that Rails can find these classes
+# for reloading in development (cache_classes = false) mode.
+#
+# (The reason cache_classes = false in production mode is
+# tied to issue #62...)
+require_dependency 'validators/c62'
+require_dependency 'validators/c32_validator'
+require_dependency 'validators/schema_validator'
+require_dependency 'validators/schematron_validator'
+require_dependency 'validators/umls_validator'
+require_dependency 'validators/xds_metadata_validator'
-
+{
+ 'C32 v2.1/v2.3' => [
+ Validators::C32Validation::Validator.new,
+ Validators::Schema::Validator.new("C32 Schema Validator",
+ "resources/schemas/infrastructure/cda/C32_CDA.xsd"),
+ Validators::Schematron::CompiledValidator.new("CCD Schematron Validator",
+ "resources/schematron/ccd_errors.xslt"),
+ Validators::Schematron::CompiledValidator.new("C32 Schematron Validator",
+ "resources/schematron/c32_v2.1/c32_v2.1_errors.xslt"),
+ Validators::Umls::UmlsValidator.new("warning")
+ ],
+ 'C32 v2.4' => [
+ Validators::C32Validation::Validator.new,
+ Validators::Schema::Validator.new("C32 Schema Validator",
+ "resources/schemas/infrastructure/cda/C32_CDA.xsd"),
+ Validators::Schematron::CompiledValidator.new("CCD Schematron Validator",
+ "resources/schematron/ccd_errors.xslt"),
+ Validators::Schematron::CompiledValidator.new("C32 Schematron Validator",
+ "resources/schematron/c32_v2.4/c32_v2.4_errors.xslt"),
+ Validators::Umls::UmlsValidator.new("warning")
+ ],
+ 'C32 v2.5' => [
+ Validators::C32Validation::Validator.new,
+ Validators::Schema::Validator.new("C32 Schema Validator",
+ "resources/schemas/infrastructure/cda/C32_CDA.xsd"),
+ Validators::Schematron::CompiledValidator.new("CCD Schematron Validator",
+ "resources/schematron/ccd_errors.xslt"),
+ Validators::Schematron::CompiledValidator.new("C32 Schematron Validator",
+ "resources/schematron/c32_v2.5/c32_v2.5_errors.xslt"),
+ Validators::Umls::UmlsValidator.new("warning")
+ ],
+ 'NHIN C32' => [
+ Validators::C32Validation::Validator.new,
+ Validators::Schema::Validator.new("C32 Schema Validator",
+ "resources/schemas/infrastructure/cda/C32_CDA.xsd"),
+ Validators::Schematron::CompiledValidator.new("CCD Schematron Validator",
+ "resources/schematron/ccd_errors.xslt"),
+ Validators::Schematron::CompiledValidator.new("C32 Schematron Validator",
+ "resources/schematron/c32_v2.1_errors.xslt"),
+ Validators::Schematron::CompiledValidator.new("NHIN Schematron Validator",
+ "resources/nhin_schematron/nhin_errors.xsl"),
+ Validators::Umls::UmlsValidator.new("warning")
+ ],
+ 'C62' => [ Validators::C62::Validator.new ]
+}.each do |type, validators|
+ validators.each do |validator|
+ Validation.register_validator type.to_sym, validator
+ end
+end
View
2  lib/validators/c32_validator.rb
@@ -4,7 +4,7 @@
# FIXME Requiring files using the full path is bad practice.
# Ideally we should rename/namespace the *C32Validation modules so that they
# can be autoloaded by Rails.
-Dir.glob(File.join(File.dirname(__FILE__), 'c32/*.rb')).each {|f| require f }
+Dir.glob(File.join(File.dirname(__FILE__), 'c32/*.rb')).each {|f| require_dependency f }
module Validators
Please sign in to comment.
Something went wrong with that request. Please try again.