Class: Spoonerize::Spoonerism

Inherits:
Object
  • Object
show all
Defined in:
lib/spoonerize/spoonerism.rb

Overview

The main word-flipper.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words) ⇒ Spoonerize::Spoonerism

Initialize instance.

Parameters:

  • words (Array)


19
20
21
# File 'lib/spoonerize/spoonerism.rb', line 19

def initialize(words)
  @words = words.map(&:downcase)
end

Instance Attribute Details

#wordsArray (readonly)

The words originally passed at initialization.

Returns:

  • (Array)


11
12
13
# File 'lib/spoonerize/spoonerism.rb', line 11

def words
  @words
end

Instance Method Details

#all_excluded_wordsArray

Array of words to exclude by combining two arrays:

  • Any user-passed words, stored in Spoonerize.config.excluded_words

  • Any lazy words, if lazy mode is true

Returns:

  • (Array)


80
81
82
83
84
# File 'lib/spoonerize/spoonerism.rb', line 80

def all_excluded_words
  (Spoonerize.config.excluded_words + (
    Spoonerize.config.lazy ? Spoonerize.config.lazy_words : []
  )).map(&:downcase)
end

#enough_flippable_words?Boolean

True if there are more than one non-excluded word to flip

Returns:

  • (Boolean)


62
63
64
# File 'lib/spoonerize/spoonerism.rb', line 62

def enough_flippable_words?
  (words - all_excluded_words).size > 1
end

#saveArray

Saves the flipped words to the log file, along with the options

Returns:

  • (Array)


70
71
72
# File 'lib/spoonerize/spoonerism.rb', line 70

def save
  log.write([words.join(" "), to_s, options.join(", ")])
end

#spoonerizeArray

Iterates through words array, and maps its elements to the output of flip_words.

Returns:

  • (Array)


28
29
30
31
32
# File 'lib/spoonerize/spoonerism.rb', line 28

def spoonerize
  raise "Not enough words to flip" unless enough_flippable_words?

  words.map.with_index { |word, idx| flip_words(word, idx) }
end

#to_hHash

Spoonerized results as a joined hash.

Returns:

  • (Hash)


46
47
48
# File 'lib/spoonerize/spoonerism.rb', line 46

def to_h
  words.zip(spoonerize).to_h
end

#to_jsonString

Same as to_h, but as json.

Returns:

  • (String)


54
55
56
# File 'lib/spoonerize/spoonerism.rb', line 54

def to_json
  to_h.to_json
end

#to_sString

Spoonerized results as a joined string.

Returns:

  • (String)


38
39
40
# File 'lib/spoonerize/spoonerism.rb', line 38

def to_s
  spoonerize.join(" ")
end