Module: Tilia::Xml::ContextStackTrait
Overview
Context Stack
The Context maintains information about a document during either reading or writing.
During this process, it may be neccesary to override this context information.
This trait allows easy access to the context, and allows the end-user to override its settings for document fragments, and easily restore it again later.
Instance Attribute Summary collapse
-
#class_map ⇒ Hash
This is a list of custom serializers for specific classes.
-
#context_uri ⇒ String
A context_uri pointing to the document being parsed / written.
-
#element_map ⇒ Hash
This is the element map.
-
#namespace_map ⇒ Hash
This is a list of namespaces that you want to give default prefixes.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Object
Initializes instance variables.
-
#pop_context ⇒ void
Restore the previous “context”.
-
#push_context ⇒ void
Create a new “context”.
Instance Attribute Details
#class_map ⇒ Hash
This is a list of custom serializers for specific classes.
The writer may use this if you attempt to serialize an object with a class that does not implement XmlSerializable.
Instead it will look at this classmap to see if there is a custom serializer here. This is useful if you don’t want your value objects to be responsible for serializing themselves.
The keys in this classmap need to be fully qualified PHP class names, the values must be callbacks. The callbacks take two arguments. The writer class, and the value that must be written.
function (Writer writer, object value)
61 62 63 |
# File 'lib/tilia/xml/context_stack_trait.rb', line 61 def class_map @class_map end |
#context_uri ⇒ String
A context_uri pointing to the document being parsed / written. This uri may be used to resolve relative urls that may appear in the document.
The reader and writer don’t use this property, but as it’s an extremely common use-case for parsing XML documents, it’s added here as a convenience.
35 36 37 |
# File 'lib/tilia/xml/context_stack_trait.rb', line 35 def context_uri @context_uri end |
#element_map ⇒ Hash
This is the element map. It contains a list of XML elements (in clark notation) as keys and PHP class names as values.
The PHP class names must implement SabreXmlElement.
Values may also be a callable. In that case the function will be called directly.
24 25 26 |
# File 'lib/tilia/xml/context_stack_trait.rb', line 24 def element_map @element_map end |
#namespace_map ⇒ Hash
This is a list of namespaces that you want to give default prefixes.
You must make sure you create this entire list before starting to write. They should be registered on the root element.
43 44 45 |
# File 'lib/tilia/xml/context_stack_trait.rb', line 43 def namespace_map @namespace_map end |
Instance Method Details
#initialize(*args) ⇒ Object
Initializes instance variables
92 93 94 95 96 97 98 99 100 |
# File 'lib/tilia/xml/context_stack_trait.rb', line 92 def initialize(*args) @element_map = {} @namespace_map = {} @context_uri = '' @context_stack = [] @class_map = {} super end |
#pop_context ⇒ void
This method returns an undefined value.
Restore the previous “context”.
82 83 84 85 86 87 88 89 |
# File 'lib/tilia/xml/context_stack_trait.rb', line 82 def pop_context ( @element_map, @context_uri, @namespace_map, @class_map ) = @context_stack.pop end |
#push_context ⇒ void
This method returns an undefined value.
Create a new “context”.
This allows you to safely modify the element_map, context_uri or namespace_map. After you’re done, you can restore the old data again with popContext.
70 71 72 73 74 75 76 77 |
# File 'lib/tilia/xml/context_stack_trait.rb', line 70 def push_context @context_stack << [ @element_map.deep_dup, @context_uri.deep_dup, @namespace_map.deep_dup, @class_map.deep_dup ] end |