Class: CleanCut

Inherits:
Object
  • Object
show all
Defined in:
lib/clean-cut.rb

Constant Summary collapse

DEFAULT_Options =
{
  :string_to_cut   => "",
  :character_limit => nil
}

Class Method Summary collapse

Class Method Details

.cut(user_options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/clean-cut.rb', line 9

def cut(user_options={})
  options = DEFAULT_Options.merge(user_options)

  @string_to_cut   = options[:string_to_cut]
  @character_limit = options[:character_limit]

  raise "character limit must be >= 0." if @character_limit == nil
  raise "string must have a length >= 1" if @string_to_cut.length < 1

  cut_index = find_clean(@string_to_cut, @character_limit)

  if cut_index == 0
    first_part = ""
    second_part = @string_to_cut[(cut_index..@string_to_cut.length - 1)]
  elsif cut_index > 0
    first_part = @string_to_cut[(0..cut_index)].chomp " "
    second_part = @string_to_cut[(cut_index + 1..@string_to_cut.length - 1)]
  end

  [first_part, second_part].map do |string|
    if string == nil
      string = ""
    else
      string = string
    end
  end
end