Module: XMLable::Mixins::ElementsStorage

Defined in:
lib/xmlable/mixins/elements_storage.rb

Overview

ElementsStorage module contains the logic to store XML elements

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blocks) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/xmlable/mixins/elements_storage.rb', line 68

def method_missing(name, *args, &blocks)
  h = __has_element_handler?(name)
  return super unless h

  if name.to_s.end_with?('=')
    __element_object_set(h, args.first)
  else
    __element_object_get(h)
  end
end

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/xmlable/mixins/elements_storage.rb', line 7

def self.included(base)
  base.send(:extend, ClassMethods)
end

Instance Method Details

#[](key) ⇒ XMLable::Mixins::Object?

Get element object by its key

Parameters:

  • key (String)

    element key

Returns:



106
107
108
109
# File 'lib/xmlable/mixins/elements_storage.rb', line 106

def [](key)
  h = __has_element_handler?(key)
  h ? __element_object_get(h) : super
end

#[]=(key, val) ⇒ XMLable::Mixins::Object?

Set element value

Parameters:

  • key (String)

    element key

  • val (Object)

    new value

Returns:



119
120
121
122
# File 'lib/xmlable/mixins/elements_storage.rb', line 119

def []=(key, val)
  h = __has_element_handler?(key)
  h ? __element_object_set(h, val) : super
end

#__element_object_get(h) ⇒ XMLable::Mixins::Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get element object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/xmlable/mixins/elements_storage.rb', line 147

def __element_object_get(h)
  unless __elements.key?(h.key)
    if h.single?
      __set_element(nil, tag: h.tag, namespace: h.namespace_prefix)
    else
      __initialize_elements_container(h)
    end
  end
  ret = __elements[h.key]
  h.single? ? ret.first : ret
end

#__element_object_initialize(h, val) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set element object value



181
182
183
184
185
186
187
188
189
190
# File 'lib/xmlable/mixins/elements_storage.rb', line 181

def __element_object_initialize(h, val)
  obj = __element_object_get(h)

  val = { '__content' => val } unless val.respond_to?(:each)
  if h.single?
    obj.__initialize_with(val)
  else
    val.map { |v| obj.new(v) }
  end
end

#__element_object_set(h, val) ⇒ XMLable::Mixins::Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set element object value



169
170
171
# File 'lib/xmlable/mixins/elements_storage.rb', line 169

def __element_object_set(h, val)
  __element_object_get(h).__overwrite_content(val)
end

#__elementsHash(String => Array<XMLable::Mixins::Object>)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Elements which current object holds

Returns:



52
53
54
# File 'lib/xmlable/mixins/elements_storage.rb', line 52

def __elements
  @__elements ||= {}
end

#__elements_handlersXMLable::Handlers::Storage

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Elements handlers storage



86
87
88
# File 'lib/xmlable/mixins/elements_storage.rb', line 86

def __elements_handlers
  @__elements_handler ||= self.class.__elements_handlers.clone
end

#__empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is this object empty?

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/xmlable/mixins/elements_storage.rb', line 63

def __empty?
  return false unless super
  __elements.values.all?(&:__empty?)
end

#__has_element_handler?(key) ⇒ XMLable::Handlers::Element, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find element handler by its key

Parameters:

  • key (Symbol, String)

Returns:



133
134
135
136
# File 'lib/xmlable/mixins/elements_storage.rb', line 133

def __has_element_handler?(key)
  key = key.to_s.gsub(/[=!]$/, '')
  __elements_handlers.storage.find { |h| h.method_name == key }
end

#__initialize_elements_container(h) ⇒ XMLable::Mixins::Container

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize elements container



41
42
43
# File 'lib/xmlable/mixins/elements_storage.rb', line 41

def __initialize_elements_container(h)
  __elements[h.key] ||= h.container_for_xml_element(__node)
end

#__set_element(el, opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set element from XML Node

Parameters:

  • name (Symbol)

    tag name

  • el (Nokogiri::XML::Element, nil)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/xmlable/mixins/elements_storage.rb', line 19

def __set_element(el, opts = {})
  unless el.is_a?(Nokogiri::XML::Element)
    el = Nokogiri::XML::Element.new(opts[:tag], @__node)
    @__node << el
    if opts[:namespace]
      el.namespace = el.namespace_scopes.find { |n| n.prefix == opts[:namespace] }
    end
  end
  h = __elements_handlers.for_xml_object(el)
  el.instance_variable_set(:@__handler, h)
  __initialize_elements_container(h) << h.from_xml_element(el)
end

#key?(key) ⇒ XMLable::Mixins::Object, false

Does this object contain element with given key?

Returns:



95
96
97
# File 'lib/xmlable/mixins/elements_storage.rb', line 95

def key?(key)
  super || __has_element_handler?(key)
end