-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move class to lib, add sugar, add tests
- Loading branch information
Showing
10 changed files
with
71 additions
and
36 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'application_service' | ||
require 'xsd/util' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ApplicationService | ||
def self.call(*args, &block) | ||
new(*args, &block).call | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |