Module: Cardname::Manipulate

Included in:
Cardname
Defined in:
cardname/lib/cardname/manipulate.rb

Instance Method Summary collapse

Instance Method Details

#num_partsObject



37
38
39
# File 'cardname/lib/cardname/manipulate.rb', line 37

def num_parts
  parts.length
end

#prepend_jointObject Also known as: to_field



41
42
43
44
# File 'cardname/lib/cardname/manipulate.rb', line 41

def prepend_joint
  joint = self.class.joint
  self =~ /^#{Regexp.escape joint}/ ? self : (joint + self)
end

#sub_in(str, with:) ⇒ Object



46
47
48
49
50
51
# File 'cardname/lib/cardname/manipulate.rb', line 46

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
13
# File 'cardname/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



15
16
17
18
19
20
21
22
23
24
# File 'cardname/lib/cardname/manipulate.rb', line 15

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



26
27
28
29
30
31
32
33
34
35
# File 'cardname/lib/cardname/manipulate.rb', line 26

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