random-word 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ 1.3
2
+ ----
3
+ * Added exclude patterns. This makes it easy to prevent words with certain patterns from being returned.
4
+
1
5
  1.2
2
6
  ----
3
7
  * random phrase enumerator
@@ -6,30 +6,51 @@ library uses a large list (the wordnet corpus) of english words and
6
6
  provides an enumerator interface to that corpus that will return the
7
7
  words in a random order without repeats.
8
8
 
9
- Examples
9
+ Usage
10
10
  ----
11
11
 
12
- ### General use
12
+ You can get a random word any where you need one. Just request the
13
+ next of which ever word flavor you prefer.
13
14
 
14
- Random.adjs.next #=> "pugnacious"
15
- Random.nouns.next #=> "audience"
15
+ RandomWord.adjs.next #=> "pugnacious"
16
+ RandomWord.nouns.next #=> "audience"
16
17
 
17
18
  ### Factory Girl
18
19
 
20
+ This library was first developed to use in factories. It can be used
21
+ with Factory Girl like this.
22
+
19
23
  Factory.define(:user) do |u|
20
24
  u.name "#{RandomWord.adjs.next} User"
21
25
  u.email {|u| "#{u.name.gsub(/ +/, '.')}@example.com"
22
26
  end
23
27
 
24
- Factory(:user) #=>
28
+ Factory(:user) #=> ...
25
29
 
26
30
  ### Machinist
27
31
 
32
+
33
+ For Machinist a `#sw` (short for serial word) method is provided. It works exactly like `#sn`
34
+ but it returns a string instead of a number.
35
+
28
36
  User.blueprint do
29
37
  name { "#{sw.capitalize} User" }
30
38
  email { "#{sw}.user@example.com" }
31
39
  end
32
40
 
41
+ Exclusion
42
+ ----
43
+
44
+ Words may be excluded by pattern, or exact match. To do this just add
45
+ an object that responds to `#===` to the exclude list.
46
+
47
+ RandomWord.exclude_list << /fo+/
48
+ RandomWord.exclude_list << 'bar'
49
+
50
+ This will prevent the return of the exact string `"bar"` and any word
51
+ which matches the regex `/fo+/`.
52
+
53
+
33
54
  Contributing to random-word
34
55
  ----
35
56
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.3.0
@@ -8,13 +8,17 @@ require 'pathname'
8
8
  #
9
9
  module RandomWord
10
10
  module EachRandomly
11
+ attr_accessor :random_word_exclude_list
12
+
11
13
  def each_randomly(&blk)
12
14
  used = Set.new
13
-
15
+ exclude_list = Array(@random_word_exclude_list)
14
16
  while true
15
17
  idx = next_unused_idx(used)
16
18
  used << idx
17
- yield at(idx)
19
+ word = at(idx)
20
+ next if exclude_list.any?{|r| r === word }
21
+ yield word
18
22
  end
19
23
 
20
24
  rescue OutOfWords
@@ -37,15 +41,18 @@ module RandomWord
37
41
  end
38
42
 
39
43
  class << self
44
+ def exclude_list
45
+ @exclude_list ||= []
46
+ end
40
47
 
41
48
  # @return [Enumerator] Random noun enumerator
42
49
  def nouns
43
- @nouns ||= enumerator(load_word_list("nouns.dat"))
50
+ @nouns ||= enumerator(load_word_list("nouns.dat"), exclude_list)
44
51
  end
45
52
 
46
53
  # @return [Enumerator] Random adjective enumerator
47
54
  def adjs
48
- @adjs ||= enumerator(load_word_list("adjs.dat"))
55
+ @adjs ||= enumerator(load_word_list("adjs.dat"), exclude_list)
49
56
  end
50
57
 
51
58
  # @return [Enumerator] Random phrase enumerator
@@ -61,9 +68,10 @@ module RandomWord
61
68
 
62
69
  # Create a random, non-repeating enumerator for a list of words
63
70
  # (or anything really).
64
- def enumerator(word_list)
71
+ def enumerator(word_list, list_of_regexs_or_strings_to_exclude = [])
65
72
  word_list.extend EachRandomly
66
- Enumerator.new(word_list, :each_randomly)
73
+ word_list.random_word_exclude_list = list_of_regexs_or_strings_to_exclude
74
+ word_list.enum_for(:each_randomly)
67
75
  end
68
76
 
69
77
  protected
@@ -3,7 +3,7 @@ require File.expand_path("spec_helper", File.dirname(__FILE__))
3
3
  describe RandomWord, "enumerator" do
4
4
  subject {RandomWord.enumerator(["aaa", "bbb", "ccc"])}
5
5
 
6
- it "can get you the next word in it's list" do
6
+ it "can get you the next word in its list" do
7
7
  subject.next.should be_one_of(["aaa", "bbb", "ccc"])
8
8
  end
9
9
 
@@ -13,10 +13,7 @@ describe RandomWord, "enumerator" do
13
13
  lambda{subject.next}.should raise_error(StopIteration)
14
14
  end
15
15
 
16
- # This test might pass sometimes even if the code it wrong. It if
17
- # ever fails it is a serious issue and this test should be run
18
- # multiple times before deciding the issue has been fixed.
19
- it "should only return a word one time" do
16
+ it "make sure each word is only returned once" do
20
17
  already_received = []
21
18
  3.times do
22
19
  (new_word = subject.next).should_not be_one_of(already_received)
@@ -26,6 +23,10 @@ describe RandomWord, "enumerator" do
26
23
  end
27
24
 
28
25
  describe RandomWord do
26
+ after(:all) do
27
+ RandomWord.instance_eval{ @nouns, @adjs = nil, nil } # reset rspec effects
28
+ end
29
+
29
30
  it "can return a random noun enumerator" do
30
31
  RandomWord.nouns.should respond_to(:next)
31
32
  end
@@ -38,3 +39,54 @@ describe RandomWord do
38
39
  RandomWord.phrases.next.should be_a(String)
39
40
  end
40
41
  end
42
+
43
+ describe RandomWord, "#exclude" do
44
+ let(:word_list) { ["aaa","ccc","c", "cab", "abc", "ace", "dad"] }
45
+
46
+ [
47
+ {:name => "normal words", :exclude => "ccc", :expected => Set.new(["aaa","c", "cab", "abc", "ace", "dad"])},
48
+ {:name => "regex", :exclude => /c/, :expected => Set.new(["aaa", "dad"])},
49
+ {:name => "list", :exclude => [/c/,/d/], :expected => Set.new(["aaa"])},
50
+ ].each do |rec|
51
+ it "will not return an excluded #{rec[:name]}" do
52
+ subject = RandomWord.enumerator(word_list, rec[:exclude])
53
+
54
+ received_words = []
55
+ loop do
56
+ received_words << subject.next
57
+ end rescue StopIteration
58
+
59
+ Set.new(received_words).should == rec[:expected]
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ describe "RandomWord#nouns", "with exclusions" do
66
+
67
+ subject{ RandomWord.nouns }
68
+
69
+ before(:each) do
70
+ RandomWord.should_receive(:load_word_list).and_return(["aaa","bbb", "ccc"])
71
+ end
72
+
73
+ after(:each) do
74
+ RandomWord.exclude_list.clear
75
+ RandomWord.instance_eval{ @nouns, @adjs = nil, nil } # reset rspec effects
76
+ end
77
+
78
+ it "will not return an excluded word" do
79
+ RandomWord.exclude_list << "ccc"
80
+
81
+ received_words = []
82
+ loop do
83
+ received_words << subject.next
84
+ end
85
+
86
+ received_words.should_not include "ccc"
87
+ received_words.should include "aaa"
88
+ received_words.should include "bbb"
89
+ end
90
+
91
+ end
92
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random-word
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-02 00:00:00.000000000 -06:00
12
+ date: 2012-01-12 00:00:00.000000000 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &2158885620 !ruby/object:Gem::Requirement
17
+ requirement: &2155118380 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 2.3.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2158885620
25
+ version_requirements: *2155118380
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: yard
28
- requirement: &2158885140 !ruby/object:Gem::Requirement
28
+ requirement: &2155117900 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 0.6.0
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2158885140
36
+ version_requirements: *2155117900
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bundler
39
- requirement: &2158884660 !ruby/object:Gem::Requirement
39
+ requirement: &2155117420 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 1.0.0
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2158884660
47
+ version_requirements: *2155117420
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: jeweler
50
- requirement: &2158884180 !ruby/object:Gem::Requirement
50
+ requirement: &2155116940 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.6.2
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2158884180
58
+ version_requirements: *2155116940
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rcov
61
- requirement: &2158883700 !ruby/object:Gem::Requirement
61
+ requirement: &2155116460 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2158883700
69
+ version_requirements: *2155116460
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: machinist
72
- requirement: &2158883220 !ruby/object:Gem::Requirement
72
+ requirement: &2155115980 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 2.0.0.beta2
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2158883220
80
+ version_requirements: *2155115980
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: activesupport
83
- requirement: &2158882740 !ruby/object:Gem::Requirement
83
+ requirement: &2155115500 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *2158882740
91
+ version_requirements: *2155115500
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: i18n
94
- requirement: &2158882260 !ruby/object:Gem::Requirement
94
+ requirement: &2155115020 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,7 +99,7 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *2158882260
102
+ version_requirements: *2155115020
103
103
  description: A random word generator intended for use in test data factories. This
104
104
  library uses a large list (the wordnet corpus) of english words and provides an
105
105
  enumerator interface to that corpus that will return the words in a random order
@@ -145,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  segments:
147
147
  - 0
148
- hash: -256185191954845220
148
+ hash: 2056485225028355172
149
149
  required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  none: false
151
151
  requirements: