spoonerize 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13edc030a624f0a4d426b0e0580130e4e1c50dc3ad6b4c6c845309616f572582
4
- data.tar.gz: 925608680204984f5f51043186256d31999f717951dd461015a3e1ab6a6e6d4a
3
+ metadata.gz: 932ce6e7e6441c787ded513aebed0b3e0b83fa6afa83790abe8da66c9314e96b
4
+ data.tar.gz: 6a5dedc04324853f1bfbc63e6317b17737eab6fb10b89ae11136b598438a4f08
5
5
  SHA512:
6
- metadata.gz: baa74aac8e1e9751f983673be2654de72f9341bb9e6ca39a31ca95809194bbd618cdaa2b209c2c1c5dbd51242a19801b56a5aaa72e60287087ca7814e5af201c
7
- data.tar.gz: 9ca5c6ef469659fa70618daa0108f4bb5d0c57f1aa7c7f87fcac1302cafbcc17a425da4d78cb932db23d3d20e1d48c14c80a2396af03ac9bf2f0e1ad795f3ca7
6
+ metadata.gz: b579d2925471dca724865574a82a827a5f0d31229991d1746db4890a0d4c250141daa536bbc932c829fd9d7e8f5af63e89b85846a788d23eca766219c2a60681
7
+ data.tar.gz: 1531172c418c2cf00e155ca5e56c888218888af9c8b6694ff7a9b3195dbda173c122058e7ac46c4705c311d7951a8c6f773aa09d4571a447da7f4781ca425168
@@ -1,4 +1,5 @@
1
- name: Ruby
1
+ # .github/workflows/ruby.yml
2
+ name: Ruby CI
2
3
 
3
4
  on:
4
5
  push:
@@ -8,17 +9,31 @@ on:
8
9
 
9
10
  jobs:
10
11
  test:
12
+ runs-on: ubuntu-22.04
11
13
 
12
- runs-on: ubuntu-latest
14
+ strategy:
15
+ fail-fast: false
16
+ matrix:
17
+ ruby-version: ['3.2', '3.3', '4.0']
13
18
 
14
19
  steps:
15
- - uses: actions/checkout@v2
16
- - name: Set up Ruby 2.6
17
- uses: actions/setup-ruby@v1
18
- with:
19
- ruby-version: 2.6.x
20
- - name: Build and test with Rake
21
- run: |
22
- gem install bundler
23
- bundle install
24
- bundle exec rake test
20
+ - uses: actions/checkout@v3
21
+
22
+ # Set up Ruby and Bundler
23
+ - name: Set up Ruby ${{ matrix.ruby-version }}
24
+ uses: ruby/setup-ruby@v1
25
+ with:
26
+ ruby-version: ${{ matrix.ruby-version }}
27
+ bundler: ${{ matrix.ruby-version == '4.0' && '4.0.9' || '2.4.13' }}
28
+ bundler-cache: true
29
+
30
+ - name: Print Ruby and Bundler versions
31
+ run: |
32
+ ruby -v
33
+ bundle -v
34
+
35
+ - name: Install dependencies
36
+ run: bundle install --jobs 4 --retry 3
37
+
38
+ - name: Run tests
39
+ run: bundle exec rake
data/Gemfile.lock CHANGED
@@ -1,23 +1,39 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spoonerize (0.1.2)
4
+ spoonerize (0.2.0)
5
+ csv
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
- power_assert (2.0.0)
10
- rake (13.0.3)
11
- test-unit (3.4.1)
10
+ csv (3.3.5)
11
+ date (3.5.1)
12
+ erb (6.0.4)
13
+ power_assert (3.0.1)
14
+ psych (5.4.0)
15
+ date
16
+ stringio
17
+ rake (13.4.2)
18
+ rdoc (7.2.0)
19
+ erb
20
+ psych (>= 4.0.0)
21
+ tsort
22
+ stringio (3.2.0)
23
+ test-unit (3.7.8)
12
24
  power_assert
25
+ tsort (0.2.0)
13
26
 
14
27
  PLATFORMS
28
+ arm64-darwin-25
15
29
  x86_64-darwin-20
30
+ x86_64-linux
16
31
 
17
32
  DEPENDENCIES
18
33
  rake (~> 13.0, >= 13.0.1)
34
+ rdoc
19
35
  spoonerize!
20
36
  test-unit (~> 3.3, >= 3.3.5)
21
37
 
22
38
  BUNDLED WITH
