Class: SpacerLine
Instance Attribute Summary
Attributes inherited from BaseLine
#attribute_padding_by_name, #attribute_sort_order, #data
Instance Method Summary
collapse
Methods inherited from BaseLine
#sort_with
Constructor Details
#initialize(data, match) ⇒ SpacerLine
Returns a new instance of SpacerLine.
30
31
32
33
34
35
36
37
38
|
# File 'lib/spacify.rb', line 30
def initialize data, match
super data
@match = match
parser = HTMLNodeElementParser.new
all, @leading, code, @trailing = data.split /^(\s*)(#@match)(.*)$/
@parsed = parser.parse code
raise "Unable to parse '#{code}'" if @parsed.nil?
end
|
Instance Method Details
#attribute_sort_order=(order) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/spacify.rb', line 80
def attribute_sort_order= order
@attribute_sort_order = case order
when Hash; then order.keys
when Array
if order.first.is_a? Array
order.collect(&:first)
else
order
end
else
nil
end
end
|
#attributes ⇒ Object
40
41
42
|
# File 'lib/spacify.rb', line 40
def attributes
@parsed.attributes
end
|
#parse? ⇒ Boolean
44
45
46
|
# File 'lib/spacify.rb', line 44
def parse?
true
end
|
#to_s ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/spacify.rb', line 48
def to_s
s = ''
s << "#@leading"
s << '<'
s << @parsed.node_name
s << ' '
attribute_sort_order.each do |attribute_name|
s << whitespace_pad_value_at_key!( attribute_name )
end
s << '/' if @parsed.empty?
s << '>'
s << "#@trailing"
s << "\n"
s
end
|
#whitespace_pad_value_at_key!(key) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/spacify.rb', line 64
def whitespace_pad_value_at_key! key
longest = attribute_padding_by_name[key]
if self.attributes.has_key? key
attribute_value = self.attributes[key].to_s
padding_length = longest - attribute_value.size
else
attribute_value = ''
padding_length = longest
end
s = attribute_value
s << ' ' * padding_length
s
end
|