module Xommelier::Xml::Schema

Public Instance Methods

schema(schema = nil) click to toggle source

@param [String, Nokogiri::XML::Node] schema @return [Nokogiri::XML::Schema, nil]

# File lib/xommelier/xml/schema.rb, line 31
def schema(schema = nil)
  self.schema = schema if schema

  unless instance_variable_defined?(:@_schema)
    # Unless schema exists, try to autoload schema
    if _default_schema_location
      self.schema_location = _default_schema_location
    else
      @_schema = nil
    end
  end
  @_schema
end
schema=(schema) click to toggle source
# File lib/xommelier/xml/schema.rb, line 45
def schema=(schema)
  if schema
    # If schema or schema path provided, set schema
    if schema.is_a?(Nokogiri::XML::Schema)
      @_schema = schema
    elsif schema.is_a?(Nokogiri::XML::Node)
      @_schema = Nokogiri::XML::Schema(schema)
    else
      self.schema_location = schema
    end
  end
  @_schema
end
schema_location(new_location = nil) click to toggle source
# File lib/xommelier/xml/schema.rb, line 13
def schema_location(new_location = nil)
  self.schema_location = new_location if new_location

  @_schema_location
end
schema_location=(location) click to toggle source
# File lib/xommelier/xml/schema.rb, line 19
def schema_location=(location)
  return unless location
  @_schema_location = location
  # For loading schema containing imports we need to temporarily chdir,
  #   so relative file names will be properly discovered
  Dir.chdir(File.dirname(location)) do
    @_schema = Nokogiri::XML::Schema(File.read(File.basename(location)))
  end
end

Protected Instance Methods

_available_schemas() click to toggle source
# File lib/xommelier/xml/schema.rb, line 61
def _available_schemas
  @_available_schemas ||= $LOAD_PATH.map do |path|
    Dir[File.join(path, 'xommelier/schemas', '*.xsd')]
  end.flatten.uniq
end
_default_schema_location() click to toggle source
# File lib/xommelier/xml/schema.rb, line 67
def _default_schema_location
  @_default_schema_location ||= begin
    file_name = /#{xmlns.as || name.demodulize.underscore}\.xsd\Z/
    _available_schemas.find { |path| path =~ file_name }
  end
end