random_phrase 0.0.2 → 0.1.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.
data/.rvmrc ADDED
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory.
5
+ # For more info, see: http://beginrescueend.com/
6
+
7
+ ruby_gemset_name="ruby-1.9.2-p180@random_phrase"
8
+ ruby_name="ruby-1.9.2-p180"
9
+
10
+ echo "RVM: Using ${ruby_gemset_name} for this project."
11
+ # Ensure we have the right ruby installed to use this project.
12
+ if rvm list strings | grep -q "${ruby_name}" ; then
13
+
14
+ # Load or create the specified environment
15
+ # We attempt to load the desired environment directly from the environment
16
+ # file. This is very fast and efficient compared to running through the entire
17
+ # CLI and selector. If you want feedback on which environment was used then
18
+ # insert the word 'use' after --create as this triggers verbose mode.
19
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
20
+ && -s "${rvm_path:-$HOME/.rvm}/environments/${ruby_gemset_name}" ]] ; then
21
+ \. "${rvm_path:-$HOME/.rvm}/environments/${ruby_gemset_name}"
22
+ else
23
+ # If the environment file has not yet been created, use the RVM CLI to select.
24
+ if ! rvm --create "${ruby_gemset_name}"
25
+ then
26
+ echo "RVM: Failed to create environment '$environment_id'."
27
+ fi
28
+
29
+ fi
30
+
31
+ # Ensure that Bundler is installed, install it if it is not.
32
+ bundler_installed=`gem list bundler | grep bundler`
33
+ if [ -z "$bundler_installed" ]; then
34
+ gem install bundler
35
+ fi
36
+
37
+ # Bundle while reducing excess noise.
38
+ # We generate bin stubs so that we don't need to use
39
+ # 'bundle exec' before gem executables. You should add
40
+ # './bin' to your path so that you can run 'rake', 'cucumber'
41
+ # etc from the Gemfile with ease.
42
+ # See http://yehudakatz.com/2011/05/30/gem-versioning-and-bundler-doing-it-right/
43
+ echo "RVM: Ensuring your bundle is up to date."
44
+ bundle install | grep -v 'Using' | grep -v 'complete' | sed '/^$/d'
45
+
46
+ else
47
+
48
+ # Notify the user to install the desired interpreter before proceeding.
49
+ echo "RVM: ${ruby_name} was not found, please run 'rvm install ${ruby_name}' and then cd back into the project directory."
50
+
51
+ fi
data/lib/random_phrase.rb CHANGED
@@ -1,15 +1,40 @@
1
1
  require "random_phrase/version"
2
2
  require "random_phrase/dictionary"
3
- require "random_phrase/core_ext/array"
4
3
 
5
4
  module RandomPhrase
6
5
 
7
- def self.phrase(word_count = 1)
8
- phrase = []
9
- word_count.times do
10
- phrase << RandomPhrase::Dictionary.words.pick
6
+ class << self
7
+ def dictionary=(dictionary)
8
+ @@dictionary = dictionary
9
+ end
10
+
11
+ def dictionary
12
+ @@dictionary ||= RandomPhrase::Dictionary.new
13
+ end
14
+
15
+ def phrase(*args)
16
+ options = args.extract_options!
17
+ word_patterns = []
18
+
19
+ unless args.empty?
20
+ if args.first.respond_to?(:times)
21
+ word_count = args.first
22
+ elsif args.first.kind_of?(Regexp)
23
+ word_patterns = args.first.source.split(/(?:\s|\\\\s)/)
24
+ word_count = word_patterns.count
25
+ elsif args.first.respond_to?(:to_s)
26
+ word_count = args.first.to_s.split(/\s/).count
27
+ end
28
+ end
29
+
30
+ word_count ||= 1
31
+ phrase = []
32
+ word_count.times do |i|
33
+ options.merge!(:pattern => Regexp.compile(word_patterns[i])) unless word_patterns.empty?
34
+ phrase << dictionary.words(options).sample
35
+ end
36
+ phrase.join(" ")
11
37
  end
12
- phrase.join(" ")
13
38
  end
14
39
 
15
40
  end
