Class: Mocktail::CreatesIdentifier

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mocktail/share/creates_identifier.rb,
lib/mocktail/sorbet/mocktail/share/creates_identifier.rb

Constant Summary collapse

KEYWORDS =
T.let(%w[__FILE__ __LINE__ alias and begin BEGIN break case class def defined? do else elsif end END ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield], T::Array[String])

Instance Method Summary collapse

Instance Method Details

#create(s, default: "identifier", max_length: 24) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mocktail/share/creates_identifier.rb', line 7

def create(s, default: "identifier", max_length: 24)
  case s
  when Kernel
    id = (s.to_s.downcase
      .gsub(/:0x[0-9a-f]+/, "") # Lazy attempt to wipe any Object:0x802beef identifiers
      .gsub(/[^\w\s]/, "")
      .gsub(/^\d+/, "")[0...max_length] || "")
      .strip
      .gsub(/\s+/, "_") # snake_case

    if id.empty?
      default
    else
      unreserved(id, default)
    end
  else
    default
  end
end