random-word 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "yard", "~> 0.6.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.6.2"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 OpenLogic, Peter Williams
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ random-word
2
+ ====
3
+
4
+ A random word generator intended for use in test data factories. This
5
+ library uses a large list (the wordnet corpus) of english words and
6
+ provides an enumerator interface to that corpus that will return the
7
+ words in a random order without repeats.
8
+
9
+ Contributing to random-word
10
+ ----
11
+
12
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
13
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
14
+ * Fork the project
15
+ * Start a feature/bugfix branch
16
+ * Commit and push until you are happy with your contribution
17
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
18
+ * Update history
19
+ * Update version (remember to use [semantic versioning][semver])
20
+ * Commit and push
21
+ * Send me a pull request. Bonus points for topic branches.
22
+
23
+ [semver]:http://semver.org/
24
+
25
+ Copyright
26
+ ----
27
+
28
+ Copyright (c) 2011 OpenLogic, Peter Williams. See LICENSE.txt for
29
+ further details.
30
+
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "random-word"
18
+ gem.homepage = "http://github.com/pezra/random-word"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Pick random words for factory data}
21
+ gem.description = %Q{A random word generator intended for use in test data factories. This library uses a large list (the wordnet corpus) of english words and provides an enumerator interface to that corpus that will return the words in a random order without repeats.}
22
+ gem.email = "pezra@barelyenough.org"
23
+ gem.authors = ["Peter Williams"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
Binary file
Binary file
@@ -0,0 +1 @@
1
+ require 'random_word'
@@ -0,0 +1,55 @@
1
+ require 'set'
2
+
3
+ module RandomWord
4
+ module EachRandomly
5
+ def each_randomly(&blk)
6
+ used = Set.new
7
+
8
+ while true
9
+ idx = next_unused_idx(used)
10
+ used << idx
11
+ yield at(idx)
12
+ end
13
+
14
+ rescue OutOfWords
15
+ # we are done.
16
+ end
17
+
18
+ private
19
+ def next_unused_idx(used)
20
+ idx = rand(length)
21
+ try = 1
22
+ while used.include?(idx)
23
+ raise OutOfWords if try > 1000
24
+ idx = rand(length)
25
+ try += 1
26
+ end
27
+
28
+ idx
29
+ end
30
+ class OutOfWords < Exception; end
31
+ end
32
+
33
+ class << self
34
+ def enumerator(word_list)
35
+ word_list.extend EachRandomly
36
+ Enumerator.new(word_list, :each_randomly)
37
+ end
38
+
39
+
40
+ def nouns
41
+ @nouns ||= enumerator(load_word_list("nouns.dat"))
42
+ end
43
+
44
+ def adjs
45
+ @adjs ||= enumerator(load_word_list("adjs.dat"))
46
+ end
47
+
48
+ protected
49
+ def load_word_list(name)
50
+ data_root = Pathname(File.dirname(__FILE__)) + "../data"
51
+ File.open(data_root + name){|f| Marshal.load(f)}
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "RandomWord" do
4
+ it "defines the module" do
5
+ defined?(RandomWord).should be_true
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path("spec_helper", File.dirname(__FILE__))
2
+
3
+ describe RandomWord, "enumerator" do
4
+ subject {RandomWord.enumerator(["aaa", "bbb", "ccc"])}
5
+
6
+ it "can get you the next word in it's list" do
7
+ subject.next.should be_one_of(["aaa", "bbb", "ccc"])
8
+ end
9
+
10
+ it "raises error when it runs out of words" do
11
+ 3.times{subject.next}
12
+
13
+ lambda{subject.next}.should raise_error(StopIteration)
14
+ end
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
20
+ already_received = []
21
+ 3.times do
22
+ (new_word = subject.next).should_not be_one_of(already_received)
23
+ already_received << new_word
24
+ end
25
+ end
26
+ end
27
+
28
+ describe RandomWord do
29
+ it "can return a random noun enumerator" do
30
+ RandomWord.nouns.should respond_to(:next)
31
+ end
32
+
33
+ it "can return a random adj enumerator" do
34
+ RandomWord.adjs.should respond_to(:next)
35
+ end
36
+
37
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'random-word'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :be_one_of do |expected|
2
+ match do |actual|
3
+ expected.any?{|exp_val| exp_val === actual}
4
+ end
5
+
6
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: random-word
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Peter Williams
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-23 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.3.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: yard
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: jeweler
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.2
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: rcov
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ description: A random word generator intended for use in test data factories. This library uses a large list (the wordnet corpus) of english words and provides an enumerator interface to that corpus that will return the words in a random order without repeats.
71
+ email: pezra@barelyenough.org
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - LICENSE.txt
78
+ - README.markdown
79
+ files:
80
+ - .document
81
+ - .rspec
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.markdown
85
+ - Rakefile
86
+ - VERSION
87
+ - data/adjs.dat
88
+ - data/nouns.dat
89
+ - lib/random-word.rb
90
+ - lib/random_word.rb
91
+ - spec/random-word_spec.rb
92
+ - spec/random_word_spec.rb
93
+ - spec/spec_helper.rb
94
+ - spec/support/matchers.rb
95
+ homepage: http://github.com/pezra/random-word
96
+ licenses:
97
+ - MIT
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 2610495265960102185
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.5
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Pick random words for factory data
125
+ test_files: []
126
+