module XMLable::Mixins::AttributesStorage
AttributesStorage
module contains the logic for object that able to
store XML attributes
Public Class Methods
# File lib/xmlable/mixins/attributes_storage.rb, line 8 def self.included(base) base.send(:extend, ClassMethods) end
Public Instance Methods
Get attribute object by its key
@param [String] key attribute key
@return [XMLable::Mixins::Object, nil]
# File lib/xmlable/mixins/attributes_storage.rb, line 85 def [](key) h = __has_attribute_handler?(key) h ? __attribute_object_get(h) : super end
Set attribute value
@param [String] key attribute key @param [Object] val new value
@return [XMLable::Mixins::Object, nil]
# 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
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
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
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 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 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
Is this object empty?
@api private
@return [Hash(String => Array<XMLable::Mixins::Object>)]
# File lib/xmlable/mixins/attributes_storage.rb, line 53 def __empty? return false unless super __attributes.values.all?(&:__empty?) end
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 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
Does this object contain attribute with given key?
@return [XMLable::Mixins::Object, false]
# File lib/xmlable/mixins/attributes_storage.rb, line 74 def key?(key) super || __has_attribute_handler?(key) end
# 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