madlibs 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1125a3c5ba72d78edcea246293d14d836d734924
4
- data.tar.gz: a0f62fba8126f03873912664f19342cd9bace812
3
+ metadata.gz: 50e1725db43dffc6104a7a07115b23f7f61cdefa
4
+ data.tar.gz: 684394e817d9405bcaf0d05b46716e4962a56f34
5
5
  SHA512:
6
- metadata.gz: b0c7095bd644e9aadd88641e892bf9100d0b4a2cb6987fa391f253b8a1e51373806f23f8d99b1a3d7f7f0f169c61d98f3c094c3c01a08d84833129dd8b9158e3
7
- data.tar.gz: fff04cdde7b252c459eb0320ce152fed065cf0497aece0337644dcb79b5d222f1cd11ebfef3e62456901bd47d7525fc3d7265eebdad79151ba6dd7ba82a209f0
6
+ metadata.gz: ced0191503cb29e9dd03b06c978b4b2807cd4c264ac40d671e8a3263b96329d4cd14b95d329ff8ffefde9deeffc98be29259c277205b2711cb73d572e174a363
7
+ data.tar.gz: 6cfe7a0a5d96f166bfbdb3b190895d1e66cf9e41d9a8e4b3c0d2330adf6a48bcb6b672bd060ba082712c3124c1d9e10576dd5b6cffba176ce1626778ce6416cc
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # Madlibs
2
2
 
3
- Sometimes you just need to make something silly on a whim. That's exactly
4
- what this gem is and does.
5
-
6
3
  This gem simply takes a template, does some randomizing, applies a dictionary,
7
4
  and produces a string. Depending on the dictionary, it can be quite humorous.
8
5
 
