class AttributedString
Constants
- VERSION
Attributes
attributes[R]
string[R]
Public Class Methods
new(string, attributes = [])
click to toggle source
# File lib/attributed_string.rb, line 8 def initialize(string, attributes = []) @string = string @attributes = attributes end
Public Instance Methods
<<(attributed_string)
click to toggle source
# File lib/attributed_string.rb, line 13 def <<(attributed_string) @string << attributed_string.string @attributes += attributed_string.attributes end
fix()
click to toggle source
# File lib/attributed_string.rb, line 28 def fix # Group into identical data structures grouped_attributes = @attributes.group_by { |a| a.data }.values # Sort each group into order grouped_attributes.each do |group| group.sort_by! { |a| a.range.begin } end # Merge attributes in identical groups together merged_attributes = grouped_attributes.map do |group| group.inject([]) do |attrs, a| if !attrs.empty? && attributes_overlap?(attrs.last, a) attrs[0...-1] + [merge_attributes(attrs.last, a)] else attrs + [a] end end end @attributes = merged_attributes.flatten end
length()
click to toggle source
# File lib/attributed_string.rb, line 24 def length @string.length end
prepend(attributed_string)
click to toggle source
# File lib/attributed_string.rb, line 18 def prepend(attributed_string) @string.prepend(attributed_string.string) move_attributes(attributed_string.length) @attributes += attributed_string.attributes end
Private Instance Methods
attributes_overlap?(a, b)
click to toggle source
# File lib/attributed_string.rb, line 59 def attributes_overlap?(a, b) a.data == b.data && (a.range.include?(b.range.begin) || b.range.include?(a.range.begin)) end
merge_attributes(a, b)
click to toggle source
# File lib/attributed_string.rb, line 63 def merge_attributes(a, b) range = [a.range.begin, b.range.begin].min..[a.range.end, b.range.end].max AttributedString::Attribute.new(range, a.data) end
move_attributes(offset)
click to toggle source
# File lib/attributed_string.rb, line 53 def move_attributes(offset) @attributes.each do |attribute| attribute.move(offset) end end