Class: Coradoc::Element::AttributeList

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/element/attribute_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*positional, **named) ⇒ AttributeList

Returns a new instance of AttributeList.



6
7
8
9
# File 'lib/coradoc/element/attribute_list.rb', line 6

def initialize(*positional, **named)
  @positional = positional || []
  @named = named || {}
end

Instance Attribute Details

#namedObject (readonly)

Returns the value of attribute named.



4
5
6
# File 'lib/coradoc/element/attribute_list.rb', line 4

def named
  @named
end

#positionalObject (readonly)

Returns the value of attribute positional.



4
5
6
# File 'lib/coradoc/element/attribute_list.rb', line 4

def positional
  @positional
end

Instance Method Details

#add_named(name, value) ⇒ Object



15
16
17
# File 'lib/coradoc/element/attribute_list.rb', line 15

def add_named(name, value)
  @named[name] = value
end

#add_positional(attr) ⇒ Object



11
12
13
# File 'lib/coradoc/element/attribute_list.rb', line 11

def add_positional(attr)
  @positional << attr
end

#empty?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/coradoc/element/attribute_list.rb', line 19

def empty?
  @positional.empty? && @named.empty?
end

#to_adocObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/coradoc/element/attribute_list.rb', line 23

def to_adoc
  return "[]" if [@positional, @named].all?(:empty?)

  adoc = ""
  adoc << @positional.join(",") if @positional.any?
  adoc << "," if @positional.any? && @named.any?
  adoc << @named.map do |k, v|
    if v.is_a?(String)
      v2 = v.to_s
      v2 = v2.include?("\"") ? v2.gsub("\"","\\\"") : v2
      if v2.include?(" ") || v2.include?(",") || v2.include?("\"")
        v2 = "\"#{v2}\""
      end
    elsif v.is_a?(Array)
      v2 = "\"#{v.join(",")}\""
    end
    [k.to_s, "=", v2].join
  end.join(",")
  adoc = "[#{adoc}]" if @positional.any? || @named.any?
  adoc
end