Class: Saxon::FeatureFlags::Version Private

Inherits:
Object
  • Object
show all
Defined in:
lib/saxon/feature_flags/version.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Restrict a specific method to only work with the specified Saxon version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constraint_string) ⇒ Version

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a version constraint check from the supplied constraint string

Parameters:

  • constraint_string (String)

    the version constraint



58
59
60
61
# File 'lib/saxon/feature_flags/version.rb', line 58

def initialize(constraint_string)
  @constraint_string = constraint_string
  @comparison_operator, @version_string = parse_version_constraint(constraint_string)
end

Instance Attribute Details

#comparison_operatorSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the extracted comparison operator.

Returns:

  • (Symbol)

    the extracted comparison operator



51
52
53
# File 'lib/saxon/feature_flags/version.rb', line 51

def comparison_operator
  @comparison_operator
end

#constraint_stringString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the complete constraint string.

Returns:

  • (String)

    the complete constraint string



49
50
51
# File 'lib/saxon/feature_flags/version.rb', line 49

def constraint_string
  @constraint_string
end

#version_stringString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the extracted version string.

Returns:

  • (String)

    the extracted version string



53
54
55
# File 'lib/saxon/feature_flags/version.rb', line 53

def version_string
  @version_string
end

Class Method Details

.create(klass, method_name, version_constraint) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Modify the method so that it will only run if the version constraint is satisfied.

We can’t know what version of Saxon is in use at code load time, only once Loader#load! has been called, either explicitly or by calling a method which requires a Saxon Java object. Therefore, we have to check this the first time someone calls the method.

Creating this check replaces the method on the target class with one that checks the version, and runs the original version if its constraint is satisfied.

To avoid performing the check every time the method is called, once we know whether or not the constraint is satisfied, we assume that the verion of Saxon cannot be unloaded and a new one loaded in its place and replace our version checking method with the original (if the constraint is passed), or with one that simply raises the constraint error.

Parameters:

  • klass (Class)

    the class the method lives on

  • method_name (Symbol)

    the name of the method to be constrained

  • version_constraint (String)

    the version constraint ('>9.9.1.7' or '>= 9.9')



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/saxon/feature_flags/version.rb', line 30

def self.create(klass, method_name, version_constraint)
  method = klass.instance_method(method_name)
  version_check = new(version_constraint)


  klass.send(:define_method, method_name, ->(*args) {
    if version_check.ok?
      klass.send(:define_method, method_name, method)
      method.bind(self).call(*args)
    else
      klass.send(:define_method, method_name, ->(*args) {
        raise UnavailableInThisSaxonVersionError
      })
      raise UnavailableInThisSaxonVersionError
    end
  })
end

Instance Method Details

#constraint_versionSaxon::Version::Library

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a Version::Library representing the version specified in the constraint that can be compared with the loaded Saxon version

Returns:



74
75
76
# File 'lib/saxon/feature_flags/version.rb', line 74

def constraint_version
  @constraint_version ||= Saxon::Version::Library.new(version_string, 'HE')
end

#ok?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reports if the version constraint is satisfied or not.

Returns:

  • (Boolean)

    true if the constraint is satisfied, false otherwise



66
67
68
# File 'lib/saxon/feature_flags/version.rb', line 66

def ok?
  Saxon::Version::Library.loaded_version.send(comparison_operator, constraint_version)
end