Module: XMLable::Mixins::AttributesStorage

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

Overview

AttributesStorage module contains the logic for object that able to

store XML attributes

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, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# 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

Class Method Details

.included(base) ⇒ Object



8
9
10
# File 'lib/xmlable/mixins/attributes_storage.rb', line 8

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

Instance Method Details

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

Get attribute object by its key

Parameters:

  • key (String)

    attribute key

Returns:



85
86
87
88
# 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) ⇒ XMLable::Mixins::Object?

Set attribute value

Parameters:

  • key (String)

    attribute key

  • val (Object)

    new value

Returns:



98
99
100
101
# 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) ⇒ 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 attribute object



126
127
128
129
130
131
# 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) ⇒ 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.

Initialize attribute object with value



153
154
155
# 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) ⇒ 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 attribute object value

Parameters:



141
142
143
# File 'lib/xmlable/mixins/attributes_storage.rb', line 141

def __attribute_object_set(h, val)
  __attribute_object_get(h).__overwrite_value(val)
end

#__attributesHash(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.

Attributes which current object holds

Returns:



42
43
44
# File 'lib/xmlable/mixins/attributes_storage.rb', line 42

def __attributes
  @__attributes ||= {}
end

#__attributes_handlersXMLable::Handlers::Storage

Attributes handlers storage



162
163
164
# File 'lib/xmlable/mixins/attributes_storage.rb', line 162

def __attributes_handlers
  @__attributes_handler ||= self.class.__attributes_handlers.clone
end

#__empty?Hash(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.

Is this object empty?

Returns:



53
54
55
56
# File 'lib/xmlable/mixins/attributes_storage.rb', line 53

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

#__has_attribute_handler?(key) ⇒ XMLable::Handlers::Attribute, ...

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 attribute handler by its key

Parameters:

  • key (Symbol, String)

Returns:



112
113
114
115
# 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 = {}) ⇒ 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 XML attribute

Parameters:

  • att (Nokogiri::XML::Attr, nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :tag (String)

    attribute’s name

  • :namespace (String)

    attribute’s namespace



22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ XMLable::Mixins::Object, false

Does this object contain attribute with given key?

Returns:



74
75
76
# File 'lib/xmlable/mixins/attributes_storage.rb', line 74

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