Add a way to get latest version of an xsd schema by prefix

This commit is contained in:
Alex Sherman 2021-05-20 14:03:22 +05:00
parent b2226e3ea2
commit 74e3d5461b

36
app/lib/xsd_util.rb Normal file
View file

@ -0,0 +1,36 @@
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