module XMLable::Mixins::AttributesStorage

AttributesStorage module contains the logic for object that able to

store XML attributes

Public Class Methods

included(base) click to toggle source
# File lib/xmlable/mixins/attributes_storage.rb, line 8
def self.included(base)
  base.send(:extend, ClassMethods)
end

Public Instance Methods

[](key) click to toggle source

Get attribute object by its key

@param [String] key attribute key

@return [XMLable::Mixins::Object, nil]

Calls superclass method
# File lib/xmlable/mixins/attributes_storage.rb, line 85
def [](key)
  h = __has_attribute_handler?(key)
  h ? __attribute_object_get(h) : super
end
[]=(key, val) click to toggle source

Set attribute value

@param [String] key attribute key @param [Object] val new value

@return [XMLable::Mixins::Object, nil]

Calls superclass method
# File lib/xmlable/mixins/attributes_storage.rb, line 98
def []=(key, val)
  h = __has_attribute_handler?(key)
  h ? __attribute_object_set(h, val) : super
end
__attribute_object_get(h) click to toggle source

Get attribute object

@param [XMLable::Handlers::Attribute, XMLable::Handlers::AttributeNone] h

@api private

@return [XMLable::Mixins::Object]

# File lib/xmlable/mixins/attributes_storage.rb, line 126
def __attribute_object_get(h)
  unless __attributes.key?(h.key)
    __set_attribute(nil, tag: h.tag, namespace: h.namespace_prefix)
  end
  __attributes[h.key]
end
__attribute_object_initialize(h, value) click to toggle source

Initialize attribute object with value

@param [XMLable::Handlers::Attribute, XMLable::Handlers::AttributeNone, nil] h @param [Object] val

@api private

# File lib/xmlable/mixins/attributes_storage.rb, line 153
def __attribute_object_initialize(h, value)
  __attribute_object_get(h).__overwrite_value(value)
end
__attribute_object_set(h, val) click to toggle source

Set attribute object value

@param [XMLable::Handlers::Attribute, XMLable::Handlers::AttributeNone, nil] h @param [Object] val new value

@api private

# File lib/xmlable/mixins/attributes_storage.rb, line 141
def __attribute_object_set(h, val)
  __attribute_object_get(h).__overwrite_value(val)
end
__attributes() click to toggle source

Attributes which current object holds

@api private

@return [Hash(String => Array<XMLable::Mixins::Object>)]

# File lib/xmlable/mixins/attributes_storage.rb, line 42
def __attributes
  @__attributes ||= {}
end
__attributes_handlers() click to toggle source

Attributes handlers storage

@return [XMLable::Handlers::Storage]

# File lib/xmlable/mixins/attributes_storage.rb, line 162
def __attributes_handlers
  @__attributes_handler ||= self.class.__attributes_handlers.clone
end
__empty?() click to toggle source

Is this object empty?

@api private

@return [Hash(String => Array<XMLable::Mixins::Object>)]

Calls superclass method
# File lib/xmlable/mixins/attributes_storage.rb, line 53
def __empty?
  return false unless super
  __attributes.values.all?(&:__empty?)
end
__has_attribute_handler?(key) click to toggle source

Find attribute handler by its key

@param [Symbol, String] key

@api private

@return [XMLable::Handlers::Attribute, XMLable::Handlers::AttributeNone, nil]

# File lib/xmlable/mixins/attributes_storage.rb, line 112
def __has_attribute_handler?(key)
  key = key.to_s.gsub(/[=!]$/, '')
  __attributes_handlers.storage.find { |h| h.method_name == key }
end
__set_attribute(att, opts = {}) click to toggle source

Set XML attribute

@param [Nokogiri::XML::Attr, nil] att @param [Hash] opts @option opts [String] :tag attribute's name @option opts [String] :namespace attribute's namespace

@api private

# File lib/xmlable/mixins/attributes_storage.rb, line 22
def __set_attribute(att, opts = {})
  unless att
    @__node[opts[:tag]] = att
    att = @__node.attributes[opts[:tag]]
    if opts[:namespace]
      att.namespace = att.namespace_scopes.find { |n| n.prefix == opts[:namespace] }
    end
  end
  h = __attributes_handlers.for_xml_object(att)
  att.instance_variable_set(:@__handler, h)
  __attributes[h.key] = h.from_xml_attribute(att)
end
key?(key) click to toggle source

Does this object contain attribute with given key?

@return [XMLable::Mixins::Object, false]

Calls superclass method
# File lib/xmlable/mixins/attributes_storage.rb, line 74
def key?(key)
  super || __has_attribute_handler?(key)
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/xmlable/mixins/attributes_storage.rb, line 58
def method_missing(name, *args, &block)
  h = __has_attribute_handler?(name)
  return super unless h

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