Module: Goldendocx::HasAttributes

Extended by:
ActiveSupport::Concern
Included in:
Element
Defined in:
lib/goldendocx/has_attributes.rb

Instance Method Summary collapse

Instance Method Details

#assign_attributes(**attributes) ⇒ Object



63
64
65
# File 'lib/goldendocx/has_attributes.rb', line 63

def assign_attributes(**attributes)
  attributes.each { |key, value| send("#{key}=", value) if respond_to?("#{key}=") }
end

#attributesObject



41
42
43
44
45
46
47
48
49
# File 'lib/goldendocx/has_attributes.rb', line 41

def attributes
  self.class.attributes.each_with_object({}) do |(name, options), result|
    value = public_send(options[:method] || name) || options[:default]
    next if value.nil?

    key = [options[:namespace], options[:alias_name] || name].compact.join(':')
    result[key] = value
  end
end

#read_attributes(node) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/goldendocx/has_attributes.rb', line 51

def read_attributes(node)
  node_attributes = node.attributes_hash

  attributes = self.class.attributes.each_with_object({}) do |(name, options), result|
    attribute_tag = [options[:namespace], (options[:alias_name] || name)].compact.join(':')
    result[name] = node_attributes.delete(attribute_tag)
  end
  assign_attributes(**attributes)

  unparsed_attributes.update(node_attributes)
end