Class: Spoonerize::Spoonerism

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

Overview

The main word-flipper.

Constant Summary collapse

VOWEL_LETTERS =
"aeio"
CONSONANT_LETTERS =
"bcdfghjklmnprstvwxz"
Y_FOLLOWING_CONSONANTS =
"bcdfghjklmnpqrstvwxz"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*words, config: Spoonerize.config, lazy: nil, lazy_words: nil, excluded_words: nil, consonants_only: nil, reverse: nil, logfile_name: nil) ⇒ Spoonerize::Spoonerism

Initialize instance.

Parameters:

  • words (Array<String>)

    Words to spoonerize. Passing a single array is deprecated and will be removed in Spoonerize 1.0.

  • config (Spoonerize::Config) (defaults to: Spoonerize.config)

    Base config to copy.

  • lazy (Boolean, nil) (defaults to: nil)

    Override lazy mode for this instance.

  • lazy_words (Array<String>, nil) (defaults to: nil)

    Override lazy words for this instance.

  • excluded_words (Array<String>, nil) (defaults to: nil)

    Override excluded words for this instance.

  • consonants_only (Boolean, nil) (defaults to: nil)

    Override consonants-only mode for this instance.

  • reverse (Boolean, nil) (defaults to: nil)

    Override reverse mode for this instance.

  • logfile_name (String, nil) (defaults to: nil)

    Override the log file path for this instance.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/spoonerize/spoonerism.rb', line 37

def initialize(
  *words,
  config: Spoonerize.config,
  lazy: nil,
  lazy_words: nil,
  excluded_words: nil,
  consonants_only: nil,
  reverse: nil,
  logfile_name: nil
)
  @words = normalize_words(words).map(&:downcase)
  @config = config.with(**{
    lazy: lazy,
    lazy_words: lazy_words,
    excluded_words: excluded_words,
    consonants_only: consonants_only,
    reverse: reverse,
    logfile_name: logfile_name
  }.reject { |_, value| value.nil? })
end

Instance Attribute Details

#configSpoonerize::Config (readonly)

Configuration values for this spoonerism.

Returns:



21
22
23
# File 'lib/spoonerize/spoonerism.rb', line 21

def config
  @config
end

#wordsArray (readonly)

The words originally passed at initialization.

Returns:

  • (Array)


15
16
17
# File 'lib/spoonerize/spoonerism.rb', line 15

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 config.excluded_words

  • Any lazy words, if lazy mode is true

Returns:

  • (Array)


115
116
117
118
119
# File 'lib/spoonerize/spoonerism.rb', line 115

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

#enough_flippable_words?Boolean

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

Returns:

  • (Boolean)


97
98
99
# File 'lib/spoonerize/spoonerism.rb', line 97

def enough_flippable_words?
  words.each_index.count { |index| flippable?(index) } > 1
end

#saveArray

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

Returns:

  • (Array)


105
106
107
# File 'lib/spoonerize/spoonerism.rb', line 105

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)


63
64
65
66
67
# File 'lib/spoonerize/spoonerism.rb', line 63

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)


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

def to_h
  words.zip(spoonerize).to_h
end

#to_jsonString

Same as to_h, but as json.

Returns:

  • (String)


89
90
91
# File 'lib/spoonerize/spoonerism.rb', line 89

def to_json
  to_h.to_json
end

#to_sString

Spoonerized results as a joined string.

Returns:

  • (String)


73
74
75
# File 'lib/spoonerize/spoonerism.rb', line 73

def to_s
  spoonerize.join(" ")
end