@@ -3,4 +3,24 @@ how
3
3
  well
4
4
  this
5
5
  gem
6
- works
6
+ works
7
+ a
8
+ ah
9
+ are
10
+ aunt
11
+ angst
12
+ apples
13
+ aspects
14
+ audience
15
+ amplitude
16
+ auspicious
17
+ i
18
+ in
19
+ ink
20
+ ides
21
+ inset
22
+ ignore
23
+ ideally
24
+ implicit
25
+ illicitly
26
+ introspect
@@ -1,14 +1,38 @@
1
- module RandomPhrase::Dictionary
2
- def self.load
3
- if File.exists?("config/words")
4
- File.read("config/words").split
5
- else
6
- "flowing hill".split
7
- end
8
- end
1
+ require "active_support/core_ext/array/extract_options"
9
2
 
10
- def self.words
11
- @@dictionary ||= self.load
3
+ class RandomPhrase::Dictionary
4
+ attr_accessor :loader
5
+
6
+ module DefaultLoader
7
+ def self.load
8
+ if File.exists?("config/words")
9
+ File.read("config/words").split
10
+ else
11
+ "flowing river down brightly sparkling in the sun may I dip my toes".split
12
+ end
13
+ end
12
14
  end
13
15
 
16
+ def initialize(loader = nil)
17
+ loader = Proc.new {loader} unless loader.nil? || loader.kind_of?(Proc)
18
+ self.loader = loader ||= Proc.new { DefaultLoader.load }
19
+ end
20
+
21
+
22
+ def words(*args)
23
+ options = args.extract_options!
24
+
25
+ @dictionary ||= loader.call().group_by {|word| word.length}
26
+ result = @dictionary.values.flatten
27
+ if options.key?(:length)
28
+ result = @dictionary.fetch(options[:length], [])
29
+ end
30
+
31
+ if options.key?(:pattern)
32
+ result = result.select { |w| w =~ Regexp.try_convert(options[:pattern]) }
33
+ end
34
+
35
+ result.flatten
36
+ end
37
+
14
38
  end
@@ -1,3 +1,3 @@
1
1
  module RandomPhrase
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ["yosem.sweet@suite101.com"]
10
10
  s.homepage = "https://github.com/yosemsweet/random_phrase"
11
11
  s.summary = %q{Generate a simple random phrase}
12
- s.description = %q{This gem provides random phrase functionality and extends Array with a random .pick}
12
+ s.description = %q{This gem provides the ability to generate a random phrase from a customizable word list via word length and regexp matching.}
13
13
 
14
14
  s.rubyforge_project = "random_phrase"
15
15
 
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  # specify any dependencies here; for example:
22
- s.add_development_dependency "rspec"
23
- # s.add_runtime_dependency "rest-client"
22
+ s.add_dependency "activesupport", "~>3"
23
+ s.add_development_dependency "require_all"
24
+ s.add_development_dependency "ruby-debug19"
25
+ s.add_development_dependency "rspec", "~>2"
24
26
  end
@@ -1,20 +1,58 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe RandomPhrase::Dictionary do
4
- context "module methods" do
5
- context "::load" do
6
- it "should respond to load" do
7
- RandomPhrase::Dictionary.should respond_to(:load)
4
+ context "class methods" do
5
+
6
+ context "::new" do
7
+ it "returns a dictionary object" do
8
+ RandomPhrase::Dictionary.new.should be_kind_of(RandomPhrase::Dictionary)
9
+ end
10
+
11
+ it "can be passed a block that defines the implementation of #load" do
12
+ load_static = Proc.new { ["a","b","c","d"]}
13
+ RandomPhrase::Dictionary.new(load_static).words.should == ["a","b","c","d"]
8
14
  end
9
15
  end
10
-
11
- context "::word" do
16
+ end
17
+
18
+ context "object methods" do
19
+ context "#words" do
12
20
  it "should respond to words" do
13
- RandomPhrase::Dictionary.should respond_to(:words)
21
+ RandomPhrase::Dictionary.new.should respond_to(:words)
14
22
  end
15
23
 
16
24
  it "should return an array" do
