class Spoonerize::Spoonerism

The main word-flipper.

Attributes

config[R]

The options from config_file as a hash.

@return [Hash] Options from config_file

config_file[R]

The configuration file. Default is nil. If set to a string, and the file exists, it is used to set options.

@return [String] file path

excluded_words[RW]

The words that are to be excluded.

@param [Array] words

@return [Array]

lazy[W]

This boolean determines if flipping should be performed lazily.

@param [Boolean] true if should be lazy.

@return [Boolean]

logfile_name[RW]

The full path to the log file.

@param [String] file

@return [String]

reverse[W]

This boolean determines if flipping should be reversed.

@param [Boolean] true if should be reversed.

@return [Boolean]

words[R]

The words originally passed at initialization.

@return [Array]

Public Class Methods

new(words, config_file = nil) { |self| ... } click to toggle source

Initialize instance. You can also use the config_file either by passing it at initialization, or via the setter. The config file will be automatically loaded if passed at initialization, before the instance is yielded so you can still change the values via the block. If set via the setter, you must call `#load_config_file`.

@param [Array] words

@param [String] config_file

@example

# Config file would be automatically loaded before block is executed.
s = Spoonerise::Spoonerism.new(%w[not too shabby], '~/.spoonerize.yml') do |sp|
  sp.reverse = true # Would override setting from config file
end
# Config file would need to be manually loaded.
s = Spoonerise::Spoonerism.new(%w[not too shabby]) do |sp|
  sp.config_file = '~/.spoonerize.yml'
  sp.reverse = true
end
s.load_config_file # Would override setting from initialization
# File lib/spoonerize/spoonerism.rb, line 79
def initialize(words, config_file = nil)
  @config = {}
  @excluded_words = []
  @words = words.map(&:downcase)
  @lazy = false
  @reverse = false
  @config_file = config_file && File.expand_path(config_file)
  @config_file_loaded = false
  @logfile_name = File.expand_path(
    File.join(ENV['HOME'], '.cache', 'spoonerize', 'spoonerize.csv')
  )

  load_config_file if config_file

  yield self if block_given?
end

Public Instance Methods

all_excluded_words() click to toggle source

Returns an array of words to exclude by combining three arrays:

  • Any word in the passed arguments that's only one character

  • Any user-passed words, stored in excluded_words

  • If lazy-mode, the LAZY_WORDS from yaml file are added

# File lib/spoonerize/spoonerism.rb, line 160
def all_excluded_words
  (excluded_words + (lazy? ? LAZY_WORDS : [])).map(&:downcase)
end
config_file=(config_file) click to toggle source

Setter for config_file. Must be expanded in case the user uses `~` for home.

@param [String] file

@return [String]

# File lib/spoonerize/spoonerism.rb, line 171
def config_file=(config_file)
  @config_file = File.expand_path(config_file)
end
config_file_loaded?() click to toggle source

Has a config file been loaded?

@return [Boolean]

# File lib/spoonerize/spoonerism.rb, line 127
def config_file_loaded?
  @config_file_loaded
end
enough_flippable_words?() click to toggle source

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

# File lib/spoonerize/spoonerism.rb, line 133
def enough_flippable_words?
  (words - all_excluded_words).size > 1
end
lazy?() click to toggle source

Should the lazy words be excluded?

# File lib/spoonerize/spoonerism.rb, line 139
def lazy?
  @lazy
end
load_config_file() click to toggle source

Loads the config file

@return [Hash] The config options

# File lib/spoonerize/spoonerism.rb, line 179
def load_config_file
  raise 'No config file set' if config_file.nil?
  raise "File #{config_file} does not exist" unless File.file?(config_file)
  @config = YAML::load_file(config_file)
  @config_file_loaded = true
  @config.each { |k, v| send("#{k}=", v) }
end
reverse?() click to toggle source

Should the words flip the other direction?

# File lib/spoonerize/spoonerism.rb, line 145
def reverse?
  @reverse
end
save() click to toggle source

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

# File lib/spoonerize/spoonerism.rb, line 151
def save
  log.write([words.join(' '), to_s, options.join(', ')])
end
spoonerize() click to toggle source

Iterates through words array, and maps its elements to the output of flip_words. Returns results as an array.

# File lib/spoonerize/spoonerism.rb, line 99
def spoonerize
  raise JakPibError, 'Not enough words to flip' unless enough_flippable_words?

  words.map.with_index { |word, idx| flip_words(word, idx) }
end
to_h() click to toggle source

Returns hash of the original words mapped to their flipped counterparts.

# File lib/spoonerize/spoonerism.rb, line 113
def to_h
  Hash[words.zip(spoonerize)]
end
to_json() click to toggle source

Same as to_h, but as json.

# File lib/spoonerize/spoonerism.rb, line 119
def to_json
  to_h.to_json
end
to_s() click to toggle source

Returns spoonerize array as a joined string.

# File lib/spoonerize/spoonerism.rb, line 107
def to_s
  spoonerize.join(' ')
end