Module: Cardname::Manipulate
- Included in:
- Cardname
- Defined in:
- lib/cardname/manipulate.rb
Instance Method Summary collapse
- #num_parts ⇒ Object
- #prepend_joint ⇒ Object (also: #to_field)
- #sub_in(str, with:) ⇒ Object
-
#swap(old, new) ⇒ Object
swap a subname keys are used for comparison.
- #swap_part(oldpart, newpart) ⇒ Object
- #swap_piece(oldpiece, newpiece) ⇒ Object
Instance Method Details
#num_parts ⇒ Object
35 36 37 |
# File 'lib/cardname/manipulate.rb', line 35 def num_parts parts.length end |
#prepend_joint ⇒ Object Also known as: to_field
39 40 41 42 |
# File 'lib/cardname/manipulate.rb', line 39 def prepend_joint joint = self.class.joint self =~ /^#{Regexp.escape joint}/ ? self : (joint + self) end |
#sub_in(str, with:) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/cardname/manipulate.rb', line 44 def sub_in str, with: %i[capitalize downcase].product(%i[pluralize singularize]) .inject(str) do |s, (m1, m2)| s.gsub(/\b#{send(m1).send(m2)}\b/, with.send(m1).send(m2)) end end |
#swap(old, new) ⇒ Object
swap a subname keys are used for comparison
5 6 7 8 9 10 11 12 |
# File 'lib/cardname/manipulate.rb', line 5 def swap old, new old_name = old.to_name new_name = new.to_name return self if old_name.num_parts > num_parts return swap_part(old_name, new_name) if old_name.simple? return self unless include? old_name swap_all_subsequences(old_name, new_name).to_name end |
#swap_part(oldpart, newpart) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cardname/manipulate.rb', line 14 def swap_part oldpart, newpart ensure_simpleness oldpart, "Use 'swap' to swap junctions" oldpart = oldpart.to_name newpart = newpart.to_name parts.map do |p| oldpart == p ? newpart : p end.to_name end |
#swap_piece(oldpiece, newpiece) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/cardname/manipulate.rb', line 25 def swap_piece oldpiece, newpiece oldpiece = oldpiece.to_name newpiece = newpiece.to_name return swap_part oldpiece, newpiece if oldpiece.simple? return self unless starts_with_parts?(oldpiece) return newpiece if oldpiece.num_parts == num_parts self.class.new [newpiece, self[oldpiece.num_parts..-1]].flatten end |