mirror of
https://github.com/internetee/registry.git
synced 2025-07-22 02:35:57 +02:00
Add a way to get latest version of an xsd schema by prefix
This commit is contained in:
parent
b2226e3ea2
commit
74e3d5461b
1 changed files with 36 additions and 0 deletions
36
app/lib/xsd_util.rb
Normal file
36
app/lib/xsd_util.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue