Class: Spoonerize::Spoonerism
- Inherits:
-
Object
- Object
- Spoonerize::Spoonerism
- Defined in:
- lib/spoonerize/spoonerism.rb
Overview
The main word-flipper.
Instance Attribute Summary collapse
-
#config ⇒ Hash
readonly
The options from
config_fileas a hash. -
#config_file ⇒ String
The configuration file.
-
#excluded_words ⇒ Array
The words that are to be excluded.
-
#lazy ⇒ Boolean
writeonly
This boolean determines if flipping should be performed lazily.
-
#logfile_name ⇒ String
The full path to the log file.
-
#reverse ⇒ Boolean
writeonly
This boolean determines if flipping should be reversed.
-
#words ⇒ Array
readonly
The words originally passed at initialization.
Instance Method Summary collapse
-
#all_excluded_words ⇒ Object
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. -
#config_file_loaded? ⇒ Boolean
Has a config file been loaded?.
-
#enough_flippable_words? ⇒ Boolean
Returns true if there are more than one non-excluded word to flip.
-
#initialize(words, config_file = nil) {|_self| ... } ⇒ Spoonerism
constructor
Initialize instance.
-
#lazy? ⇒ Boolean
Should the lazy words be excluded?.
-
#load_config_file ⇒ Hash
Loads the config file.
-
#reverse? ⇒ Boolean
Should the words flip the other direction?.
-
#save ⇒ Object
Saves the flipped words to the log file, along with the options.
-
#spoonerize ⇒ Object
Iterates through words array, and maps its elements to the output of flip_words.
-
#to_h ⇒ Object
Returns hash of the original words mapped to their flipped counterparts.
-
#to_json ⇒ Object
Same as to_h, but as json.
-
#to_s ⇒ Object
Returns spoonerize array as a joined string.
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`.
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.(config_file) @config_file_loaded = false @logfile_name = File.( File.join(ENV["HOME"], ".cache", "spoonerize", "spoonerize.csv") ) load_config_file if config_file yield self if block_given? end |
Instance Attribute Details
#config ⇒ Hash (readonly)
The options from config_file as a hash.
54 55 56 |
# File 'lib/spoonerize/spoonerism.rb', line 54 def config @config end |
#config_file ⇒ String
The configuration file. Default is nil. If set to a string, and the file exists, it is used to set options.
48 49 50 |
# File 'lib/spoonerize/spoonerism.rb', line 48 def config_file @config_file end |
#excluded_words ⇒ Array
The words that are to be excluded.
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.
17 18 19 |
# File 'lib/spoonerize/spoonerism.rb', line 17 def lazy=(value) @lazy = value end |
#logfile_name ⇒ String
The full path to the log file.
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.
25 26 27 |
# File 'lib/spoonerize/spoonerism.rb', line 25 def reverse=(value) @reverse = value end |
#words ⇒ Array (readonly)
The words originally passed at initialization.
9 10 11 |
# File 'lib/spoonerize/spoonerism.rb', line 9 def words @words end |
Instance Method Details
#all_excluded_words ⇒ Object
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?
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
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?
138 139 140 |
# File 'lib/spoonerize/spoonerism.rb', line 138 def lazy? @lazy end |
#load_config_file ⇒ Hash
Loads the config file
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?
144 145 146 |
# File 'lib/spoonerize/spoonerism.rb', line 144 def reverse? @reverse end |
#save ⇒ Object
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, .join(", ")]) end |
#spoonerize ⇒ Object
Iterates through words array, and maps its elements to the output of flip_words. Returns results as an array.
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_h ⇒ Object
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_json ⇒ Object
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_s ⇒ Object
Returns spoonerize array as a joined string.
106 107 108 |
# File 'lib/spoonerize/spoonerism.rb', line 106 def to_s spoonerize.join(" ") end |