17
- RandomPhrase::Dictionary.words.should be_kind_of(Array)
25
+ RandomPhrase::Dictionary.new.words.should be_kind_of(Array)
26
+ end
27
+
28
+ it "should accept optional arguments" do
29
+ RandomPhrase::Dictionary.new.words.should be_kind_of(Array)
30
+ RandomPhrase::Dictionary.new.words(:length => 3).should be_kind_of(Array)
31
+ RandomPhrase::Dictionary.new.words(:length => 3, :pattern => /abc/).should be_kind_of(Array)
32
+ end
33
+
34
+ context "passing in length" do
35
+ it "should return only words with length characters" do
36
+ dictionary = RandomPhrase::Dictionary.new(Proc.new {["a", "a"*2, "a"*3, "a"*4, "a"]})
37
+ dictionary.words(:length => 1).each { |w| w.length.should == 1}
38
+ dictionary.words(:length => 2).each { |w| w.length.should == 2}
39
+ dictionary.words(:length => 3).each { |w| w.length.should == 3}
40
+ dictionary.words(:length => 4).each { |w| w.length.should == 4}
41
+
42
+ dictionary.words(:length => 1).length.should == 2
43
+ dictionary.words(:length => 2).length.should == 1
44
+ dictionary.words(:length => 3).length.should == 1
45
+ dictionary.words(:length => 4).length.should == 1
46
+ end
47
+ end
48
+
49
+ context "passing in pattern" do
50
+ it "should return only words matching the pattern" do
51
+ dictionary = RandomPhrase::Dictionary.new(Proc.new {["a", "a"*2, "a"*3, "a"*4, "b"]})
52
+ dictionary.words(:pattern => /b/).each { |w| w.should =~ /b/}
53
+ dictionary.words(:pattern => /a/).each { |w| w.should =~ /a/}
54
+ dictionary.words(:pattern => /a{3}/).each { |w| w.should =~ /a{3}/}
55
+ end
18
56
  end
19
57
  end
20
58
  end
@@ -1,7 +1,25 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe RandomPhrase do
4
- context "module methods" do
4
+
5
+ context "::dictionary" do
6
+ it "should have a dictionary accessor attribute" do
7
+ RandomPhrase.should respond_to(:dictionary=)
8
+ RandomPhrase.should respond_to(:dictionary)
9
+ end
10
+
11
+ it "should only return words from its dictionary" do
12
+ dictionary = RandomPhrase::Dictionary.new(Proc.new {["a", "b", "c"]})
13
+ RandomPhrase.dictionary= dictionary
14
+ RandomPhrase.phrase().split.each { |word| dictionary.words.should include(word) }
15
+ end
16
+
17
+ after(:all) do
18
+ RandomPhrase.dictionary= nil
19
+ end
20
+ end
21
+
22
+ context "::phrase" do
5
23
  it "should respond to phrase an optional word count" do
6
24
  RandomPhrase.should respond_to(:phrase)
7
25
  RandomPhrase.should respond_to(:phrase).with(1)
@@ -24,5 +42,45 @@ describe RandomPhrase do
24
42
  it "should return different phrases on successive calls" do
25
43
  RandomPhrase.phrase.should_not == RandomPhrase.phrase
26
44
  end
45
+
46
+ context "with options" do
47
+ context ":length =>" do
48
+ it "should only return words of the specified length" do
49
+ RandomPhrase.dictionary= RandomPhrase::Dictionary.new(Proc.new {["aaa", "bbbb", "cc", "ddd", "ee"]})
50
+ RandomPhrase.phrase(2, :length => 3).split.each {|word| word.length.should == 3}
51
+ end
52
+ after(:all) do
53
+ RandomPhrase.dictionary= nil
54
+ end
55
+ end
56
+
57
+ context ":pattern =>" do
58
+ before(:all) do
59
+ RandomPhrase.dictionary= nil
60
+ end
61
+
62
+ context "single word patterns" do
63
+ it "should return a phrase matching the pattern" do
64
+ RandomPhrase.dictionary= RandomPhrase::Dictionary.new(Proc.new {["aed", "bed", "cat", "a", "foeder"]})
65
+ pattern = /[a-zA-Z]+ed/
66
+ phrase = RandomPhrase.phrase(pattern)
67
+ pattern.match(phrase).should_not be_nil
68
+ end
69
+ end
70
+
71
+ context "multi-word patterns" do
72
+ it "should return a phrase matching the pattern" do
73
+ RandomPhrase.dictionary= RandomPhrase::Dictionary.new(Proc.new {["aed", "bed", "cat", "a", "foeder"]})
74
+ pattern = /\w+ [a-zA-Z]+ed/
75
+ phrase = RandomPhrase.phrase(pattern)
76
+ pattern.match(phrase).should_not be_nil
77
+ end
78
+ end
79
+
80
+ after(:all) do
81
+ RandomPhrase.dictionary= nil
82
+ end
83
+ end
84
+ end
27
85
  end
