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, config_file = nil) {|_self| ... } ⇒ Spoonerism

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`.

Examples:

# 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

Parameters:

  • words (Array)
  • config_file (String) (defaults to: nil)

Yields:

  • (_self)

Yield Parameters:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/spoonerize/spoonerism.rb', line 78

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

Instance Attribute Details

#configHash (readonly)

The options from config_file as a hash.

Returns:

  • (Hash)

    Options from config_file



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

def config
  @config
end

#config_fileString

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

Returns:

  • (String)

    file path



48
49
50
# File 'lib/spoonerize/spoonerism.rb', line 48

def config_file
  @config_file
end

#excluded_wordsArray

The words that are to be excluded.

Parameters:

  • words (Array)

Returns:

  • (Array)


41
42
43
# File 'lib/spoonerize/spoonerism.rb', line 41

def excluded_words
  @excluded_words
end

#lazy=(value) ⇒ Boolean (writeonly)

This boolean determines if flipping should be performed lazily.

Parameters:

  • true (Boolean)

    if should be lazy.

Returns:

  • (Boolean)


17
18
19
# File 'lib/spoonerize/spoonerism.rb', line 17

def lazy=(value)
  @lazy = value
end

#logfile_nameString

The full path to the log file.

Parameters:

  • file (String)

Returns:

  • (String)


33
34
35
# File 'lib/spoonerize/spoonerism.rb', line 33

def logfile_name
  @logfile_name
end

#reverse=(value) ⇒ Boolean (writeonly)

This boolean determines if flipping should be reversed.

Parameters:

  • true (Boolean)

    if should be reversed.

Returns:

  • (Boolean)


25
26
27
# File 'lib/spoonerize/spoonerism.rb', line 25

def reverse=(value)
  @reverse = value
end

#wordsArray (readonly)

The words originally passed at initialization.

Returns:

  • (Array)


9
10
11
# File 'lib/spoonerize/spoonerism.rb', line 9

def words
  @words
end

Instance Method Details

#all_excluded_wordsObject

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



159
160
161
# File 'lib/spoonerize/spoonerism.rb', line 159

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

#config_file_loaded?Boolean

Has a config file been loaded?

Returns:

  • (Boolean)


126
127
128
# File 'lib/spoonerize/spoonerism.rb', line 126

def config_file_loaded?
  @config_file_loaded
end

#enough_flippable_words?Boolean

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

Returns:

  • (Boolean)


132
133
134
# File 'lib/spoonerize/spoonerism.rb', line 132

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

#lazy?Boolean

Should the lazy words be excluded?

Returns:

  • (Boolean)


138
139
140
# File 'lib/spoonerize/spoonerism.rb', line 138

def lazy?
  @lazy
end

#load_config_fileHash

Loads the config file

Returns:

  • (Hash)

    The config options



178
179
180
181
182
183
184
# File 'lib/spoonerize/spoonerism.rb', line 178

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?Boolean

Should the words flip the other direction?

Returns:

  • (Boolean)


144
145
146
# File 'lib/spoonerize/spoonerism.rb', line 144

def reverse?
  @reverse
end

#saveObject

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



150
151
152
# File 'lib/spoonerize/spoonerism.rb', line 150

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

#spoonerizeObject

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

Raises:



98
99
100
101
102
# File 'lib/spoonerize/spoonerism.rb', line 98

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_hObject

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



112
113
114
# File 'lib/spoonerize/spoonerism.rb', line 112

def to_h
  words.zip(spoonerize).to_h
end

#to_jsonObject

Same as to_h, but as json.



118
119
120
# File 'lib/spoonerize/spoonerism.rb', line 118

def to_json
  to_h.to_json
end

#to_sObject

Returns spoonerize array as a joined string.



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

def to_s
  spoonerize.join(" ")
end