Module: Xommelier::Xml::Schema

Included in:
ClassMethods
Defined in:
lib/xommelier/xml/schema.rb

Instance Method Summary collapse

Instance Method Details

#schema(schema = nil) ⇒ Nokogiri::XML::Schema?

Parameters:

  • schema (String, Nokogiri::XML::Node) (defaults to: nil)

Returns:

  • (Nokogiri::XML::Schema, nil)


31
32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



13
14
15
16
17
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
# 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