Class: Nokogiri::XML::Schematron::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/xml/schematron/base.rb

Overview

This class is abstract.

The base class for internal representations of Schematron types.

Direct Known Subclasses

Assertion, Namespace, Nodes::Base, Paragraph, Pattern, Rule, Schema

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, **options) {|base| ... } ⇒ Base

Create a new Base object.

Parameters:

Yield Parameters:

Yield Returns:

  • (void)


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/nokogiri/xml/schematron/base.rb', line 92

def initialize(parent, **options, &block)
  @parent = parent
  @children = []

  @options = options

  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
end

Instance Attribute Details

#childrenArray<Nokogiri::XML::Schematron::Base> (readonly)

Returns the children of the internal representation of the Schematron type.

Returns:



81
82
83
# File 'lib/nokogiri/xml/schematron/base.rb', line 81

def children
  @children
end

#optionsHash<Symbol, Object> (readonly)

Returns the options.

Returns:

  • (Hash<Symbol, Object>)

    the options.



84
85
86
# File 'lib/nokogiri/xml/schematron/base.rb', line 84

def options
  @options
end

#parentNokogiri::XML::Schematron::Base (readonly)

Returns the parent object.

Returns:



78
79
80
# File 'lib/nokogiri/xml/schematron/base.rb', line 78

def parent
  @parent
end

Class Method Details

.attribute(name, **options) ⇒ void

This method returns an undefined value.

Defines methods for a named XML attribute.

For example:

attribute :name

defines reader and writer methods that are equivalent to:

def name
  @options[:name]
end

def name=(value)
  @options[:name] = value
end

Parameters:

  • name (#to_sym)

    the method name.

  • options (Hash<Symbol, Object>)

    the options.

Options Hash (**options):

  • :reader (Boolean) — default: true

    define the reader method?

  • :writer (Boolean) — default: true

    define the writer method?



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nokogiri/xml/schematron/base.rb', line 31

def attribute(name, **options)
  unless options[:reader] == false
    define_method(name.to_sym) do
      instance_variable_get(:@options).send(:[], name.to_sym)
    end
  end

  unless options[:writer] == false
    define_method(:"#{name.to_sym}=") do |value|
      instance_variable_get(:@options).send(:[]=, name.to_sym, value)
    end
  end

  return
end

.element(name, klass) ⇒ void

This method returns an undefined value.

Defines methods for a named XML element.

For example:

element :name, DescendentOfBase

where

class DescendentOfBase < Base; end

is a descendent of this class, defines methods that are equivalent to:

def name(*args, **options, &block)
  base = DescendentOfBase.new(self, *args, **options, &block)
  @children << base
  base
end

Parameters:

  • name (#to_sym)

    the method name.

  • klass (Class)

    the class.



68
69
70
71
72
73
74
# File 'lib/nokogiri/xml/schematron/base.rb', line 68

def element(name, klass)
  define_method(name.to_sym) do |*args, **options, &block|
    base = klass.send(:new, self, *args, **options, &block)
    instance_variable_get(:@children) << base
    base
  end
end

Instance Method Details

#to_builder(*args, **options) ⇒ Nokogiri::XML::Builder

Convert this Base object to a Builder object.



110
111
112
113
114
# File 'lib/nokogiri/xml/schematron/base.rb', line 110

def to_builder(*args, **options)
  ::Nokogiri::XML::Builder.new(*args, **options) do |xml|
    build!(xml)
  end
end