lib/ncs_navigator/mdes/specification.rb in ncs_mdes-0.2.0 vs lib/ncs_navigator/mdes/specification.rb in ncs_mdes-0.3.0

- old
+ new

@@ -1,10 +1,11 @@ require 'ncs_navigator/mdes' require 'forwardable' require 'logger' require 'nokogiri' +require 'yaml' module NcsNavigator::Mdes class Specification extend Forwardable @@ -41,10 +42,31 @@ def xsd @xsd ||= Nokogiri::XML(File.read source_documents.schema) end ## + # @return [Hash] the loaded heuristic overrides, or a default + # (empty) set + def heuristic_overrides + @heuristic_overrides ||= + begin + if File.exist?(source_documents.heuristic_overrides) + empty_overrides.merge(YAML.load(File.read source_documents.heuristic_overrides)) + else + empty_overrides + end + end + end + + def empty_overrides + { + 'foreign_keys' => { } + } + end + private :empty_overrides + + ## # @return [Array<TransmissionTable>] all the transmission tables # in this version of the MDES. def transmission_tables @transmission_tables ||= read_transmission_tables end @@ -55,12 +77,44 @@ source_documents.xmlns ).collect { |table_elt| TransmissionTable.from_element(table_elt, :log => @log) }.tap { |tables| tables.each { |t| t.variables.each { |v| v.resolve_type!(types, :log => @log) } } + # All types must be resolved before doing FK resolution or + # forward refs are missed. + tables.each { |t| + fk_overrides = heuristic_overrides['foreign_keys'][t.name] || { } + t.variables.each { |v| + v.resolve_foreign_key!(tables, fk_overrides[v.name], :log => @log) + } + } } end private :read_transmission_tables + + ## + # A shortcut for accessing particular {#transmission_tables}. + # + # @overload [](table_name) + # Retrieves a single table by name. + # @param [String] table_name the transmission table to return. + # @return [TransmissionTable,nil] the matching table or nothing. + # + # @overload [](pattern) + # Searches the transmission tables by name. + # @param [Regexp] pattern the pattern to match the name against. + # @return [Array<TransmissionTable>] the matching tables (or an + # empty array). + def [](criterion) + case criterion + when Regexp + transmission_tables.select { |t| t.name =~ criterion } + when String + transmission_tables.detect { |t| t.name == criterion } + else + fail "Unexpected criterion #{criterion.inspect}" + end + end ## # @return [Array<VariableType>] all the named types in the # MDES. This includes all the code lists. def types