Module: Cardname::Manipulate

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

Overview

methods for altering name

Instance Method Summary collapse

Instance Method Details

#<<(val) ⇒ Cardname

append part to cardname

Returns:



14
15
16
# File 'cardname/lib/cardname/manipulate.rb', line 14

def << val
  replace self.class.new(parts << val)
end

#[]=(index, val) ⇒ Cardname

alter cardname based on card index

Returns:



6
7
8
9
10
# File 'cardname/lib/cardname/manipulate.rb', line 6

def []= index, val
  p = parts
  p[index] = val
  replace self.class.new(p)
end

#prepend_jointString Also known as: to_field

add a joint to name’s beginning (if it doesn’t already start with one)

Returns:

  • (String)


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

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

#sub_in(str, with:) ⇒ String

substitute name, where it appears in str, with new string

Returns:

  • (String)


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

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) ⇒ Cardname

swap one name for another (keys are used for comparison)

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'cardname/lib/cardname/manipulate.rb', line 20

def swap old, new
  old_name = old.to_name
  new_name = new.to_name

  if old_name.num_parts > num_parts
    self
  elsif old_name.simple?
    swap_part old_name, new_name
  elsif include? old_name
    swap_all_subsequences(old_name, new_name).to_name
  else
    self
  end
end