Module: Cardname::Manipulate
- Included in:
- Cardname
- Defined in:
- lib/cardname/manipulate.rb
Instance Method Summary collapse
-
#replace(old, new) ⇒ Object
replace a subname keys are used for comparison.
- #replace_part(oldpart, newpart) ⇒ Object
- #replace_piece(oldpiece, newpiece) ⇒ Object
Instance Method Details
#replace(old, new) ⇒ Object
replace a subname keys are used for comparison
5 6 7 8 9 10 11 12 |
# File 'lib/cardname/manipulate.rb', line 5 def replace old, new old_name = old.to_name new_name = new.to_name return self if old_name.length > length return replace_part(old_name, new_name) if old_name.simple? return self unless include? old_name replace_all_subsequences(old_name, new_name).to_name end |
#replace_part(oldpart, newpart) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cardname/manipulate.rb', line 14 def replace_part oldpart, newpart ensure_simpliness oldpart, "Use 'replace' to replace junctions" oldpart = oldpart.to_name newpart = newpart.to_name parts.map do |p| oldpart == p ? newpart : p end.to_name end |
#replace_piece(oldpiece, newpiece) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/cardname/manipulate.rb', line 25 def replace_piece oldpiece, newpiece oldpiece = oldpiece.to_name newpiece = newpiece.to_name return replace_part oldpiece, newpiece if oldpiece.simple? return self unless self.starts_with?(oldpiece) return newpiece if oldpiece.length == length newpiece + self[oldpiece.length..-1] end |