Class: XMLable::Handlers::Storage
- Inherits:
-
Object
- Object
- XMLable::Handlers::Storage
- Defined in:
- lib/xmlable/handlers/storage.rb
Overview
Storage stores handlers
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#<<(handler) ⇒ Object
Append new handler to storage.
-
#clone ⇒ XMLable::Handers::Storage
Clone handlers storage.
-
#default_handler ⇒ XMLable::Handlers::Base
Default handler class.
-
#for_xml_object(node) ⇒ XMLable::Handlers::Base
Find or create handler for the XML node.
-
#handler_with_tag(tag, opts = {}) ⇒ XMLable::Handlers::Base?
Find handler with given tag.
-
#handler_with_tag!(tag, opts = {}) ⇒ XMLable::Handlers::Base
Find handler with given tag or create a new one.
-
#initialize(opts = {}, storage = []) ⇒ Storage
constructor
A new instance of Storage.
- #inspect ⇒ String
Constructor Details
#initialize(opts = {}, storage = []) ⇒ Storage
Returns a new instance of Storage.
15 16 17 18 |
# File 'lib/xmlable/handlers/storage.rb', line 15 def initialize(opts = {}, storage = []) @opts = opts @storage = storage end |
Instance Attribute Details
#storage ⇒ Array<XMLable::Handlers::Base> (readonly)
8 9 10 |
# File 'lib/xmlable/handlers/storage.rb', line 8 def storage @storage end |
Instance Method Details
#<<(handler) ⇒ Object
Note:
Described handler is always on top of the list
Append new handler to storage
41 42 43 44 |
# File 'lib/xmlable/handlers/storage.rb', line 41 def <<(handler) @storage << handler @storage = @storage.sort_by { |h| !h.described?.to_s } end |
#clone ⇒ XMLable::Handers::Storage
Clone handlers storage
83 84 85 |
# File 'lib/xmlable/handlers/storage.rb', line 83 def clone self.class.new(@opts, storage.clone) end |
#default_handler ⇒ XMLable::Handlers::Base
Default handler class
92 93 94 |
# File 'lib/xmlable/handlers/storage.rb', line 92 def default_handler @opts[:default] end |
#for_xml_object(node) ⇒ XMLable::Handlers::Base
Find or create handler for the XML node
27 28 29 30 31 32 |
# File 'lib/xmlable/handlers/storage.rb', line 27 def for_xml_object(node) tag = node.name.to_s namespace = node.namespace.prefix if node.namespace namespace = namespace.to_s if namespace handler_with_tag!(tag, namespace: namespace) end |
#handler_with_tag(tag, opts = {}) ⇒ XMLable::Handlers::Base?
Find handler with given tag
56 57 58 59 60 61 |
# File 'lib/xmlable/handlers/storage.rb', line 56 def handler_with_tag(tag, opts = {}) match = @storage.find { |h| h.tag == tag } return match if !match || !opts.key?(:namespace) return match if opts[:namespace] == false match.namespace_prefix == opts[:namespace] ? match : nil end |
#handler_with_tag!(tag, opts = {}) ⇒ XMLable::Handlers::Base
Find handler with given tag or create a new one
72 73 74 75 76 |
# File 'lib/xmlable/handlers/storage.rb', line 72 def handler_with_tag!(tag, opts = {}) match = handler_with_tag(tag, opts) return match if match default_handler.build(tag, opts).tap { |h| self << h } end |
#inspect ⇒ String
99 100 101 |
# File 'lib/xmlable/handlers/storage.rb', line 99 def inspect "#<XMLable::Handlers::Storage #{@storage.map(&:key).join(', ')} >" end |