Class: Coradoc::Element::AttributeList
- Inherits:
-
Base
- Object
- Base
- Coradoc::Element::AttributeList
show all
- Defined in:
- lib/coradoc/element/attribute_list.rb
Defined Under Namespace
Modules: Matchers
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Base
#children_accessors, children_accessors, declare_children, #simplify_block_content, visit, #visit
Constructor Details
#initialize(*positional, **named) ⇒ AttributeList
Returns a new instance of AttributeList.
8
9
10
11
12
13
|
# File 'lib/coradoc/element/attribute_list.rb', line 8
def initialize(*positional, **named)
@positional = positional || []
@named = named || {}
@rejected_positional = []
@rejected_named = []
end
|
Instance Attribute Details
#named ⇒ Object
Returns the value of attribute named.
4
5
6
|
# File 'lib/coradoc/element/attribute_list.rb', line 4
def named
@named
end
|
#positional ⇒ Object
Returns the value of attribute positional.
4
5
6
|
# File 'lib/coradoc/element/attribute_list.rb', line 4
def positional
@positional
end
|
#rejected_named ⇒ Object
Returns the value of attribute rejected_named.
4
5
6
|
# File 'lib/coradoc/element/attribute_list.rb', line 4
def rejected_named
@rejected_named
end
|
#rejected_positional ⇒ Object
Returns the value of attribute rejected_positional.
4
5
6
|
# File 'lib/coradoc/element/attribute_list.rb', line 4
def rejected_positional
@rejected_positional
end
|
Instance Method Details
#add_named(name, value) ⇒ Object
29
30
31
|
# File 'lib/coradoc/element/attribute_list.rb', line 29
def add_named(name, value)
@named[name] = value
end
|
#add_positional(*attr) ⇒ Object
25
26
27
|
# File 'lib/coradoc/element/attribute_list.rb', line 25
def add_positional(*attr)
@positional += attr
end
|
#any? ⇒ Boolean
33
34
35
|
# File 'lib/coradoc/element/attribute_list.rb', line 33
def any?
!empty?
end
|
#empty? ⇒ Boolean
37
38
39
|
# File 'lib/coradoc/element/attribute_list.rb', line 37
def empty?
@positional.empty? && @named.empty?
end
|
#inspect ⇒ Object
15
16
17
18
19
20
21
22
23
|
# File 'lib/coradoc/element/attribute_list.rb', line 15
def inspect
"AttributeList: " +
[
@positional.map(&:inspect).join(", "),
@named.map { |k, v| "#{k}: #{v.inspect}" }.join(", "),
(@rejected_positional.empty? or "rejected: #{@rejected_positional.inspect}"),
(@rejected_positional.empty? or "rejected: #{@rejected_named.inspect}"),
].reject { |i| i == true || i.empty? }.join(", ")
end
|
#to_adoc(show_empty = true) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/coradoc/element/attribute_list.rb', line 73
def to_adoc(show_empty = true)
return "[]" if [@positional, @named].all?(:empty?)
adoc = +""
if !@positional.empty?
adoc << @positional.map do |p|
[nil, ""].include?(p) ? '""' : p
end.join(",")
end
adoc << "," if @positional.any? && @named.any?
adoc << @named.map do |k, v|
if v.is_a?(String)
v = v.gsub("\"", "\\\"")
if v.include?(",") || v.include?('"')
v = "\"#{v}\""
end
elsif v.is_a?(Array)
v = "\"#{v.join(',')}\""
end
[k.to_s, "=", v].join
end.join(",")
if !empty? || (empty? && show_empty)
"[#{adoc}]"
elsif empty? && !show_empty
adoc
end
end
|
#validate_attr(attr, matcher) ⇒ Object
41
42
43
|
# File 'lib/coradoc/element/attribute_list.rb', line 41
def validate_attr(attr, matcher)
matcher === attr
end
|
#validate_named(validators) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/coradoc/element/attribute_list.rb', line 61
def validate_named(validators)
@named.each_with_index do |(name, value), i|
name = name.to_sym
validator = validators[name]
unless validator && validate_attr(value, validator)
@named.delete(name)
@rejected_named << [name, value]
end
end
end
|
#validate_positional(validators) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/coradoc/element/attribute_list.rb', line 45
def validate_positional(validators)
@positional.each_with_index do |value, i|
_positional_name = validators[i][0]
validator = validators[i][1]
unless validator && validate_attr(value, validator)
@positional[i] = nil
@rejected_positional << [i, value]
end
end
@positional.pop while !@positional.empty? && @positional.last.nil?
end
|