Class: XMLable::Handlers::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlable/handlers/storage.rb

Overview

Storage stores handlers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, storage = []) ⇒ Storage

Returns a new instance of Storage.

Parameters:

  • opts (Hash) (defaults to: {})

    additional options

  • storage (Array) (defaults to: [])

    initial handlers to store

Options Hash (opts):



15
16
17
18
# File 'lib/xmlable/handlers/storage.rb', line 15

def initialize(opts = {}, storage = [])
  @opts = opts
  @storage = storage
end

Instance Attribute Details

#storageArray<XMLable::Handlers::Base> (readonly)

Returns:



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

Parameters:

  • handler (XMLable::Handler::Base)


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

#cloneXMLable::Handers::Storage

Clone handlers storage

Returns:

  • (XMLable::Handers::Storage)


83
84
85
# File 'lib/xmlable/handlers/storage.rb', line 83

def clone
  self.class.new(@opts, storage.clone)
end

#default_handlerXMLable::Handlers::Base

Default handler class

Returns:



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

Parameters:

  • node (XML::Nokogiri::Node)

Returns:



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

Parameters:

  • tag (String)

    element tag name

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :namespace (String, nil)

    element namespace

Returns:



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

Parameters:

  • tag (String)

    element tag name

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :namespace (String, nil)

    element namespace

Returns:



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

#inspectString

Returns:

  • (String)


99
100
101
# File 'lib/xmlable/handlers/storage.rb', line 99

def inspect
  "#<XMLable::Handlers::Storage #{@storage.map(&:key).join(', ')} >"
end