poefy 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -3
- data/README.md +73 -34
- data/bin/poefy +44 -7
- data/bin/poefy_make +6 -8
- data/lib/poefy/core_extensions/array.rb +18 -0
- data/lib/poefy/database.rb +1 -3
- data/lib/poefy/db_type.rb +7 -14
- data/lib/poefy/exceptions.rb +123 -0
- data/lib/poefy/generation.rb +29 -11
- data/lib/poefy/poem_base.rb +112 -112
- data/lib/poefy/poetic_forms.rb +529 -367
- data/lib/poefy/string_manipulation.rb +0 -14
- data/lib/poefy/version.rb +2 -2
- data/lib/poefy.rb +1 -2
- data/settings.yml +2 -0
- data/spec/poefy_unit_spec.rb +455 -33
- metadata +4 -3
- data/lib/poefy/handle_error.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2836cb70692fce83aa7c647d66d5165b786c2a4c
|
4
|
+
data.tar.gz: 359f87bf030ca881585f696d6bad29cf442e143d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19f4e4e13177e712efce9919ad4f524a008c6cb257d81d39e3e43ad24b8397107d6265b06ed55da22ed7dafa8b010f4947a2e05251976f6206ff7c5c186be063
|
7
|
+
data.tar.gz: 9ef68d0b24110653b8d496be8f17618a5558697e426a024a78bb747b8943f2b137935fd1d456de81acd801b16c309166c0b935b917e4feb06e986dedb82bcffb
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,3 @@
|
|
1
|
-
__Warning: This project is undergoing serious revision at the moment, and parts of this readme are invalid.__
|
2
|
-
|
3
|
-
__For the correct documentation for the most recent gem version (0.6.1), please see the revision here:
|
4
|
-
[README.md](https://github.com/nossidge/poefy/blob/fb4e39a88296074075a8ad1708e60747f3323baf/README.md)__
|
5
|
-
|
6
|
-
__Version 1.0.0 is coming soon, with Postgres support as the major feature.__
|
7
|
-
|
8
|
-
|
9
1
|
# Poefy
|
10
2
|
|
11
3
|
by [Paul Thompson](https://tilde.town/~nossidge) - nossidge@gmail.com
|
@@ -17,6 +9,20 @@ Poems are created using a template to select lines from the database, according
|
|
17
9
|
I wrote this because I was banging my head against a wall trying to use [Tracery](https://github.com/galaxykate/tracery) to generate villanelles. Then I remembered that I know how to program computers. Lucky!
|
18
10
|
|
19
11
|
|
12
|
+
## Table of Contents
|
13
|
+
* [Installation](#installation)
|
14
|
+
* [Install the base gem](#install-the-base-gem)
|
15
|
+
* [Install a database gem](#install-a-database-gem)
|
16
|
+
* [PostgreSQL](#postgresql)
|
17
|
+
* [SQLite](#sqlite)
|
18
|
+
* [Set up some example corpora](#set-up-some-example-corpora)
|
19
|
+
* [Usage](#usage)
|
20
|
+
* [From the Command Line](#from-the-command-line)
|
21
|
+
* [As a Ruby Gem](#as-a-ruby-gem)
|
22
|
+
* [Some tips](#some-tips)
|
23
|
+
* [Sample output](#sample-output)
|
24
|
+
|
25
|
+
|
20
26
|
## Installation
|
21
27
|
|
22
28
|
### Install the base gem
|
@@ -33,11 +39,12 @@ You have two options when it comes to databases.
|
|
33
39
|
|
34
40
|
Install [PostgreSQL 9.0+](https://www.postgresql.org/download/)
|
35
41
|
|
36
|
-
Install the
|
42
|
+
Install the gem, and let poefy know that you will be using 'pg'.
|
37
43
|
|
38
44
|
$ gem install poefy-pg
|
45
|
+
$ poefy -D pg
|
39
46
|
|
40
|
-
|
47
|
+
In your Postgres instance, create a new user:
|
41
48
|
|
42
49
|
username: 'poefy'
|
43
50
|
password: 'poefy'
|
@@ -53,9 +60,12 @@ Everything is called 'poefy'. Nice and easy.
|
|
53
60
|
|
54
61
|
Install [SQLite 3.0+](https://sqlite.org/download.html)
|
55
62
|
|
56
|
-
|
63
|
+
Install the gem, and let poefy know that you will be using 'sqlite3'.
|
57
64
|
|
58
65
|
$ gem install poefy-sqlite3
|
66
|
+
$ poefy -D sqlite3
|
67
|
+
|
68
|
+
There is no further setup needed.
|
59
69
|
|
60
70
|
|
61
71
|
### Set up some example corpora
|
@@ -64,13 +74,6 @@ The repo comes with some initial text files included. To generate corpora for th
|
|
64
74
|
|
65
75
|
$ poefy_make
|
66
76
|
|
67
|
-
This command will also setup a `settings.yml` file in the gem root. By default it will be set to 'pg' or 'sqlite3', depending on which gem you installed. If you need to change that setting you can run either of the below:
|
68
|
-
|
69
|
-
$ poefy -D pg
|
70
|
-
$ poefy -D sqlite3
|
71
|
-
|
72
|
-
You can use the `-D` option by itself without the argument to see the current database setting.
|
73
|
-
|
74
77
|
|
75
78
|
## Usage
|
76
79
|
|
@@ -107,7 +110,7 @@ You then need to use the `-l` option when generating poems:
|
|
107
110
|
$ cd path/to
|
108
111
|
$ poefy -l eliot.db ballata
|
109
112
|
|
110
|
-
Note that using this option
|
113
|
+
Note that using this option you must use the whole file name, i.e. "coleridge.db" and not just "coleridge".
|
111
114
|
|
112
115
|
|
113
116
|
#### Option `-f` or `--form`
|
@@ -148,8 +151,8 @@ To view and amend these definitions, the code is in `lib/poefy/poetic_forms.rb`.
|
|
148
151
|
double_dactyl: {
|
149
152
|
rhyme: 'abcd efgd',
|
150
153
|
indent: '',
|
151
|
-
syllable: '
|
152
|
-
regex: '{7
|
154
|
+
syllable: '{0:6, 4m0:4}',
|
155
|
+
regex: '{7: ^\S+$}'
|
153
156
|
}
|
154
157
|
}
|
155
158
|
```
|
@@ -197,30 +200,64 @@ If the string is comma delimited, all lines will be any of those numbers of syll
|
|
197
200
|
|
198
201
|
$ poefy whitman sonnet -s'9,10,11'
|
199
202
|
|
203
|
+
The above can also be written to use a range format:
|
204
|
+
|
205
|
+
$ poefy whitman sonnet -s'9-11'
|
206
|
+
|
200
207
|
If the string is an array, each element corresponds to a line in the output. This will skip blank lines.
|
201
208
|
|
202
|
-
|
209
|
+
All of the below will generate limericks, with the latter ones more permissive than the first.
|
203
210
|
|
204
211
|
$ poefy whitman -r'aabba' -s'[8,8,5,5,8]'
|
205
212
|
$ poefy whitman -r'aabba' -s'[[8,9],[8,9],[4,5,6],[4,5,6],[8,9]]'
|
213
|
+
$ poefy whitman -r'aabba' -s'[8-9,8-9,4-6,4-6,8-9]'
|
206
214
|
|
207
|
-
If the string is a hash, the key will be used to match the line number.
|
215
|
+
If the string is a hash, the key will be used to match the line number. The underlying code that reads this uses YAML, but because the values will only be numbers we can be kinder with regards to key identifiers. So you can use `=>` or `:`, and they will be replaced with YAML's default `: ` (colon + space).
|
208
216
|
|
209
217
|
$ poefy whitman -r'aabba' -s'{1:8,2:8,3:5,4:5,5:8}'
|
210
218
|
$ poefy whitman -r'aabba' -s'{1:[8,9],2:[8,9],3:[4,5,6],4:[4,5,6],5:[8,9]}'
|
211
|
-
$ poefy whitman -r'aabba' -s'{
|
212
|
-
$ poefy whitman -r'aabba' -s'{0
|
213
|
-
|
214
|
-
In the hash form, any lines not explicitly specified will use the value of the '0' key. If there is no '0' key, the lines will be ignored.
|
219
|
+
$ poefy whitman -r'aabba' -s'{1:[8,9],2:[8,9],3:4-6,4:4-6,5:[8,9]}'
|
220
|
+
$ poefy whitman -r'aabba' -s'{0: [8,9],3: [4,5,6],4: 4-6}'
|
221
|
+
$ poefy whitman -r'aabba' -s'{0=>[8,9],3=>[4,5,6],4=>4-6}'
|
215
222
|
|
216
|
-
The below example will have 8 syllables for the
|
223
|
+
The below example will have 8 syllables for the 1st and 5th lines, but any number for the rest.
|
217
224
|
|
218
225
|
$ poefy whitman -r'aabba' -s'{1:8,5:8}'
|
219
226
|
|
227
|
+
Any lines not explicitly specified will use the value of the '0' key (zero). If there is no '0' key, the lines will follow no constraints.
|
228
|
+
|
229
|
+
The below example will have 8 syllables for the 1st and 5th lines, and 6 syllables for the rest.
|
230
|
+
|
231
|
+
$ poefy whitman -r'aabba' -s'{0:6,1:8,5:8}'
|
232
|
+
|
220
233
|
The key of the hash can take the form of negative numbers. In that case, they will refer to lines from the end of the poem. Any duplicated keys will be overwritten by the latest one.
|
221
234
|
|
222
235
|
$ poefy whitman -r'aabba' -s'{1:8,2:8,3:5,-2:5,-1:8}'
|
223
236
|
|
237
|
+
You can use the special keys 'o' and 'e' to specify odd or even lines. This will ignore empty lines between stanzas (spaces in the rhyme string). The below example will have 8 syllables for the 1st and 3rd lines, 6 syllables for the 2nd and 4th, 6 syllables for the 5th, and 4 syllables for the 6th.
|
238
|
+
|
239
|
+
$ poefy whitman -r'ad bd cd' -s'{o:8,e:6,-2:6,-1:4}'
|
240
|
+
|
241
|
+
A more advanced version of the above, you can use the letter 'm' to perform a [modulo operation][0] on the line number. This key takes the format `[divider]m[remainder]`. There's a few examples here, but it's not as complicated as it might at first seem.
|
242
|
+
|
243
|
+
[0]: https://en.wikipedia.org/wiki/Modulo_operation
|
244
|
+
|
245
|
+
If you have stanzas of 6 lines, to match the last line in each you can use the key `6m0`. This is because the line has a number of 6, 12, or 18, and those divide by 6 cleanly, leaving 0 as the remainder:
|
246
|
+
|
247
|
+
$ poefy whitman -r'abcdef abcdef abcdef' -s'{6m0:4}'
|
248
|
+
|
249
|
+
To match the first two lines, use `6m1` and `6m2`:
|
250
|
+
|
251
|
+
$ poefy whitman -r'abcdef abcdef abcdef' -s'{6m1:4, 6m2:8}'
|
252
|
+
|
253
|
+
This will give you stanzas of three lines, and longer syllable counts for the first line of each stanza:
|
254
|
+
|
255
|
+
$ poefy whitman -r'abb abb cdd cdd' -s'{0:4, 3m1:8}'
|
256
|
+
|
257
|
+
This will create a [double dactyl](https://en.wikipedia.org/wiki/Double_dactyl):
|
258
|
+
|
259
|
+
$ poefy whitman -r'abcd efgd' -s'{0:6, 4m0:4}' -x'{7: ^\S+$}'
|
260
|
+
|
224
261
|
Use zero `-s0` to specify no syllable matching.
|
225
262
|
|
226
263
|
|
@@ -230,14 +267,16 @@ Specify a regular expression for lines to follow.
|
|
230
267
|
|
231
268
|
If the string is just one regex, all lines will be forced to match that regex.
|
232
269
|
|
233
|
-
$ poefy whitman sonnet -x'^[A-Z]
|
234
|
-
$ poefy whitman sonnet -x'^[^e]*$'
|
270
|
+
$ poefy whitman sonnet -x'^[A-Z]'
|
271
|
+
$ poefy whitman sonnet -x'^[^e]*$'
|
235
272
|
|
236
|
-
If the string is a hash, the key will be
|
273
|
+
If the string is a hash, you can use the same key types as with `syllable` string. Again, it will be parsed as YAML, but here we are more concerned about string substitution messing up the regex. So you have to use `: ` as the key identifier. Also, you must put the regex inside `'single'` or `"double"` quotes.
|
237
274
|
|
238
275
|
Example, to ensure the first line always starts with capitalisation:
|
239
276
|
|
240
|
-
$ poefy whitman sonnet -x'{1
|
277
|
+
$ poefy whitman sonnet -x'{1: "^[A-Z]"}'
|
278
|
+
|
279
|
+
Unlike with the syllable option, you cannot use an array. You must use a single value or a hash. This is because the use of `[]` symbols in regular expressions confuses the parser, and I didn't want to forbid these common regex symbols. You can use hashes, but because of this you cannot use `{}` repetition matching in Poefy command line expressions. It's a pretty niche regex use though. You'll be fine without it.
|
241
280
|
|
242
281
|
Use a space `-x' '` to specify no regex matching.
|
243
282
|
|
@@ -431,8 +470,8 @@ puts poefy.poem ({ rhyme: 'abab cdcd efef gg', indent: '0101 0101 0011 01' })
|
|
431
470
|
puts poefy.poem ({ form: 'sonnet' })
|
432
471
|
puts poefy.poem ({ form: :sonnet, syllable: 0 })
|
433
472
|
puts poefy.poem ({ form: :sonnet, syllable: 10 })
|
434
|
-
puts poefy.poem ({ form: :sonnet, regex: /^[A-Z]
|
435
|
-
puts poefy.poem ({ form: :sonnet, regex: '^[A-Z]
|
473
|
+
puts poefy.poem ({ form: :sonnet, regex: /^[A-Z]/ })
|
474
|
+
puts poefy.poem ({ form: :sonnet, regex: '^[A-Z]' })
|
436
475
|
puts poefy.poem ({ form: :sonnet, acrostic: 'pauldpthompson' })
|
437
476
|
puts poefy.poem ({ form: 'sonnet', indent: '01010101001101' })
|
438
477
|
puts poefy.poem ({ form: 'sonnet', proper: false })
|
data/bin/poefy
CHANGED
@@ -23,6 +23,14 @@ def corpora
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
# Create a new Poefy::Poem object, and catch any exceptions thrown.
|
27
|
+
def poefy_new corpus, options = nil
|
28
|
+
Poefy::Poem.new corpus, options
|
29
|
+
rescue Poefy::DatabaseError => e
|
30
|
+
STDERR.puts e.console_msg
|
31
|
+
exit 1
|
32
|
+
end
|
33
|
+
|
26
34
|
################################################################################
|
27
35
|
|
28
36
|
def parse_options
|
@@ -60,6 +68,7 @@ def parse_options
|
|
60
68
|
poefy therese -r 'abab cdcd efef gg' -i '0101 0101 0011 01'
|
61
69
|
poefy whitman -r 'A1bA2 abA1 abA2 abA1 abA2 abA1A2'
|
62
70
|
poefy -Lc
|
71
|
+
poefy -f
|
63
72
|
].gsub(' ',' ')
|
64
73
|
|
65
74
|
list = 'Corpora: ' + corpora.join("\n ")
|
@@ -67,11 +76,15 @@ def parse_options
|
|
67
76
|
opts.banner = program_info + "\n" + usage + "\n" + list + "\n\n"
|
68
77
|
|
69
78
|
# These will be further validated within the class.
|
70
|
-
opts.on('-f', '--form STRING',
|
79
|
+
opts.on('-f', '--form [STRING]',
|
71
80
|
"A named poetic form for the output\n" +
|
72
81
|
' ' * 37 + "Specifies rhyme, indent and syllable\n" +
|
73
82
|
' ' * 37 + "One of:\n" +
|
74
83
|
' ' * 39 + forms_by_4.join("\n" + ' ' * 39)) do |s|
|
84
|
+
if s.nil?
|
85
|
+
puts forms
|
86
|
+
exit 0
|
87
|
+
end
|
75
88
|
options[:form] = s
|
76
89
|
end
|
77
90
|
opts.on('-r', '--rhyme STRING', "(See 'Description of rhyme string' below)") do |s|
|
@@ -107,8 +120,12 @@ def parse_options
|
|
107
120
|
' ' * 39 + "Defaults to ON -- Use this option to DISABLE") do
|
108
121
|
options[:proper] = false
|
109
122
|
end
|
123
|
+
opts.on('-c', '--capital',
|
124
|
+
"Capitalise the first letter of each line") do
|
125
|
+
options[:transform] = proc { |line| line.capitalize }
|
126
|
+
end
|
110
127
|
|
111
|
-
#
|
128
|
+
# Poem output options.
|
112
129
|
opts.separator nil
|
113
130
|
opts.on('-n', '--number INTEGER',
|
114
131
|
"Number of poems to generate") do |n|
|
@@ -212,7 +229,14 @@ options[:form] = second_arg if second_arg != ''
|
|
212
229
|
# If we need to make a corpus.
|
213
230
|
# Exit the program after corpus is generated.
|
214
231
|
if options[:make_corpus]
|
215
|
-
|
232
|
+
|
233
|
+
# It's okay if there's an error with an existing database, because
|
234
|
+
# we're creating a new one. So we can swallow any errors here.
|
235
|
+
begin
|
236
|
+
poefy = Poefy::Poem.new first_arg
|
237
|
+
rescue Poefy::DatabaseError
|
238
|
+
end
|
239
|
+
|
216
240
|
if data
|
217
241
|
poefy.make_database data, options[:corpus_desc], true
|
218
242
|
poefy.close
|
@@ -227,7 +251,7 @@ end
|
|
227
251
|
# If we need to update a corpus description.
|
228
252
|
# Exit the program after corpus is generated.
|
229
253
|
if options[:corpus_desc]
|
230
|
-
poefy =
|
254
|
+
poefy = poefy_new first_arg
|
231
255
|
begin
|
232
256
|
poefy.corpus.desc = options[:corpus_desc]
|
233
257
|
poefy.close
|
@@ -241,7 +265,7 @@ end
|
|
241
265
|
# If the second argument is 'rhyme', then output all
|
242
266
|
# lines that rhyme with the word.
|
243
267
|
if second_arg == 'rhyme'
|
244
|
-
poefy =
|
268
|
+
poefy = poefy_new first_arg
|
245
269
|
third_arg = (ARGV.length > 2) ? ARGV[2] : nil
|
246
270
|
fourth_arg = (ARGV.length > 3) ? ARGV[3] : nil
|
247
271
|
puts poefy.corpus.rhymes(third_arg, fourth_arg)
|
@@ -255,12 +279,25 @@ if data or File.exists?(second_arg)
|
|
255
279
|
end
|
256
280
|
|
257
281
|
# Create poefy object using the options.
|
258
|
-
|
282
|
+
begin
|
283
|
+
poefy = poefy_new first_arg, options
|
284
|
+
rescue Poefy::DatabaseError => e
|
285
|
+
STDERR.puts e.console_msg
|
286
|
+
exit 1
|
287
|
+
end
|
259
288
|
|
260
289
|
# Make the correct number of poems, and output them.
|
261
290
|
number = options[:number] || 1
|
262
291
|
number.times do |i|
|
263
|
-
|
292
|
+
|
293
|
+
# Exit the program if a Poefy error is raised.
|
294
|
+
begin
|
295
|
+
poem = poefy.poem
|
296
|
+
rescue Poefy::Error => e
|
297
|
+
STDERR.puts e.console_msg
|
298
|
+
exit 1
|
299
|
+
end
|
300
|
+
|
264
301
|
if poem
|
265
302
|
puts poem
|
266
303
|
puts nil if i < number - 1
|
data/bin/poefy_make
CHANGED
@@ -15,6 +15,8 @@ Poefy.console = true
|
|
15
15
|
# Determine which database interface to use, based on the gems installed.
|
16
16
|
# Attempt to load exactly one of the below files.
|
17
17
|
# Array is ordered by priority, so use PostgreSQL before SQLite.
|
18
|
+
# (This code shouldn't really be necessary, unless the user has explicitly
|
19
|
+
# deleted their settings file.)
|
18
20
|
def determine_database_interface
|
19
21
|
loaded_file = nil
|
20
22
|
[
|
@@ -22,21 +24,17 @@ def determine_database_interface
|
|
22
24
|
'poefy/sqlite3'
|
23
25
|
].each do |file|
|
24
26
|
begin
|
25
|
-
|
27
|
+
require file
|
26
28
|
loaded_file = File.basename(file)
|
27
29
|
break
|
28
30
|
rescue LoadError
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
|
-
# Exit
|
34
|
+
# Exit the program if no file loaded.
|
33
35
|
if loaded_file.nil?
|
34
|
-
|
35
|
-
|
36
|
-
"\n by default; you must install one of the below gems:" +
|
37
|
-
"\n gem install poefy-sqlite3" +
|
38
|
-
"\n gem install poefy-pg"
|
39
|
-
STDERR.puts msg
|
36
|
+
e = Poefy::MissingDBInterface.new
|
37
|
+
STDERR.puts e.console_msg
|
40
38
|
exit 1
|
41
39
|
end
|
42
40
|
|
@@ -13,6 +13,8 @@ module Poefy
|
|
13
13
|
module Array
|
14
14
|
module SortByDistance
|
15
15
|
end
|
16
|
+
module ModuloIndex
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
@@ -93,11 +95,27 @@ module Poefy::CoreExtensions::Array::SortByDistance
|
|
93
95
|
end
|
94
96
|
end
|
95
97
|
|
98
|
+
#--
|
99
|
+
# Define module methods.
|
100
|
+
#++
|
101
|
+
module Poefy::CoreExtensions::Array::ModuloIndex
|
102
|
+
|
103
|
+
##
|
104
|
+
# Return elements located at specific index periods.
|
105
|
+
#
|
106
|
+
def modulo_index(divider, remainder = 0, start_index = 0)
|
107
|
+
self.values_at(* self.each_index.select do |i|
|
108
|
+
(i + start_index) % divider == remainder
|
109
|
+
end)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
96
113
|
#--
|
97
114
|
# Extend Array class.
|
98
115
|
#++
|
99
116
|
class Array
|
100
117
|
include Poefy::CoreExtensions::Array::SortByDistance
|
118
|
+
include Poefy::CoreExtensions::Array::ModuloIndex
|
101
119
|
end
|
102
120
|
|
103
121
|
################################################################################
|
data/lib/poefy/database.rb
CHANGED
@@ -7,7 +7,6 @@
|
|
7
7
|
################################################################################
|
8
8
|
|
9
9
|
require_relative 'string_manipulation.rb'
|
10
|
-
require_relative 'handle_error.rb'
|
11
10
|
|
12
11
|
################################################################################
|
13
12
|
|
@@ -16,7 +15,6 @@ module Poefy
|
|
16
15
|
class Database
|
17
16
|
|
18
17
|
include Poefy::StringManipulation
|
19
|
-
include Poefy::HandleError
|
20
18
|
|
21
19
|
attr_reader :name, :local
|
22
20
|
|
@@ -49,7 +47,7 @@ module Poefy
|
|
49
47
|
create_sprocs
|
50
48
|
rescue
|
51
49
|
@db = nil
|
52
|
-
|
50
|
+
raise Poefy::StructureInvalid
|
53
51
|
end
|
54
52
|
end
|
55
53
|
@db
|
data/lib/poefy/db_type.rb
CHANGED
@@ -27,35 +27,28 @@ module Poefy
|
|
27
27
|
hsh = {'database' => db_name}
|
28
28
|
file.write hsh.to_yaml
|
29
29
|
end
|
30
|
+
@@database_type = nil
|
30
31
|
end
|
31
32
|
def self.database_type create_file = true
|
33
|
+
@@database_type ||= nil
|
34
|
+
return @@database_type if @@database_type
|
32
35
|
settings_file = Poefy.root + '/settings.yml'
|
33
36
|
if not File.exists?(settings_file)
|
34
37
|
return nil if !create_file
|
35
38
|
Poefy.database_type = 'pg'
|
36
39
|
end
|
37
|
-
YAML::load_file(settings_file)['database']
|
40
|
+
@@database_type = YAML::load_file(settings_file)['database']
|
38
41
|
end
|
39
42
|
|
40
43
|
# Requires the chosen database interface gem.
|
41
44
|
def self.require_db db_interface_gem = nil
|
42
45
|
begin
|
46
|
+
@@database_type = db_interface_gem if db_interface_gem
|
43
47
|
require 'poefy/' + (db_interface_gem || Poefy.database_type)
|
44
48
|
|
45
|
-
#
|
49
|
+
# Replace with custom exception.
|
46
50
|
rescue LoadError
|
47
|
-
|
48
|
-
msg = "ERROR: Please specify the type of database to use." +
|
49
|
-
"\n The 'poefy' gem does not implement a database interface" +
|
50
|
-
"\n by default; you must install one of the below gems:" +
|
51
|
-
"\n gem install poefy-sqlite3" +
|
52
|
-
"\n gem install poefy-pg"
|
53
|
-
if Poefy.console
|
54
|
-
STDERR.puts msg
|
55
|
-
exit 1
|
56
|
-
end
|
57
|
-
raise msg
|
58
|
-
end
|
51
|
+
raise Poefy::MissingDBInterface
|
59
52
|
end
|
60
53
|
end
|
61
54
|
|
@@ -0,0 +1,123 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Encoding: UTF-8
|
3
|
+
|
4
|
+
################################################################################
|
5
|
+
# Poefy exception hierarchy.
|
6
|
+
# These errors have short messages for developers using the gem, and longer
|
7
|
+
# messages for end users who will be using the bin through the console.
|
8
|
+
# If accessing through a console, all these errors are cause to exit.
|
9
|
+
################################################################################
|
10
|
+
|
11
|
+
module Poefy
|
12
|
+
|
13
|
+
class Error < StandardError
|
14
|
+
def console_msg
|
15
|
+
"ERROR: " + msg
|
16
|
+
end
|
17
|
+
def initialize short_message, console_message = nil
|
18
|
+
super short_message
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
##############################################################################
|
23
|
+
|
24
|
+
class InputError < Error
|
25
|
+
end
|
26
|
+
|
27
|
+
class MissingFormOrRhyme < InputError
|
28
|
+
def msg
|
29
|
+
"No valid rhyme or form input specified"
|
30
|
+
end
|
31
|
+
def console_msg
|
32
|
+
"ERROR: No valid rhyme or form option specified." +
|
33
|
+
"\n Try again using the -f or -r option." +
|
34
|
+
"\n Use -h or --help to view valid forms."
|
35
|
+
end
|
36
|
+
def initialize
|
37
|
+
super msg, console_msg
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class RhymeError < InputError
|
42
|
+
def msg
|
43
|
+
"Rhyme input is not valid"
|
44
|
+
end
|
45
|
+
def initialize
|
46
|
+
super msg
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class SyllableError < InputError
|
51
|
+
def msg
|
52
|
+
"Syllable input is not valid"
|
53
|
+
end
|
54
|
+
def initialize short_message = msg
|
55
|
+
super short_message
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class RegexError < InputError
|
60
|
+
def msg
|
61
|
+
"Regular expression input is not valid"
|
62
|
+
end
|
63
|
+
def initialize short_message = msg
|
64
|
+
super short_message
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
##############################################################################
|
69
|
+
|
70
|
+
class GenerationError < Error
|
71
|
+
end
|
72
|
+
|
73
|
+
class NotEnoughData < GenerationError
|
74
|
+
def msg
|
75
|
+
"Not enough rhyming lines in the input"
|
76
|
+
end
|
77
|
+
def initialize console_message = nil
|
78
|
+
super msg, console_message
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
##############################################################################
|
83
|
+
|
84
|
+
class DatabaseError < Error
|
85
|
+
end
|
86
|
+
|
87
|
+
class MissingDatabase < DatabaseError
|
88
|
+
def msg
|
89
|
+
"Database does not exist"
|
90
|
+
end
|
91
|
+
def initialize
|
92
|
+
super msg
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class StructureInvalid < DatabaseError
|
97
|
+
def msg
|
98
|
+
"Database contains invalid structure"
|
99
|
+
end
|
100
|
+
def initialize console_message = nil
|
101
|
+
super msg, console_message
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
class MissingDBInterface < DatabaseError
|
106
|
+
def msg
|
107
|
+
"Database interface not specified"
|
108
|
+
end
|
109
|
+
def console_msg
|
110
|
+
"ERROR: Please specify the type of database to use." +
|
111
|
+
"\n poefy does not implement a database interface by" +
|
112
|
+
"\n default; you must install one of the below gems:" +
|
113
|
+
"\n gem install poefy-sqlite3" +
|
114
|
+
"\n gem install poefy-pg"
|
115
|
+
end
|
116
|
+
def initialize
|
117
|
+
super msg, console_msg
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
################################################################################
|