Class: Passweird::Substringer

Inherits:
Object
  • Object
show all
Defined in:
lib/passweird/substringer.rb

Overview

The Substringer class is responsible for generating all unique substrings of a given root string with a specified minimum length.

Example usage:

substringer = Passweird::Substringer.new("example")
substrings = substringer.substrings(min_length: 3)
# => ["exa", "exam", "examp", "exampl", "example", "xam", "xamp", "xampl", "xample", ...]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_string) ⇒ Substringer

Returns a new instance of Substringer.

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/passweird/substringer.rb', line 18

def initialize(root_string)
  raise ArgumentError, "root_string must be a String" unless root_string.is_a?(String)

  @root_string = root_string
end

Instance Attribute Details

#root_stringObject (readonly)

Returns the value of attribute root_string.



12
13
14
# File 'lib/passweird/substringer.rb', line 12

def root_string
  @root_string
end

Class Method Details

.substrings(root_string, min_length: 3) ⇒ Object



14
15
16
# File 'lib/passweird/substringer.rb', line 14

def self.substrings(root_string, min_length: 3)
  new(root_string).substrings(min_length: min_length)
end

Instance Method Details

#substrings(min_length: 3) ⇒ Array<String>

Generates all unique substrings of the root_string with a minimum length

Parameters:

  • min_length (Integer) (defaults to: 3)

    the minimum length of the substrings

Returns:

  • (Array<String>)

    an array of unique substrings

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
# File 'lib/passweird/substringer.rb', line 28

def substrings(min_length: 3)
  raise ArgumentError, "min_length must be an Integer" unless min_length.is_a?(Integer)
  raise ArgumentError, "min_length must be greater than 0" if min_length <= 0

  return [root_string] if root_string.length < min_length

  ([root_string] + get_substrings(min_length)).uniq
end