module SmartName::Manipulate
Public Instance Methods
replace(old, new)
click to toggle source
replace a subname keys are used for comparison
# File lib/smart_name/manipulate.rb 5 def replace old, new 6 old_name = old.to_name 7 new_name = new.to_name 8 return self if old_name.length > length 9 return replace_part(old_name, new_name) if old_name.simple? 10 return self unless include? old_name 11 replace_all_subsequences(old_name, new_name).to_name 12 end
replace_part(oldpart, newpart)
click to toggle source
# File lib/smart_name/manipulate.rb 14 def replace_part oldpart, newpart 15 ensure_simpliness oldpart, "Use 'replace' to replace junctions" 16 17 oldpart = oldpart.to_name 18 newpart = newpart.to_name 19 20 parts.map do |p| 21 oldpart == p ? newpart : p 22 end.to_name 23 end
replace_piece(oldpiece, newpiece)
click to toggle source
# File lib/smart_name/manipulate.rb 25 def replace_piece oldpiece, newpiece 26 oldpiece = oldpiece.to_name 27 newpiece = newpiece.to_name 28 29 return replace_part oldpiece, newpiece if oldpiece.simple? 30 return self unless self.starts_with?(oldpiece) 31 return newpiece if oldpiece.length == length 32 newpiece + self[oldpiece.length..-1] 33 end
Private Instance Methods
ensure_simpliness(part, msg=nil)
click to toggle source
# File lib/smart_name/manipulate.rb 55 def ensure_simpliness part, msg=nil 56 return if part.to_name.simple? 57 raise StandardError, "'#{part}' has to be simple. #{msg}" 58 end
replace_all_subsequences(oldseq, newseq)
click to toggle source
# File lib/smart_name/manipulate.rb 37 def replace_all_subsequences oldseq, newseq 38 res = [] 39 i = 0 40 while i <= length - oldseq.length 41 # for performance reasons: check first character first then the rest 42 if oldseq.part_keys.first == part_keys[i] && 43 oldseq.part_keys == part_keys[i, oldseq.length] 44 res += newseq.parts 45 i += oldseq.length 46 else 47 res << parts[i] 48 i += 1 49 end 50 end 51 res += parts[i..-1] if i < length 52 res 53 end