28
86
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random_phrase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-30 00:00:00.000000000 -07:00
13
- default_executable:
12
+ date: 2011-10-28 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
- name: rspec
17
- requirement: &2153086480 !ruby/object:Gem::Requirement
15
+ name: activesupport
16
+ requirement: &2161656640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2161656640
25
+ - !ruby/object:Gem::Dependency
26
+ name: require_all
27
+ requirement: &2161656220 !ruby/object:Gem::Requirement
18
28
  none: false
19
29
  requirements:
20
30
  - - ! '>='
@@ -22,9 +32,31 @@ dependencies:
22
32
  version: '0'
23
33
  type: :development
24
34
  prerelease: false
25
- version_requirements: *2153086480
26
- description: This gem provides random phrase functionality and extends Array with
27
- a random .pick
35
+ version_requirements: *2161656220
36
+ - !ruby/object:Gem::Dependency
37
+ name: ruby-debug19
38
+ requirement: &2161655760 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2161655760
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &2161655260 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2161655260
58
+ description: This gem provides the ability to generate a random phrase from a customizable
59
+ word list via word length and regexp matching.
28
60
  email:
29
61
  - yosem.sweet@suite101.com
30
62
  executables: []
@@ -32,19 +64,17 @@ extensions: []
32
64
  extra_rdoc_files: []
33
65
  files:
34
66
  - .gitignore
67
+ - .rvmrc
35
68
  - Gemfile
36
69
  - Rakefile
37
70
  - lib/random_phrase.rb
38
71
  - lib/random_phrase/config/words
39
- - lib/random_phrase/core_ext/array.rb
40
72
  - lib/random_phrase/dictionary.rb
41
73
  - lib/random_phrase/version.rb
42
74
  - random_phrase.gemspec
43
- - spec/lib/core_ext/array_spec.rb
44
75
  - spec/lib/dictionary_spec.rb
45
76
  - spec/lib/random_phrase_spec.rb
46
77
  - spec/spec_helper.rb
47
- has_rdoc: true
48
78
  homepage: https://github.com/yosemsweet/random_phrase
49
79
  licenses: []
50
80
  post_install_message:
@@ -65,12 +95,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
95
  version: '0'
66
96
  requirements: []
67
97
  rubyforge_project: random_phrase
68
- rubygems_version: 1.6.2
98
+ rubygems_version: 1.8.10
69
99
  signing_key:
70
100
  specification_version: 3
71
101
  summary: Generate a simple random phrase
72
102
  test_files:
73
- - spec/lib/core_ext/array_spec.rb
74
103
  - spec/lib/dictionary_spec.rb
75
104
  - spec/lib/random_phrase_spec.rb
76
105
  - spec/spec_helper.rb
@@ -1,5 +0,0 @@
1
- class Array
2
- def pick
3
- at Kernel.rand(size)
4
- end
5
- end
@@ -1,15 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Array do
4
- context "#pick" do
5
- it "should respond to pick" do
6
- [].should respond_to(:pick)
7
- end
8
-
9
- it "should return a random element" do
10
- a = [1,2,3,4,5,6,7,8,9,10]
11
- a.pick.should_not == a.pick
12
- a.find_index(a.pick).should_not be_nil
13
- end
14
- end
15
- end