diff --git a/app/lib/xsd_util.rb b/app/lib/xsd_util.rb deleted file mode 100644 index 99a648841a..0000000000 --- a/app/lib/xsd_util.rb +++ /dev/null @@ -1,36 +0,0 @@ -class XsdUtil - SCHEMA_PATH = 'lib/schemas/'.freeze - - def initialise(schema_path = SCHEMA_PATH) - @schema_path = schema_path - end - - def xsd_schemas - @xsd_schemas ||= Dir.entries(SCHEMA_PATH) - .select { |f| File.file? File.join(SCHEMA_PATH, f) } - end - - def basename(filename) - File.basename(filename, '.xsd') - end - - def prefix(filename) - regex = /([a-zA-Z]+-?[a-zA-Z]+)/ - - basename(filename).match(regex)[0] - end - - def prefixes - xsd_schemas.map { |filename| prefix(filename) }.uniq - end - - def schemas_by_name - prefixes.each_with_object({}) do |prefix, hash| - hash[prefix] = xsd_schemas.select { |filename| filename.include? prefix }.uniq.sort - end - end - - def latest(prefix) - schemas_by_name[prefix].last - end -end diff --git a/config/initializers/xsd.rb b/config/initializers/xsd.rb new file mode 100644 index 0000000000..a02fe2dae1 --- /dev/null +++ b/config/initializers/xsd.rb @@ -0,0 +1,2 @@ +require 'application_service' +require 'xsd/util' diff --git a/lib/application_service.rb b/lib/application_service.rb new file mode 100644 index 0000000000..6185f03147 --- /dev/null +++ b/lib/application_service.rb @@ -0,0 +1,5 @@ +class ApplicationService + def self.call(*args, &block) + new(*args, &block).call + end +end diff --git a/lib/xsd/util.rb b/lib/xsd/util.rb new file mode 100644 index 0000000000..196175cbee --- /dev/null +++ b/lib/xsd/util.rb @@ -0,0 +1,48 @@ +module Xsd + class Util < ApplicationService + SCHEMA_PATH = 'lib/schemas/'.freeze + + attr_reader :xsd_schemas, :for_prefix + + def initialize(params) + schema_path = params.fetch(:schema_path, SCHEMA_PATH) + @for_prefix = params.fetch(:for_prefix) + @xsd_schemas = Dir.entries(schema_path).select { |f| File.file? File.join(schema_path, f) } + end + + def call + latest(for_prefix) + end + + private + + def latest(prefix) + schemas_by_name[prefix].last + end + + def basename(filename) + File.basename(filename, '.xsd') + end + + def prefix(filename) + regex = /([a-zA-Z]+-?[a-zA-Z]+)/ + + basename(filename).match(regex)[0] + end + + def prefixes + xsd_schemas.map { |filename| prefix(filename) }.uniq + end + + def schemas_by_name + prefixes.each_with_object({}) do |prefix, hash| + hash[prefix] = xsd_schemas.select { |filename| prefix_check(prefix, filename) }.uniq.sort + end + end + + def prefix_check(prefix, filename) + version_regex = /\-\d+\S\d+/ + (filename.include? prefix) && (filename.sub(prefix, '')[0, 4] =~ version_regex) + end + end +end diff --git a/test/fixtures/files/schemas/abcde-1.1.xsd b/test/fixtures/files/schemas/abcde-1.1.xsd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/fixtures/files/schemas/abcde-1.2.xsd b/test/fixtures/files/schemas/abcde-1.2.xsd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/fixtures/files/schemas/abcde-fghij-1.1.xsd b/test/fixtures/files/schemas/abcde-fghij-1.1.xsd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/fixtures/files/schemas/abcde-fghij-1.2.xsd b/test/fixtures/files/schemas/abcde-fghij-1.2.xsd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/fixtures/files/schemas/abcde-fghij-1.3.xsd b/test/fixtures/files/schemas/abcde-fghij-1.3.xsd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/lib/xsd_util/xsd_util_test.rb b/test/lib/xsd_util/xsd_util_test.rb new file mode 100644 index 0000000000..d2d70a68f8 --- /dev/null +++ b/test/lib/xsd_util/xsd_util_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' +require 'xsd/util' + +class XsdUtilTest < ActiveSupport::TestCase + def test_single_part_name + version = Xsd::Util.call(schema_path: 'test/fixtures/files/schemas', for_prefix: 'abcde') + + assert_equal 'abcde-1.2.xsd', version + end + + def test_double_part_name + version = Xsd::Util.call(schema_path: 'test/fixtures/files/schemas', for_prefix: 'abcde-fghij') + + assert_equal 'abcde-fghij-1.3.xsd', version + end +end