23
- 2.2.16
39
+ 4.0.10
data/README.md CHANGED
@@ -104,19 +104,20 @@ Here is a list of all available options:
104
104
  ```
105
105
 
106
106
  ### Config File
107
- You can create a config file called `~/.spoonerize.yml`. In this file, you can
108
- change default options at runtime. Available settings are:
109
-
110
- ```yaml
111
- # Setting Default
112
- excluded_words: []
113
- lazy: false
114
- reverse: false
115
- logfile_name: '~/.cache/spoonerize/spoonerize.csv'
107
+ You can create a Ruby config file called `~/.spoonerizerc`. The CLI loads this
108
+ file automatically before it parses command-line options, so options set in the
109
+ file can still be overridden at runtime by executable flags.
110
+
111
+ ```ruby
112
+ Spoonerize.configure do |config|
113
+ config.excluded_words = []
114
+ config.lazy = false
115
+ config.reverse = false
116
+ config.logfile_name = File.expand_path("~/.cache/spoonerize/spoonerize.csv")
117
+ end
116
118
  ```
117
119
 
118
- Options set by this file can be overridden at runtime by the use of the
119
- executable's flags.
120
+ Because the file is Ruby, you can set only the values you want to change.
120
121
 
121
122
  ## API
122
123
  The API is [fully
@@ -126,39 +127,41 @@ are some quick examples of how you could use this in your ruby code.
126
127
  ```ruby
127
128
  require 'spoonerize'
128
129
 
129
- spoonerism = Spoonerize::Spoonerism.new(%w[not too shabby]) do |s|
130
- s.reverse = true
130
+ spoonerism = Spoonerize::Spoonerism.new(%w[not too shabby])
131
+
132
+ spoonerism.to_s
133
+ # => tot shoo nabby
134
+
135
+ Spoonerize.configure do |config|
136
+ config.reverse = true
131
137
  end
132
138
 
133
- spoonerism.spoonerize
139
+ spoonerism.to_s
134
140
  # => shot noo tabby
135
141
 
136
- spoonerism.reverse = false
137
- spoonerism.spoonerize
138
- # => tot shoo nabby
139
-
140
- spoonerism.logfile_name = '~/.cache/spoonerize/spoonerize.csv'
142
+ Spoonerize.configure do |config|
143
+ config.logfile_name = File.expand_path("~/.cache/spoonerize/spoonerize.csv")
144
+ end
141
145
  spoonerism.save
142
146
  ```
143
147
 
144
- You can also use the [config file](#config-file), either by passing it at
145
- initialization, or via the setter. The config file will be automatically loaded
146
- if passed at initialization, before the instance is yielded so you can still
147
- change the values via the block. If set via the setter, you must call
148
- `#load_config_file`.
148
+ You can also configure Spoonerize in Ruby before creating a spoonerism:
149
149
 
150
150
  ```ruby
151
- # Config file would be automatically loaded before block is executed.
152
- s = Spoonerise::Spoonerism.new(%w[not too shabby], '~/.spoonerize.yml') do |sp|
153
- sp.reverse = true
151
+ Spoonerize.configure do |config|
152
+ config.reverse = true
154
153
  end
155
154
 
156
- # Config file would need to be manually loaded.
157
- s = Spoonerise::Spoonerism.new(%w[not too shabby]) do |sp|
158
- sp.config_file = '~/.spoonerize.yml'
159
- end
155
+ s = Spoonerize::Spoonerism.new(%w[not too shabby])
156
+ s.spoonerize
157
+ # => shot noo tabby
158
+ ```
159
+
160
+ Or load a config file manually:
160
161
 
161
- s.load_config_file
162
+ ```ruby
163
+ Spoonerize.load_config_file("~/.spoonerizerc")
164
+ s = Spoonerize::Spoonerism.new(%w[not too shabby])
162
165
  ```
163
166
 
164
167
  ## Self Promotion
data/Rakefile CHANGED
@@ -1,19 +1,67 @@
1
- require_relative 'lib/spoonerize'
2
- require 'bundler/gem_tasks'
3
- require 'rdoc/task'
4
- require 'rake/testtask'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/spoonerize"
4
+ require "bundler/gem_tasks"
5
+ require "rdoc/task"
6
+ require "rake/testtask"
5
7
 
6
8
  Rake::TestTask.new do |t|
7
- t.libs = ['lib']
9
+ t.libs = ["lib"]
8
10
  t.warning = true
9
11
  t.verbose = true
10
- t.test_files = FileList['test/**/*_test.rb']
12
+ t.test_files = FileList["test/**/*_test.rb"]
11
13
  end
12
14
 
13
15
  RDoc::Task.new do |rdoc|
14
- rdoc.main = 'README.md'
15
- rdoc.rdoc_dir = 'docs'
16
- rdoc.rdoc_files.include('README.md', 'lib/**/*.rb')
16
+ rdoc.main = "README.md"
17
+ rdoc.rdoc_dir = "docs"
18
+ rdoc.rdoc_files.include("README.md", "lib/**/*.rb")
19
+ end
20
+
21
+ task default: :test
22
+
23
+ namespace :version do
24
+ desc "Print the current version from the version.rb file"
25
+ task :current do
26
+ puts Spoonerize::VERSION
27
+ end
28
+
29
+ namespace :increment do
30
+ desc "Increment the version's PATCH level"
31
+ task :patch do
32
+ File.join(__dir__, "lib", "spoonerize", "version.rb").then do |version_file|
33
+ File.write(
34
+ version_file,
35
+ File.read(version_file).sub(/(PATCH\s=\s)(\d+)/) { "#{$1}#{$2.next}" }
36
+ )
37
+ end
38
+ system("bundle lock")
39
+ end
40
+ desc "Increment the version's MINOR level"
41
+ task :minor do
42
+ File.join(__dir__, "lib", "spoonerize", "version.rb").then do |version_file|
43
+ File.write(
44
+ version_file,
45
+ File.read(version_file)
46
+ .sub(/(PATCH\s=\s)(\d+)/) { "#{$1}0" }
47
+ .sub(/(MINOR\s=\s)(\d+)/) { "#{$1}#{$2.next}" }
48
+ )
49
+ end
50
+ system("bundle lock")
51
+ end
52
+ desc "Increment the version's MAJOR level"
53
+ task :major do
54
+ File.join(__dir__, "lib", "spoonerize", "version.rb").then do |version_file|
55
+ File.write(
56
+ version_file,
57
+ File.read(version_file)
58
+ .sub(/(PATCH\s=\s)(\d+)/) { "#{$1}0" }
59
+ .sub(/(MINOR\s=\s)(\d+)/) { "#{$1}0" }
60
+ .sub(/(MAJOR\s=\s)(\d+)/) { "#{$1}#{$2.next}" }
61
+ )
62
+ end
63
+ system("bundle lock")
64
+ end
65
+ end
17
66
  end
18
67
 
19
- task :default => :test
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Spoonerize
2
4
  ##
3
5
  # Class that handles bumping an index.
4
6
  class Bumper
5
-
6
7
  ##
7
8
  # The number after being bumped.
8
9
  #
@@ -55,7 +56,7 @@ module Spoonerize
55
56
 
56
57
  def bump_value(val) # :nodoc:
57
58
  return val - 1 if @reverse
58
- val + 1 == @max_value ? 0 : val + 1
59
+ (val + 1 == @max_value) ? 0 : val + 1
59
60
  end
60
61
  end
61
62
  end
@@ -1,17 +1,16 @@
1
- require 'optparse'
2
- require 'yaml'
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
3
4
 
4
5
  module Spoonerize
5
6
  ##
6
7
  # The class for handling the command-line interface.
7
8
  class Cli
8
-
9
9
  ##
10
- # The preferences file the user can create to change runtime options.
10
+ # The config file the user can create to change default runtime options.
11
11
  #
12
12
  # @return [String]
13
- PREFERENCE_FILE =
14
- File.expand_path(File.join(ENV['HOME'], '.spoonerize.yml')).freeze
13
+ CONFIG_FILE = File.expand_path(File.join(ENV["HOME"], ".spoonerizerc"))
15
14
 
16
15
  ##
17
16
  # Creates an instance of +Spoonerism+ and runs what the user requested.
@@ -20,12 +19,12 @@ module Spoonerize
20
19
  def self.execute(options = [])
21
20
  exe = new(options)
22
21
 
23
- if exe.print?
22
+ if exe.print_log?
24
23
  exe.print_log
25
24
  return
26
25
  end
27
26
 
28
- puts exe.spoonerism.to_s
27
+ puts exe.spoonerism
29
28
  exe.print_mappings if exe.map?
30
29
 
31
30
  if exe.save?
@@ -53,23 +52,20 @@ module Spoonerize
53
52
  #
54
53
  # @return [self]
55
54
  def initialize(options)
56
- @map = false
57
- @save = false
58
- @print = false
59
- @options = options
55
+ Spoonerize.load_config_file(CONFIG_FILE) if File.file?(CONFIG_FILE)
56
+ @map = false
57
+ @save = false
58
+ @print_log = false
59
+ @options = options
60
60
  @preferences = get_preferences
61
61
  end
62
62
 
63
63
  ##
64
- # Sets up an instance of +Spoonerize::Spoonerism+ and passes all user
65
- # preferences.
64
+ # Sets up an instance of +Spoonerize::Spoonerism+
66
65
  #
67
66
  # @return [Spoonerize::Spoonerism]
68
67
  def spoonerism
69
- pf = File.file?(PREFERENCE_FILE) ? PREFERENCE_FILE : nil
70
- @spoonerism ||= Spoonerism.new(options, pf) do |s|
71
- preferences.each { |k, v| s.send("#{k}=", v) }
72
- end
68
+ @spoonerism ||= Spoonerism.new(options)
73
69
  end
74
70
 
75
71
  ##
@@ -92,8 +88,8 @@ module Spoonerize
92
88
  # Should we print to the command line?
93
89
  #
94
90
  # @return [Boolean]
95
- def print?
96
- @print
91
+ def print_log?
92
+ @print_log
97
93
  end
98
94
 
99
95
  ##
@@ -101,8 +97,7 @@ module Spoonerize
101
97
  #
102
98
  # @return [Integer]
103
99
  def longest_word_length
104
- @longest_word_length ||=
105
- spoonerism.spoonerize.group_by(&:size).max.first.size
100
+ @longest_word_length ||= spoonerism.spoonerize.max_by(&:size).size
106
101
  end
107
102
 
108
103
  ##
@@ -110,8 +105,9 @@ module Spoonerize
110
105
  #
111
106
  # @return [nil]
112
107
  def print_log
113
- s = Spoonerize::Log.new(spoonerism.logfile_name)
114
- s.each { |row| print row.join(' | ') + "\n" }
108
+ Spoonerize::Log.new(Spoonerize.config.logfile_name).each do |row|
109
+ puts row.join(" | ")
110
+ end
115
111
  end
116
112
 
117
113
  ##
@@ -120,7 +116,7 @@ module Spoonerize
120
116
  # @return [nil]
121
117
  def print_mappings
122
118
  spoonerism.to_h.each do |k, v|
123
- puts "%-#{longest_word_length}s => %s" % [k, v]
119
+ printf("%-#{longest_word_length + 1}s => %s\n", k, v)
124
120
  end
125
121
  end
126
122
 
@@ -132,23 +128,23 @@ module Spoonerize
132
128
  {}.tap do |prefs|
133
129
  OptionParser.new do |o|
134
130
  o.version = ::Spoonerize::Version.to_s
135
- o.on('-r', '--[no-]reverse', 'Reverse flipping') do |v|
136
- prefs['reverse'] = v
131
+ o.on("-r", "--[no-]reverse", "Reverse flipping") do |v|
132
+ Spoonerize.config.reverse = v
137
133
  end
138
- o.on('-l', '--[no-]lazy', 'Skip small words') do |v|
139
- prefs['lazy'] = v
134
+ o.on("-l", "--[no-]lazy", "Skip small words") do |v|
135
+ Spoonerize.config.lazy = v
140
136
  end
141
- o.on('-m', '--[no-]map', 'Print words mapping') do |v|
137
+ o.on("-m", "--[no-]map", "Print words mapping") do |v|
142
138
  @map = v
143
139
  end
144
- o.on('-p', '--[no-]print', 'Print all entries in the log') do |v|
145
- @print = v
140
+ o.on("-p", "--[no-]print-log", "Print all entries in the log") do |v|
141
+ @print_log = v
146
142
  end
147
- o.on('-s', '--[no-]save', 'Save results in log') do |v|
143
+ o.on("-s", "--[no-]save", "Save results in log") do |v|
148
144
  @save = v
149
145
  end
150
- o.on('--exclude=WORDS', Array, 'Words to skip') do |v|
151
- prefs['exclude'] = v
146
+ o.on("--exclude=WORD", Array, "Words to skip") do |v|
147
+ Spoonerize.config.excluded_words = v
152
148
  end
153
149
  end.parse!(options)
154
150
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spoonerize
4
+ class Config
5
+ ##
6
+ # Lazy mode. If true, words in +lazy_words+ will not be altered.
7
+ #
8
+ # @return [Boolean]
9
+ attr_accessor :lazy
10
+
11
+ ##
12
+ # Words to skip when +lazy+ is true.
13
+ #
14
+ # @return [Array]
15
+ attr_accessor :lazy_words
16
+
17
+ ##
18
+ # Words that should not be altered.
19
+ #
20
+ # @return [Array]
21
+ attr_accessor :excluded_words
22
+
23
+ ##
24
+ # When true, reverse the order of the flipping. Only makes a difference
25
+ # when there are more than two flip-able words.
26
+ #
27
+ # @return [Boolean]
28
+ attr_accessor :reverse
29
+
30
+ ##
31
+ # Name of the log file. Should be a fully-qualified file path.
32
+ #
33
+ # @return [String]
34
+ attr_accessor :logfile_name
35
+
36
+ ##
37
+ # Create instance of Config.
38
+ #
39
+ # @return [Spoonerize::Config]
40
+ def initialize
41
+ @lazy = false
42
+ @lazy_words = %w[i a an and in of the my your his her him hers to is]
43
+ @excluded_words = []
44
+ @reverse = false
45
+ @logfile_name = File.expand_path(
46
+ File.join(ENV["HOME"], ".cache", "spoonerize", "spoonerize.csv")
47
+ )
48
+ end
49
+ end
50
+ end
@@ -1,11 +1,12 @@
1
- require 'csv'
2
- require 'fileutils'
1
+ # frozen_string_literal: true
2
+
3
+ require "csv"
4
+ require "fileutils"
3
5
 
4
6
  module Spoonerize
5
7
  ##
6
- # Class that handles reading/writing logs.
8
+ # Class that handles reading/writing logs. Log file is stored as a simple CSV.
7
9
  class Log
8
-
9
10
  ##
10
11
  # The file name to use.
11
12
  #
@@ -46,13 +47,13 @@ module Spoonerize
46
47
  #
47
48
  # @return [Array]
48
49
  def write(row)
49
- ::CSV.open(file, 'a') { |csv| csv << row }
50
+ ::CSV.open(file, "a") { |csv| csv << row }
50
51
  end
51
52
 
52
53
  ##
53
54
  # Iterate through each line of the file.
54
55
  #
55
- # @return [Array]
56
+ # @return [Enumerable]
56
57
  def each
57
58
  contents.each { |row| yield row }
58
59
  end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Spoonerize
2
4
  ##
3
5
  # The main word-flipper.
4
6
  class Spoonerism
5
-
6
7
  ##
7
8
  # The words originally passed at initialization.
8
9
  #
@@ -10,178 +11,76 @@ module Spoonerize
10
11
  attr_reader :words
11
12
 
12
13
  ##
13
- # This boolean determines if flipping should be performed lazily.
14
- #
15
- # @param [Boolean] true if should be lazy.
16
- #
17
- # @return [Boolean]
18
- attr_writer :lazy
19
-
20
- ##
21
- # This boolean determines if flipping should be reversed.
22
- #
23
- # @param [Boolean] true if should be reversed.
24
- #
25
- # @return [Boolean]
26
- attr_writer :reverse
27
-
28
- ##
29
- # The full path to the log file.
30
- #
31
- # @param [String] file
32
- #
33
- # @return [String]
34
- attr_accessor :logfile_name
35
-
36
- ##
37
- # The words that are to be excluded.
14
+ # Initialize instance.
38
15
  #
39
16
  # @param [Array] words
40
17
  #
41
- # @return [Array]
42
- attr_accessor :excluded_words
43
-
44
- ##
45
- # The configuration file. Default is +nil+. If set to a string, and the file
46
- # exists, it is used to set options.
47
- #
48
- # @return [String] file path
49
- attr_reader :config_file
50
-
51
- ##
52
- # The options from +config_file+ as a hash.
53
- #
54
- # @return [Hash] Options from +config_file+
55
- attr_reader :config
56
-
57
- ##
58
- # Initialize instance. You can also use the +config_file+ either by passing
59
- # it at initialization, or via the setter. The config file will be
60
- # automatically loaded if passed at initialization, before the instance is
61
- # yielded so you can still change the values via the block. If set via the
62
- # setter, you must call `#load_config_file`.
63
- #
64
- # @param [Array] words
65
- #
66
- # @param [String] config_file
67
- #
68
- # @example
69
- # # Config file would be automatically loaded before block is executed.
70
- # s = Spoonerise::Spoonerism.new(%w[not too shabby], '~/.spoonerize.yml') do |sp|
71
- # sp.reverse = true # Would override setting from config file
72
- # end
73
- # # Config file would need to be manually loaded.
74
- # s = Spoonerise::Spoonerism.new(%w[not too shabby]) do |sp|
75
- # sp.config_file = '~/.spoonerize.yml'
76
- # sp.reverse = true
77
- # end
78
- # s.load_config_file # Would override setting from initialization
79
- def initialize(words, config_file = nil)
80
- @config = {}
81
- @excluded_words = []
18
+ # @return [Spoonerize::Spoonerism]
19
+ def initialize(words)
82
20
  @words = words.map(&:downcase)
83
- @lazy = false
84
- @reverse = false
85
- @config_file = config_file && File.expand_path(config_file)
86
- @config_file_loaded = false
87
- @logfile_name = File.expand_path(
88
- File.join(ENV['HOME'], '.cache', 'spoonerize', 'spoonerize.csv')
89
- )
90
-
91
- load_config_file if config_file
92
-
93
- yield self if block_given?
94
21
  end
95
22
 
96
23
  ##
97
24
  # Iterates through words array, and maps its elements to the output of
98
- # flip_words. Returns results as an array.
25
+ # flip_words.
26
+ #
27
+ # @return [Array]
99
28
  def spoonerize
100
- raise JakPibError, 'Not enough words to flip' unless enough_flippable_words?
29
+ raise "Not enough words to flip" unless enough_flippable_words?
101
30
 
102
31
  words.map.with_index { |word, idx| flip_words(word, idx) }
103
32
  end
104
33
 
105
34
  ##
106
- # Returns spoonerize array as a joined string.
35
+ # Spoonerized results as a joined string.
36
+ #
37
+ # @return [String]
107
38
  def to_s
108
- spoonerize.join(' ')
39
+ spoonerize.join(" ")
109
40
  end
110
41
 
111
42
  ##
112
- # Returns hash of the original words mapped to their flipped counterparts.
43
+ # Spoonerized results as a joined hash.
44
+ #
45
+ # @return [Hash]
113
46
  def to_h
114
- Hash[words.zip(spoonerize)]
47
+ words.zip(spoonerize).to_h
115
48
  end
116
49
 
117
50
  ##
118
51
  # Same as to_h, but as json.
52
+ #
53
+ # @return [String]
119
54
  def to_json
120
55
  to_h.to_json
121
56
  end
122
57
 
123
58
  ##
124
- # Has a config file been loaded?
59
+ # True if there are more than one non-excluded word to flip
125
60
  #
126
61
  # @return [Boolean]
127
- def config_file_loaded?
128
- @config_file_loaded
129
- end
130
-
131
- ##
132
- # Returns true if there are more than one non-excluded word to flip
133
62
  def enough_flippable_words?
134
63
  (words - all_excluded_words).size > 1
135
64
  end
136
65
 
137
- ##
138
- # Should the lazy words be excluded?
139
- def lazy?
140
- @lazy
141
- end
142
-
143
- ##
144
- # Should the words flip the other direction?
145
- def reverse?
146
- @reverse
147
- end
148
-
149
66
  ##
150
67
  # Saves the flipped words to the log file, along with the options
151
- def save
152
- log.write([words.join(' '), to_s, options.join(', ')])
153
- end
154
-
155
- ##
156
- # Returns an array of words to exclude by combining three arrays:
157
- # * Any word in the passed arguments that's only one character
158
- # * Any user-passed words, stored in +excluded_words+
159
- # * If lazy-mode, the LAZY_WORDS from yaml file are added
160
- def all_excluded_words
161
- (excluded_words + (lazy? ? LAZY_WORDS : [])).map(&:downcase)
162
- end
163
-
164
- ##
165
- # Setter for +config_file+. Must be expanded in case the user uses `~` for
166
- # home.
167
- #
168
- # @param [String] file
169
68
  #
170
- # @return [String]
171
- def config_file=(config_file)
172
- @config_file = File.expand_path(config_file)
69
+ # @return [Array]
70
+ def save
71
+ log.write([words.join(" "), to_s, options.join(", ")])
173
72
  end
174
73
 
175
74
  ##
176
- # Loads the config file
75
+ # Array of words to exclude by combining two arrays:
76
+ # * Any user-passed words, stored in +Spoonerize.config.excluded_words+
77
+ # * Any lazy words, if lazy mode is true
177
78
  #
178
- # @return [Hash] The config options
179
- def load_config_file
180
- raise 'No config file set' if config_file.nil?
181
- raise "File #{config_file} does not exist" unless File.file?(config_file)
182
- @config = YAML::load_file(config_file)
183
- @config_file_loaded = true
184
- @config.each { |k, v| send("#{k}=", v) }
79
+ # @return [Array]
80
+ def all_excluded_words
81
+ (Spoonerize.config.excluded_words + (
82
+ Spoonerize.config.lazy ? Spoonerize.config.lazy_words : []
83
+ )).map(&:downcase)
185
84
  end
186
85
 
187
86
  private
@@ -192,8 +91,8 @@ module Spoonerize
192
91
  # through the end of the word.
193
92
  def flip_words(word, idx) # :nodoc:
194
93
  return word if excluded?(idx)
195
- bumper = Bumper.new(idx, words.size, reverse?)
196
- bumper.bump until !excluded?(bumper.value)
94
+ bumper = Bumper.new(idx, words.size, Spoonerize.config.reverse)
95
+ bumper.bump while excluded?(bumper.value)
197
96
  words[bumper.value].match(consonants).to_s + word.match(vowels).to_s
198
97
  end
199
98
 
@@ -218,17 +117,19 @@ module Spoonerize
218
117
  ##
219
118
  # Creates and memoizes instance of the log file.
220
119
  def log # :nodoc:
221
- @log ||= Spoonerize::Log.new(logfile_name)
120
+ @log ||= Spoonerize::Log.new(Spoonerize.config.logfile_name)
222
121
  end
223
122
 
224
123
  ##
225
- # the options as a string
124
+ # The options that were passed at runtime as a string
226
125
  def options # :nodoc:
227
126
  [].tap do |o|
228
- o << 'Lazy' if lazy?
229
- o << 'Reverse' if reverse?
230
- o << "Exclude [#{all_excluded_words.join(', ')}]" if excluded_words.any?
231
- o << 'No Options' if o.empty?
127
+ o << "Lazy" if Spoonerize.config.lazy
128
+ o << "Reverse" if Spoonerize.config.reverse
129
+ if Spoonerize.config.excluded_words.any?
130
+ o << "Exclude [#{Spoonerize.config.excluded_words.join(", ")}]"
131
+ end
132
+ o << "No Options" if o.empty?
232
133
  end
233
134
  end
234
135
  end
@@ -5,23 +5,54 @@ module Spoonerize
5
5
  # Module that contains all gem version information. Follows semantic
6
6
  # versioning. Read: https://semver.org/
7
7
  module Version
8
-
9
8
  ##
10
9
  # Major version.
10
+ #
11
+ # @return [Integer]
11
12
  MAJOR = 0
12
13
 
13
14
  ##
14
15
  # Minor version.
15
- MINOR = 1
16
+ #
17
+ # @return [Integer]
18
+ MINOR = 2
16
19
 
17
20
  ##
18
21
  # Patch version.
19
- PATCH = 2
22
+ #
23
+ # @return [Integer]
24
+ PATCH = 0
25
+
26
+ module_function
27
+
28
+ ##
29
+ # Version as +[MAJOR, MINOR, PATCH]+
30
+ #
31
+ # @return [Array<Integer>]
32
+ def to_a
33
+ [MAJOR, MINOR, PATCH]
34
+ end
20
35
 
21
36
  ##
22
37
  # Version as +MAJOR.MINOR.PATCH+
23
- def self.to_s
24
- "#{MAJOR}.#{MINOR}.#{PATCH}"
38
+ #
39
+ # @return [String]
40
+ def to_s
41
+ to_a.join(".")
42
+ end
43
+
44
+ ##
45
+ # Version as +{major: MAJOR, minor: MINOR, patch: PATCH}+
46
+ #
47
+ # @return [Hash]
48
+ def to_h
49
+ %i[major minor patch].zip(to_a).to_h
25
50
  end
26
51
  end
52
+
53
+ ##
54
+ # The version, as a string.
55
+ #
56
+ # @return [String]
57
+ VERSION = Version.to_s
27
58
  end
data/lib/spoonerize.rb CHANGED
@@ -1,21 +1,67 @@
1
- require 'yaml'
2
- require_relative 'spoonerize/spoonerism'
3
- require_relative 'spoonerize/bumper'
4
- require_relative 'spoonerize/version'
5
- require_relative 'spoonerize/log'
6
- require_relative 'spoonerize/cli'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "spoonerize/config"
4
+ require_relative "spoonerize/spoonerism"
5
+ require_relative "spoonerize/bumper"
6
+ require_relative "spoonerize/version"
7
+ require_relative "spoonerize/log"
8
+ require_relative "spoonerize/cli"
7
9
 
8
10
  ##
9
11
  # The main namespace for the gem.
10
12
  module Spoonerize
11
13
  ##
12
- # The error exception raised when there are not enough flippable words.
13
- JakPibError = Class.new(StandardError)
14
+ # Has the config file been loaded?
15
+ @config_file_loaded = false
16
+
17
+ module_function
14
18
 
15
19
  ##
16
- # Excluded words from config files.
17
- LAZY_WORDS = YAML.load_file(
18
- File.join(File.dirname(__FILE__), 'config', 'lazy_words.yml')
19
- ).freeze
20
- end
20
+ # Method for accessing the configuration.
21
+ #
22
+ # @return [Spoonerize::Config]
23
+ def config
24
+ @config || reset_config
25
+ end
26
+
27
+ ##
28
+ # Reset all configuration values to their defaults.
29
+ #
30
+ # @return [Spoonerize::Config]
31
+ def reset_config
32
+ @config_file_loaded = false
33
+ @config = Spoonerize::Config.new
34
+ end
35
+
36
+ ##
37
+ # Allows for configuration via a block. Useful when making config files.
38
+ #
39
+ # @example
40
+ # Spoonerize.configure { |s| s.lazy = true }
41
+ def configure
42
+ yield config
43
+ end
44
+
45
+ ##
46
+ # Has a config file been loaded?
47
+ #
48
+ # @return [Boolean]
49
+ def config_file_loaded?
50
+ @config_file_loaded
51
+ end
21
52
 
53
+ ##
54
+ # Loads a config file.
55
+ #
56
+ # @param [String] file
57
+ #
58
+ # @return [String] file
59
+ def load_config_file(config_file)
60
+ ::File.expand_path(config_file).tap do |file|
61
+ raise "File #{file} does not exist." unless ::File.file?(file)
62
+
63
+ @config_file_loaded = true
64
+ load file
65
+ end
66
+ end
67
+ end
data/spoonerize.gemspec CHANGED
@@ -1,38 +1,35 @@
1
- require_relative 'lib/spoonerize/version'
1
+ require_relative "lib/spoonerize/version"
2
2
 
3
3
  Gem::Specification.new do |spec|
4
- spec.name = 'spoonerize'
5
- spec.version = Spoonerize::Version.to_s
6
- spec.authors = ['Evan Gray']
7
- spec.email = 'evanthegrayt@vivaldi.net'
8
- spec.license = 'MIT'
9
- spec.date = Time.now.strftime('%Y-%m-%d')
4
+ spec.name = "spoonerize"
5
+ spec.version = Spoonerize::VERSION
6
+ spec.authors = ["Evan Gray"]
7
+ spec.email = "evanthegrayt@vivaldi.net"
8
+ spec.license = "MIT"
10
9
 
11
- spec.summary = %q{Spoonerize phrases from the command line.}
12
- spec.description = %q{Spoonerize phrases from the command line. Comes with an API}
13
- spec.homepage = 'https://evanthegrayt.github.io/spoonerize/'
10
+ spec.summary = %(Spoonerize phrases from the command line.)
11
+ spec.description = %(Spoonerize phrases from the command line. Comes with an API)
12
+ spec.homepage = "https://evanthegrayt.github.io/spoonerize/"
14
13
 
15
14
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
15
  # to allow pushing to a single host or delete this section to allow pushing to any host.
17
16
  if spec.respond_to?(:metadata)
18
- spec.metadata['allowed_push_host'] = 'https://rubygems.org'
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
18
 
20
- spec.metadata['homepage_uri'] = spec.homepage
21
- spec.metadata['source_code_uri'] = 'https://github.com/evanthegrayt/spoonerize'
22
- spec.metadata['documentation_uri'] = 'https://evanthegrayt.github.io/spoonerize/'
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/evanthegrayt/spoonerize"
21
+ spec.metadata["documentation_uri"] = "https://evanthegrayt.github.io/spoonerize/"
23
22
  else
24
- raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
25
24
  end
26
25
 
27
26
  # Specify which files should be added to the gem when it is released.
28
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
- # spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
- # `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
- # end
32
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
- spec.bindir = 'bin'
34
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
35
- spec.require_paths = ['lib']
36
- spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.1'
37
- spec.add_development_dependency 'test-unit', '~> 3.3', '>= 3.3.5'
27
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ spec.bindir = "bin"
29
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+ spec.add_dependency "csv"
32
+ spec.add_development_dependency "rdoc"
33
+ spec.add_development_dependency "rake", "~> 13.0", ">= 13.0.1"
34
+ spec.add_development_dependency "test-unit", "~> 3.3", ">= 3.3.5"
38
35
  end
metadata CHANGED
@@ -1,15 +1,42 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spoonerize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Gray
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2021-05-22 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: csv
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rdoc
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
13
40
  - !ruby/object:Gem::Dependency
14
41
  name: rake
15
42
  requirement: !ruby/object:Gem::Requirement
@@ -66,10 +93,10 @@ files:
66
93
  - Rakefile
67
94
  - _config.yml
68
95
  - bin/spoonerize
69
- - lib/config/lazy_words.yml
70
96
  - lib/spoonerize.rb
71
97
  - lib/spoonerize/bumper.rb
72
98
  - lib/spoonerize/cli.rb
99
+ - lib/spoonerize/config.rb
73
100
  - lib/spoonerize/log.rb
74
101
  - lib/spoonerize/spoonerism.rb
75
102
  - lib/spoonerize/version.rb
@@ -82,7 +109,6 @@ metadata:
82
109
  homepage_uri: https://evanthegrayt.github.io/spoonerize/
83
110
  source_code_uri: https://github.com/evanthegrayt/spoonerize
84
111
  documentation_uri: https://evanthegrayt.github.io/spoonerize/
85
- post_install_message:
86
112
  rdoc_options: []
87
113
  require_paths:
88
114
  - lib
@@ -97,8 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
123
  - !ruby/object:Gem::Version
98
124
  version: '0'
99
125
  requirements: []
100
- rubygems_version: 3.2.3
101
- signing_key:
126
+ rubygems_version: 4.0.10
102
127
  specification_version: 4
103
128
  summary: Spoonerize phrases from the command line.
104
129
  test_files: []
@@ -1,22 +0,0 @@
1
- #=================================================================#
2
- # File: lazy_words.yml #
3
- # Description: Words to be excluded when "lazy" mode is enabled #
4
- # #
5
- # Author: Evan Gray #
6
- #=================================================================#
7
-
8
- - i
9
- - a
10
- - an
11
- - and
12
- - in
13
- - of
14
- - the
15
- - my
16
- - your
17
- - his
18
- - her
19
- - him
20
- - hers
21
- - to
22
- - is