random_fu 0.0.3

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/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ **.swp
2
+ tags
data/MIT-LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ LICENSE
2
+
3
+ The MIT License
4
+
5
+ Copyright (c) 2012 Ledsworth Consulting LLC
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ Random_fu
2
+ =========
3
+
4
+ A Ruby gem to add random capability. Currently, only for Strings.
5
+
6
+ DO NOT USE for generating any data related to security!!!
7
+ ---------------------------------------------------------
8
+ There are better, more secure, methods for this. This gem is meant ONLY for non secure
9
+ random strings.
10
+
11
+ Installation
12
+ ============
13
+
14
+ Random_fu will soon be distributed as a ruby gem (not yet published, in dev). Simply require it in your Gemfile.
15
+
16
+ gem "random_fu"
17
+
18
+ ## Usage
19
+
20
+ ## String#random_order
21
+
22
+ Returns a random string created from the characters in the string. Characters in the string are used only once, and the result
23
+ will be the same length. The result is simply a jumbled version.
24
+
25
+ ### Examples:
26
+
27
+ "abc".random_order
28
+ # => "cba"
29
+
30
+ "abcdef".random_order[0] # Random letter between a and f
31
+ # => "b"
32
+
33
+ "abcdef".random_order[0,2] # Two random letters between a and f
34
+ # => "eb"
35
+
36
+ ## String#random(count)
37
+
38
+ Returns a random string the size of count created from the characters in the string. Characters are used one or more times, and
39
+ characters may repeat and/or not be used at all. The source string is just a list of possible characters (to place a
40
+ string in a random order, use String#random_order). For example, "abcd".random(n) could result in
41
+ just one letter used multiple times:
42
+
43
+ "abcd".random(2)
44
+ # => "dd"
45
+
46
+ ### Examples:
47
+
48
+ To produce a random character from the alphabet:
49
+
50
+ "abcdefghijklmnopqrstuvwxyz".random(1)
51
+ # => "d"
52
+
53
+ ("a".."z").to_a.join.random(1)
54
+ # => "j"
55
+
56
+ To produce a 20 character random string from the alphabet:
57
+
58
+ "abcdefghijklmnopqrstuvwxyz".random(20)
59
+ # => "ijvrvddrwgucdsyzybfq"
60
+
61
+
62
+ License
63
+ =======
64
+
65
+ random_fu is Copyright © 2012 Ledsworth Consulting LLC. It is free software, and may be
66
+ redistributed under the terms specified in the MIT-LICENSE file.
@@ -0,0 +1,47 @@
1
+ # Top level module for random_fu
2
+ #
3
+ # Author:: Brian Ledsworth of Ledsworth Consluting LLC
4
+ # Copyright:: Copyright (c) 2012 Ledsworth Consulting LLC.
5
+ # License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
6
+
7
+ module RandomFu
8
+
9
+ # Instance methods mixed into String
10
+ module StringInstanceMethods
11
+
12
+ # Returns a random string the size of count created from the characters in
13
+ # the string. Characters are used one or more times, and characters may
14
+ # repeat and/or not be used at all. The source string is just a list of
15
+ # possible characters (to place a string in a random order, use
16
+ # String#random_order). For example, "abcd".random(n) could result in
17
+ # just one letter used multiple times:
18
+ def random(count)
19
+ if !count.is_a?(Integer) || count < 1 then
20
+ raise(ArgumentError, "count needs to be a Integer >= 1")
21
+ end
22
+ return "" if self.empty?
23
+
24
+ result = ""
25
+ 1.upto(count) do
26
+ result << self[rand(self.length)]
27
+ end
28
+ result
29
+ end
30
+
31
+ # Returns a random string created from the characters in the string.
32
+ # Characters in the string are used only once, and the result
33
+ # will be the same length. The result is simply a jumbled version.
34
+ def random_order
35
+ return "" if self.empty?
36
+
37
+ result = ""
38
+ chars = self.split(//)
39
+ 1.upto(chars.size) do
40
+ result << chars.delete_at(rand(chars.size))
41
+ end
42
+ result
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,3 @@
1
+ module RandomFu
2
+ VERSION = '0.0.3'
3
+ end
data/lib/random_fu.rb ADDED
@@ -0,0 +1,4 @@
1
+ require_relative 'random_fu/version'
2
+ require_relative 'random_fu/string'
3
+
4
+ String.send :include, RandomFu::StringInstanceMethods
Binary file
data/random_fu.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
2
+ require 'random_fu/version.rb'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "random_fu"
6
+ s.summary = "Random helpers"
7
+ s.version = RandomFu::VERSION
8
+ s.author = "Brian Ledsworth of Ledsworth Consluting LLC"
9
+ s.description = "Incorprate random helpers into your app"
10
+ s.email = ["brian@ledsworth.com"]
11
+ s.homepage = "https://github.com/AgileMantis/random_fu"
12
+ s.required_ruby_version = '>= 1.9.3'
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.require_paths = ["lib"]
16
+ # s.has_rdoc = false
17
+ # s.add_development_dependency('rspec')
18
+ # s.platform = Gem::Platform::RUBY
19
+ end
@@ -0,0 +1 @@
1
+ require_relative '../lib/random_fu.rb'
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe "random_fu's String#random_order" do
4
+
5
+ let(:string) { "abcdefghijklmnopqrstuvwxyz0123456789" }
6
+
7
+ describe "when called on an empy string" do
8
+ it "should return an empty string" do
9
+ "".random_order.should == ""
10
+ # I can't see this as a valid use case, but don't feel
11
+ # it should throw an error. So I left it awaiting
12
+ # community feedback.
13
+ end
14
+ end
15
+
16
+ describe "when called on a single char string" do
17
+ it "should return same string" do
18
+ "f".random_order.should == "f"
19
+ # I can't see this as a valid use case, but don't feel
20
+ # it should throw an error. So I left it awaiting
21
+ # community feedback.
22
+ end
23
+ end
24
+
25
+ describe "should return a random string which" do
26
+
27
+ it "should be the same length" do
28
+ string.random_order.should_not == string
29
+ end
30
+
31
+ it "should contain the exact same characters but jumbled" do
32
+ rand = string.random_order
33
+ rand.length.should == string.length
34
+ rand_set = rand.split(//) - string.split(//)
35
+ rand_set.should be_empty, rand_set
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+
3
+ describe "random_fu's String#random" do
4
+
5
+ let(:string) { "abcdefghijklmnopqrstuvwxyz0123456789" }
6
+
7
+ describe "when called incorrectly" do
8
+
9
+ describe "with no arguments" do
10
+ it "should raise an ArgumentError" do
11
+ lambda { rand = string.random }.should raise_exception(ArgumentError)
12
+ end
13
+ end
14
+
15
+ describe "with a non Integer" do
16
+ it "should raise an ArgumentError" do
17
+ lambda { rand = string.random("foo") }.should raise_exception(ArgumentError)
18
+ lambda { rand = string.random(-1) }.should raise_exception(ArgumentError)
19
+ lambda { rand = string.random(0) }.should raise_exception(ArgumentError)
20
+ end
21
+ end
22
+
23
+ describe "on a empty" do
24
+ it "should return a blank string" do
25
+ "".random(100).should == ""
26
+ end
27
+ end
28
+
29
+ describe "on a single char string" do
30
+ it "should return a sting made up of just that char" do
31
+ "f".random(30).should == "f"*30
32
+ # I can't see this as a valid use case, but don't feel
33
+ # it should throw an error. So I left it awaiting
34
+ # community feedback.
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ describe "should return a random string which" do
41
+
42
+ it "should be created only from the characters of the String" do
43
+ rand = string.random(string.length)
44
+ rand.should_not == string
45
+ # Cant simply set theory, as rand may not contain all chars
46
+ # from string (e.g. "abcd".random might return "cabc")
47
+ (string.split(//) & rand.split(//)).each do |odd_char|
48
+ string.should include(odd_char)
49
+ end
50
+ end
51
+
52
+ it "should be the same length as the argument" do
53
+ [ 1, 5, string.length, string.length*2 ].each do |arg|
54
+ string.random(arg).length.should == arg
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+
63
+
64
+
65
+ #
66
+ #
67
+ #
68
+ #
69
+ #
70
+ # it "should return a random string of the same length" do
71
+ # rand = string.random
72
+ # rand.length.should == string.length
73
+ # rand.should_not == string
74
+ # rand.chars.each do |char|
75
+ # string.should include(char)
76
+ # end
77
+ # end
78
+ #
79
+ # it "should not use an index greater than its length" do
80
+ # string = "ab"
81
+ # 1.upto(99) do
82
+ # string.random.length.should == string.length
83
+ # # nil returned from String#[index to high] would not increase
84
+ # # the length of the result string after foo.join. Thus, if
85
+ # # random went out of bounds on index, the result string
86
+ # # would be less in size.
87
+ # end
88
+ # end
89
+ #
90
+ # end
91
+ #
92
+ # describe "with argument of 0" do
93
+ #
94
+ # it "should return a blank string" do
95
+ # rand = string.random(0)
96
+ # rand.should == ""
97
+ # end
98
+ #
99
+ # end
100
+ #
101
+ # describe "with invalid arguments" do
102
+ #
103
+ # it "should raise an exception when count is too high" do
104
+ # lambda { rand = string.random(string.length+1) }.should raise_exception
105
+ # end
106
+ #
107
+ # it "should raise an exception if it's negative" do
108
+ # lambda { rand = string.random(-1) }.should raise_exception
109
+ # end
110
+ #
111
+ # it "should raise an exception if not an Integer class" do
112
+ # lambda { rand = string.random("foo") }.should raise_exception
113
+ # lambda { rand = string.random(0.1) }.should raise_exception
114
+ # end
115
+ #
116
+ # end
117
+ #
118
+
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe "random_fu's version" do
4
+ it "should be current" do
5
+ RandomFu::VERSION.should == '0.0.3'
6
+ end
7
+ end
8
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: random_fu
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.3
6
+ platform: ruby
7
+ authors:
8
+ - Brian Ledsworth of Ledsworth Consluting LLC
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-06-20 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Incorprate random helpers into your app
17
+ email:
18
+ - brian@ledsworth.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - .gitignore
27
+ - MIT-LICENSE
28
+ - README.md
29
+ - lib/random_fu.rb
30
+ - lib/random_fu/string.rb
31
+ - lib/random_fu/version.rb
32
+ - random_fu-0.0.2.gem
33
+ - random_fu-0.0.3.gem
34
+ - random_fu.gemspec
35
+ - spec/spec_helper.rb
36
+ - spec/string_random_order_spec.rb
37
+ - spec/string_random_spec.rb
38
+ - spec/version_spec.rb
39
+ homepage: https://github.com/AgileMantis/random_fu
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.9.3
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.24
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Random helpers
66
+ test_files:
67
+ - spec/spec_helper.rb
68
+ - spec/string_random_order_spec.rb
69
+ - spec/string_random_spec.rb
70
+ - spec/version_spec.rb
71
+ has_rdoc: