class Spoonerize::Spoonerism
The main word-flipper.
Attributes
The options from config_file
as a hash.
@return [Hash] Options from config_file
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
The words that are to be excluded.
@param [Array] words
@return [Array]
This boolean determines if flipping should be performed lazily.
@param [Boolean] true if should be lazy.
@return [Boolean]
The full path to the log file.
@param [String] file
@return [String]
This boolean determines if flipping should be reversed.
@param [Boolean] true if should be reversed.
@return [Boolean]
The words originally passed at initialization.
@return [Array]
Public Class Methods
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
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
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
Has a config file been loaded?
@return [Boolean]
# File lib/spoonerize/spoonerism.rb, line 127 def config_file_loaded? @config_file_loaded end
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
Should the lazy words be excluded?
# File lib/spoonerize/spoonerism.rb, line 139 def lazy? @lazy end
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
Should the words flip the other direction?
# File lib/spoonerize/spoonerism.rb, line 145 def reverse? @reverse end
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
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
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
Same as to_h
, but as json.
# File lib/spoonerize/spoonerism.rb, line 119 def to_json to_h.to_json end
Returns spoonerize array as a joined string.
# File lib/spoonerize/spoonerism.rb, line 107 def to_s spoonerize.join(' ') end