Class: Coradoc::Element::AttributeList
- Inherits:
-
Object
- Object
- Coradoc::Element::AttributeList
- Defined in:
- lib/coradoc/element/attribute_list.rb
Instance Attribute Summary collapse
-
#named ⇒ Object
readonly
Returns the value of attribute named.
-
#positional ⇒ Object
readonly
Returns the value of attribute positional.
Instance Method Summary collapse
- #add_named(name, value) ⇒ Object
- #add_positional(attr) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(*positional, **named) ⇒ AttributeList
constructor
A new instance of AttributeList.
- #to_adoc ⇒ Object
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
#named ⇒ Object (readonly)
Returns the value of attribute named.
4 5 6 |
# File 'lib/coradoc/element/attribute_list.rb', line 4 def named @named end |
#positional ⇒ Object (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
19 20 21 |
# File 'lib/coradoc/element/attribute_list.rb', line 19 def empty? @positional.empty? && @named.empty? end |
#to_adoc ⇒ Object
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 |