@@ -86,33 +83,30 @@ interchangeable words.
86
83
  ```rb
87
84
  require 'madlibs'
88
85
 
89
- # Create a new Madlib object, passing a template
90
- madlib = Madlibs::Madlib.new 'I will <verb> (the|another)( <adjective>)? <noun>'
91
-
92
- # Load your desired dictionary
93
- madlib.load({
94
-
95
- "nouns" => [
96
- "proxy",
97
- "cache",
98
- "architecture",
99
- "fragmentation",
100
- ],
101
-
102
- "verbs" => [
103
- "refactor",
104
- "implement",
105
- "replicate",
106
- "debug",
107
- ],
108
-
109
- "adjectives" => [
110
- "distributed",
111
- "imperative",
112
- "virtualized",
113
- ]
114
-
115
- })
86
+ # Create a new Madlib object, passing a template and a dictionary. You can also
87
+ # pass nothing and assign them after the fact with `.dictionary` and `.template`
88
+ madlib = Madlibs::Madlib.new(
89
+ 'I will <verb> (the|another)( <adjective>)? <noun>',
90
+ {
91
+ "nouns" => [
92
+ "proxy",
93
+ "cache",
94
+ "architecture",
95
+ "fragmentation",
96
+ ],
97
+ "verbs" => [
98
+ "refactor",
99
+ "implement",
100
+ "replicate",
101
+ "debug",
102
+ ],
103
+ "adjectives" => [
104
+ "distributed",
105
+ "imperative",
106
+ "virtualized",
107
+ ]
108
+ }
109
+ )
116
110
 
117
111
  # Generate your string(s)
118
112
 
@@ -0,0 +1,19 @@
1
+ module Madlibs
2
+ class Dictionary
3
+
4
+ def initialize hash
5
+ # deep clone to prevent side-effects
6
+ @hash = Marshal.load(Marshal.dump(hash))
7
+ @hash_clone = Marshal.load(Marshal.dump(hash))
8
+ end
9
+
10
+ def retrieve type
11
+ return '' if @hash[type].nil? or @hash[type].empty?
12
+ @hash[type].delete @hash[type].sample
13
+ end
14
+
15
+ def reload
16
+ @hash = Marshal.load(Marshal.dump(@hash_clone))
17
+ end
18
+ end
19
+ end
@@ -1,65 +1,30 @@
1
1
  require 'active_support/inflector'
2
+ require 'madlibs/template'
3
+ require 'madlibs/dictionary'
2
4
 
3
5
  module Madlibs
4
6
  class Madlib
7
+ attr_accessor :template
8
+ attr_reader :dictionary
5
9
 
6
- def initialize template, dictionary = nil
7
- @template = template
8
- unless dictionary.nil?
9
- @dictionary = dictionary.clone
10
- @original_dictionary = dictionary.clone
11
- end
10
+ def initialize template = '', dictionary_hash = {}
11
+ self.template = template
12
+ self.dictionary = dictionary_hash
12
13
  end
13
14
 
14
- def load dictionary
15
- @original_dictionary = dictionary.clone
16
- @dictionary = dictionary.clone
17
- end
18
-
19
- def reload
20
- @dictionary = @original_dictionary.clone
21
- end
22
-
23
- def retrieve type
24
- types = type.pluralize
25
- @dictionary[types].delete @dictionary[types].sample
26
- end
27
-
28
- def extract_keys template
29
- template.scan(/<([^>]*?)>/).flatten
30
- end
31
-
32
- def extract_word_lists template
33
- template.scan(/\([|a-zA-Z]*\)/)
15
+ def dictionary=(hash)
16
+ @dictionary = Dictionary.new hash
34
17
  end
35
18
 
36
19
  def generate
37
- reload
38
-
39
- product = @template.clone
40
-
41
- extract_word_lists(product).each do |words|
42
- product.gsub! words, words.gsub(/[()]/, '').split('|').sample
43
- end
44
-
45
- loop do
46
- optionals = product.scan(/\([^\(]*?\)(?=\?)/)
47
- break unless optionals.any?
48
- optionals.each do |o|
49
- if [true, false].sample
50
- used_string = o.gsub(/[()]/, '')
51
- product.sub! "#{o}?", used_string
52
- else
53
- product.sub! "#{o}?", ''
20
+ @dictionary.reload
21
+ Template.process_word_lists(
22
+ Template.process_optionals(
23
+ Template.keys(@template).reduce(@template) do |temp, key|
24
+ Template.process_key temp, key, @dictionary.retrieve(key.pluralize)
54
25
  end
55
- end
56
- end
57
-
58
- extract_keys(product).each do |key|
59
- product.sub! "<#{key}>", retrieve(key)
60
- end
61
-
62
- product
26
+ )
27
+ )
63
28
  end
64
29
  end
65
30
  end
@@ -0,0 +1,34 @@
1
+ module Madlibs
2
+ module Template
3
+
4
+ def self.keys template
5
+ template.scan(/<([^>]*?)>/).map do |key|
6
+ key.first
7
+ end
8
+ end
9
+
10
+ def self.process_key template, key, value
11
+ template.sub "<#{key}>", value
12
+ end
13
+
14
+ def self.process_word_lists template
15
+ template.scan(/\([|a-zA-Z]*\)/).reduce(template) do |temp, words|
16
+ temp.sub words, words.gsub(/[()]/, '').split('|').sample
17
+ end
18
+ end
19
+
20
+ def self.process_optionals template
21
+ loop do
22
+ optionals = template.scan(/\([^\(]*?\)(?=\?)/)
23
+ break if optionals.empty?
24
+ template = optionals.reduce(template) do |temp, opt|
25
+ temp.sub(
26
+ "#{opt}?",
27
+ if [true, false].sample then opt.gsub(/[()]/, '') else '' end
28
+ )
29
+ end
30
+ end
31
+ template
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module Madlibs
2
- VERSION = "1.0.1"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ["me@jondavidjohn.com"]
12
12
  spec.description = %q{Because sometimes, you just need to make something stupid.}
13
13
  spec.summary = %q{Create generated text with a combination of coditional templating and a provided dictionary}
14
- spec.homepage = ""
14
+ spec.homepage = "https://github.com/jondavidjohn/madlibs"
15
15
  spec.license = "Apache v2"
16
16
 
17
17
  spec.files = `git ls-files`.split($/)
@@ -0,0 +1,62 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'madlibs'
4
+
5
+ describe Madlibs::Dictionary do
6
+ describe '#retrieve' do
7
+ it 'should retrieve a random dictionary term in a given collection' do
8
+ hash = {
9
+ 'things' => [
10
+ 'thing_one',
11
+ 'thing_two',
12
+ ],
13
+ 'items' => [
14
+ 'item_one',
15
+ 'item_two',
16
+ 'item_three',
17
+ ]
18
+ }
19
+
20
+ dictionary = Madlibs::Dictionary.new(hash)
21
+ hash['things'].must_include dictionary.retrieve 'things'
22
+ hash['items'].must_include dictionary.retrieve 'items'
23
+ end
24
+
25
+ it 'should retrieve random dictionary items only once' do
26
+ dictionary = Madlibs::Dictionary.new({
27
+ 'items' => [
28
+ 'item_one',
29
+ 'item_two',
30
+ 'item_three',
31
+ ]
32
+ })
33
+
34
+ retrieved = []
35
+ retrieved.push dictionary.retrieve 'items'
36
+ retrieved.push dictionary.retrieve 'items'
37
+ retrieved.push dictionary.retrieve 'items'
38
+
39
+ retrieved.size.must_be_same_as retrieved.uniq.size
40
+ end
41
+
42
+ it 'should retrieve empty string from empty dictionary collection' do
43
+ dictionary = Madlibs::Dictionary.new({ 'items' => [ ] })
44
+ dictionary.retrieve('items').must_equal ''
45
+ end
46
+
47
+ it 'should not mutate the provided hash'do
48
+ hash = {
49
+ 'items' => [
50
+ 'item_one',
51
+ 'item_two',
52
+ 'item_three',
53
+ ]
54
+ }
55
+
56
+ dictionary = Madlibs::Dictionary.new(hash)
57
+ dictionary.retrieve 'items'
58
+ dictionary.retrieve 'items'
59
+ hash['items'].size.must_equal 3
60
+ end
61
+ end
62
+ end
@@ -3,66 +3,50 @@ require 'minitest/autorun'
3
3
  require 'madlibs'
4
4
 
5
5
  describe Madlibs::Madlib do
6
- def assert_block(test_description, &block)
7
- assert test_description, yield
8
- end
9
-
10
- before do
11
- @mad = Madlibs::Madlib.new '', {
12
- "things" => [
13
- "thing_one",
14
- "thing_two",
15
- "thing_three",
16
- ],
17
- "items" => [
18
- "item_one",
19
- "item_two",
20
- "item_three",
21
- ]
22
- }
23
- end
24
-
25
- it "should accurately extract simple <keys>" do
26
- keys = @mad.extract_keys('<simple> <keys>')
27
- keys.size.must_equal 2
28
- assert_block "correct keys extracted" do
29
- keys.include? 'simple' and keys.include? 'keys'
30
- end
31
- end
32
-
33
- it "should accurately extract <keys> with other templating entities around" do
34
- keys = @mad.extract_keys('<simple> (some|words) (<keys>)?')
35
- keys.size.must_equal 2
36
- assert_block "correct keys extracted" do
37
- keys.include? 'simple' and keys.include? 'keys'
38
- end
39
- end
40
-
41
- it "should accurately extract simple (word|lists)" do
42
- keys = @mad.extract_word_lists('(simple) (simple|word|lists)')
43
- keys.size.must_equal 2
44
- assert_block "correct lists extracted" do
45
- keys.include? '(simple)' and keys.include? '(simple|word|lists)'
6
+ describe 'generate' do
7
+ it 'should work' do
8
+ madlib = Madlibs::Madlib.new(
9
+ '(wow, )?such <noun>,( very <adjective>,)? so <verb>',
10
+ {
11
+ 'nouns' => [
12
+ 'desk',
13
+ 'lamp',
14
+ ],
15
+ 'adjectives' => [
16
+ 'neat',
17
+ 'fun',
18
+ ],
19
+ 'verbs' => [
20
+ 'commit',
21
+ 'scare',
22
+ ]
23
+ }
24
+ )
25
+
26
+ [ 'such lamp, very neat, so commit',
27
+ 'wow, such lamp, very fun, so commit',
28
+ 'wow, such desk, very neat, so scare',
29
+ 'such desk, so scare',
30
+ 'wow, such lamp, so scare',
31
+ 'wow, such desk, very neat, so commit',
32
+ 'wow, such desk, very fun, so commit',
33
+ 'such desk, very neat, so commit',
34
+ 'wow, such desk, very fun, so scare',
35
+ 'such lamp, very fun, so commit',
36
+ 'wow, such desk, so commit',
37
+ 'such lamp, so commit',
38
+ 'such desk, so commit',
39
+ 'such lamp, very fun, so scare',
40
+ 'wow, such desk, so scare',
41
+ 'wow, such lamp, so commit',
42
+ 'such lamp, very neat, so scare',
43
+ 'wow, such lamp, very fun, so scare',
44
+ 'such lamp, so scare',
45
+ 'wow, such lamp, very neat, so scare',
46
+ 'such desk, very fun, so scare',
47
+ 'such desk, very neat, so scare',
48
+ 'wow, such lamp, very neat, so commit',
49
+ 'such desk, very fun, so commit' ].must_include madlib.generate
46
50
  end
47
51
  end
48
-
49
- it "should accurately extract (word|lists) with other templating entities around" do
50
- keys = @mad.extract_word_lists('<simple> (some|words) (<keys>)? (to|extract)')
51
- keys.size.must_equal 2
52
- assert_block "correct keys extracted" do
53
- keys.include? '(some|words)' and keys.include? '(to|extract)'
54
- end
55
- end
56
-
57
- it "should retrieve random dictionary items only once" do
58
- retrieved = []
59
- retrieved.push @mad.retrieve 'item'
60
- retrieved.push @mad.retrieve 'item'
61
- retrieved.push @mad.retrieve 'item'
62
-
63
- retrieved.size.must_be_same_as retrieved.uniq.size
64
-
65
- @mad.retrieve('item').must_be_nil
66
- end
67
-
68
52
  end
@@ -0,0 +1,68 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'madlibs'
4
+
5
+ describe Madlibs::Template do
6
+ describe '#keys' do
7
+ it 'should extract simple keys' do
8
+ keys = Madlibs::Template.keys '<simple> <keys>'
9
+ keys.size.must_equal 2
10
+ keys.must_include 'simple'
11
+ keys.must_include 'keys'
12
+ end
13
+
14
+ it 'should extract keys from more complex templates' do
15
+ keys = Madlibs::Template.keys '<simple> (some|words) (<keys>)?'
16
+ keys.size.must_equal 2
17
+ keys.must_include 'simple'
18
+ keys.must_include 'keys'
19
+ end
20
+ end
21
+
22
+ describe '#process_key' do
23
+ it 'should put a value in place of a key' do
24
+ result = Madlibs::Template.process_key '<simple> <keys>', 'simple', 'hello'
25
+ result.must_equal 'hello <keys>'
26
+ end
27
+ end
28
+
29
+ describe '#process_word_lists' do
30
+ it 'should choose random words from word lists included in a template' do
31
+ result = Madlibs::Template.process_word_lists '(simple) (simple|word|lists)'
32
+ [ 'simple simple',
33
+ 'simple word',
34
+ 'simple lists' ].must_include result
35
+ end
36
+
37
+ it 'should choose random words from word lists included in a complex template' do
38
+ result = Madlibs::Template.process_word_lists '<simple> (some|words) (<keys>)? (to|extract)'
39
+ [ '<simple> some (<keys>)? to',
40
+ '<simple> some (<keys>)? extract',
41
+ '<simple> words (<keys>)? to',
42
+ '<simple> words (<keys>)? extract' ].must_include result
43
+ end
44
+ end
45
+
46
+ describe '#process_optionals' do
47
+ it 'should choose to randomly include optional elements' do
48
+ result = Madlibs::Template.process_optionals 'this is( really)?( very)? silly'
49
+ [ 'this is really very silly',
50
+ 'this is very silly',
51
+ 'this is really silly',
52
+ 'this is silly' ].must_include result
53
+ end
54
+
55
+ it 'should choose to randomly include optional elements in complex templates' do
56
+ result = Madlibs::Template.process_optionals '<simple> (some|words)( <keys>)? (to|extract)'
57
+ [ '<simple> (some|words) <keys> (to|extract)',
58
+ '<simple> (some|words) (to|extract)' ].must_include result
59
+ end
60
+
61
+ it 'should support the nesting of optionals' do
62
+ result = Madlibs::Template.process_optionals 'this is( very( freaking)?)? cool'
63
+ [ 'this is cool',
64
+ 'this is very cool',
65
+ 'this is very freaking cool' ].must_include result
66
+ end
67
+ end
68
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: madlibs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan D. Johnson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-16 00:00:00.000000000 Z
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -65,11 +65,15 @@ files:
65
65
  - README.md
66
66
  - Rakefile
67
67
  - lib/madlibs.rb
68
+ - lib/madlibs/dictionary.rb
68
69
  - lib/madlibs/madlib.rb
70
+ - lib/madlibs/template.rb
69
71
  - lib/madlibs/version.rb
70
72
  - madlibs.gemspec
73
+ - specs/dictionary_spec.rb
71
74
  - specs/madlib_spec.rb
72
- homepage: ''
75
+ - specs/template_spec.rb
76
+ homepage: https://github.com/jondavidjohn/madlibs
73
77
  licenses:
74
78
  - Apache v2
75
79
  metadata: {}