Module: Dcc::Schema::Version

Defined in:
lib/dcc/schema/version.rb

Constant Summary collapse

DCC_ALL =
%w[
  2.1.0 2.1.1 2.2.0 2.3.0 2.4.0
  3.0.0 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.3.0
].freeze
DCC_LATEST =
"3.3.0"
DCC_DEFAULT =
DCC_LATEST
DSI_ALL =
%w[
  1.0.1 1.3.0 1.3.1 2.0.0 2.1.0 2.2.1
].freeze
DSI_LATEST =
"2.2.1"
DSI_DEFAULT =
DSI_LATEST
QUDT_VERSION =
"2.2.1"

Class Method Summary collapse

Class Method Details

.dcc?(v) ⇒ Boolean

Returns whether the version is a known DCC release.

Parameters:

  • v (String, Symbol)

    candidate version.

Returns:

  • (Boolean)

    whether the version is a known DCC release.



39
40
41
# File 'lib/dcc/schema/version.rb', line 39

def dcc?(v)
  DCC_ALL.include?(normalize(v))
end

.detect_from_xml(xml) ⇒ String?

Extract the schemaVersion attribute from an XML string.

Returns:

  • (String, nil)


84
85
86
87
88
89
90
# File 'lib/dcc/schema/version.rb', line 84

def detect_from_xml(xml)
  return nil unless xml

  str = ::Dcc.read_input(xml)
  match = str.match(/schemaVersion\s*=\s*["']([^"']+)["']/)
  match && match[1]
end

.dsi?(v) ⇒ Boolean

Returns whether the version is a known D-SI release.

Parameters:

  • v (String, Symbol)

    candidate version.

Returns:

  • (Boolean)

    whether the version is a known D-SI release.



45
46
47
# File 'lib/dcc/schema/version.rb', line 45

def dsi?(v)
  DSI_ALL.include?(normalize(v))
end

.major(v) ⇒ Object

Major version of a semver string ("3.3.0"3).



50
51
52
# File 'lib/dcc/schema/version.rb', line 50

def major(v)
  Integer(normalize(v).split(".").first)
end

.normalize(v) ⇒ String?

Normalize a version input to a bare "X.Y.Z" String.

Parameters:

  • v (String, Symbol)

    e.g. :v3_3_0, "3.3.0", "v3.3.0".

Returns:

  • (String, nil)


30
31
32
33
34
35
# File 'lib/dcc/schema/version.rb', line 30

def normalize(v)
  return nil if v.nil?

  s = v.to_s.sub(/\Av/, "").gsub(/_/, ".")
  s.empty? ? nil : s
end

.resolve_dcc(version, xml: nil) ⇒ Object

Resolve :auto, nil, "latest", or a specific version string to a concrete DCC version. The :auto strategy reads schemaVersion from the XML body.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dcc/schema/version.rb', line 57

def resolve_dcc(version, xml: nil)
  v = normalize(version)
  return detect_from_xml(xml) if v.nil? || v == "auto"
  return DCC_LATEST if v == "latest"

  unless dcc?(v)
    raise ::Dcc::UnknownVersionError,
          "Unknown DCC schema version: #{version.inspect}"
  end

  v
end

.resolve_dsi(version, xml: nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dcc/schema/version.rb', line 70

def resolve_dsi(version, xml: nil)
  v = normalize(version)
  return DSI_LATEST if v.nil? || v == "auto" || v == "latest"

  unless dsi?(v)
    raise ::Dcc::UnknownVersionError,
          "Unknown D-SI schema version: #{version.inspect}"
  end

  v
end