random_data 1.2.1 → 1.3.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/History.txt CHANGED
@@ -1,3 +1,17 @@
1
+ == 1.3.0 2008-05-25
2
+
3
+ All these enhancements are from Hugh Sasse. Thanks Hugh!
4
+
5
+ * 4 major enhancements:
6
+ * Added RandomData::Grammar, which lets you create simple random sentences (see rdocs)
7
+ * Added Random.bit and Random.bits
8
+ * Added Random.grammatical_construct
9
+ * Fixed up rdocs which had gotten out of date
10
+
11
+ * 2 minor enhancements:
12
+ * Added Random.uk_post_code
13
+ * Zipcodes should strings, not integers
14
+
1
15
  == 1.2.1 2007-11-29
2
16
 
3
17
  * 1 minor fix:
data/Manifest.txt CHANGED
@@ -10,6 +10,7 @@ lib/random_data/array_randomizer.rb
10
10
  lib/random_data/booleans.rb
11
11
  lib/random_data/contact_info.rb
12
12
  lib/random_data/dates.rb
13
+ lib/random_data/grammar.rb
13
14
  lib/random_data/locations.rb
14
15
  lib/random_data/names.rb
15
16
  lib/random_data/numbers.rb
data/README.txt CHANGED
@@ -20,21 +20,35 @@ or get the gem manually from http://rubyforge.org/frs/?group_id=4458
20
20
  Random.phone, Random.international_phone, Random.email
21
21
 
22
22
  === Time/Date Methods
23
- Random.date
23
+ Random.date, Random.date_between
24
24
 
25
25
  === Location Methods
26
- Random.address_line_1, Random.address_line_2, Random.zipcode, Random.state, Random.country, Random.city,
26
+ Random.address_line_1, Random.address_line_2, Random.zipcode, Random.uk_post_code, Random.state, Random.state_full, Random.country, Random.city
27
27
 
28
28
  === Name Methods
29
- Random.firstname, Random.initial, Random.lastname,
29
+ Random.firstname, Random.initial, Random.lastname
30
30
 
31
31
  === Text Methods
32
32
  Random.alphanumeric, Random.paragraphs
33
33
 
34
- Note that some methods take parameters to bound or limit the amount of data returned. See RDocs for details.
34
+ === Grammatical Methods
35
+ Random.grammatical_construct
36
+
37
+ === Number Methods
38
+ Random.number, Random.bit, Random.bits
35
39
 
40
+ === Boolean Methods
41
+ Random.boolean
36
42
 
37
- == Examples
43
+ === Array Extension
44
+ Array.rand
45
+
46
+ === Choose From File Methods
47
+ Random implements a method_missing helper that will choose a line at random from a file in your load path. See Random::method_missing for details.
48
+
49
+ Note that some methods take parameters to bound or limit the amount of data returned. See RDocs for details.
50
+
51
+ == Examples of Some Methods
38
52
 
39
53
  >>Random.alphanumeric
40
54
  => "cfbutm3vYfhZXil0"
@@ -103,5 +117,8 @@ Note that some methods take parameters to bound or limit the amount of data retu
103
117
  == Contact
104
118
  Let me know if you have patches, comments, or suggestions: mailto:mike@subelsky.com
105
119
 
120
+ == Special Thanks
121
+ Major thanks to Hugh Sasse and {Paul Barry}[http://paulbarry.com/] for their contributions.
122
+
106
123
  == Copyright
107
- Copyright (c) 2007 {Mike Subelsky}[http://subelsky.com/], Baltimore, Maryland, released under the MIT license
124
+ Copyright (c) 2008 {Mike Subelsky}[http://subelsky.com/], Baltimore, Maryland, released under the MIT license.
@@ -1,7 +1,7 @@
1
1
  module RandomData
2
2
 
3
3
  module Booleans
4
- #Returns true or false
4
+ #Returns true or false randomly
5
5
  def boolean
6
6
  rand(2) == 1 ? true : false
7
7
  end
@@ -0,0 +1,61 @@
1
+
2
+ module RandomData
3
+
4
+ # Defines methods for the generation of random data based on a supplied grammar.
5
+
6
+ module Grammar
7
+
8
+ # Returns simple sentences based on a supplied grammar, which must be a hash, the
9
+ # keys of which are symbols. The values are either an array of successive values or a grammar
10
+ # (i.e, hash with symbols as keys, and hashes or arrays as values. The arrays contain symbols
11
+ # referencing the keys in the present grammar, or strings to be output. The keys are always symbols.
12
+ #
13
+ # Example:
14
+ # Random.grammatical_construct({:story => [:man, " bites ", :dog], :man => { :bob => "Bob"}, :dog => {:a =>"Rex", :b =>"Rover"}}, :story)
15
+ # => "Bob bites Rover"
16
+
17
+ def grammatical_construct(grammar, what=nil)
18
+ output = ""
19
+ if what.nil?
20
+ case grammar
21
+ when Hash
22
+ a_key = grammar.keys.sort_by{rand}[0]
23
+ output += grammatical_construct(grammar, a_key)
24
+ when Array
25
+ grammar.each do |item|
26
+ output += grammatical_construct(item)
27
+ end
28
+ when String
29
+ output += grammar
30
+ end
31
+ else
32
+ rhs = grammar[what]
33
+ case rhs
34
+ when Array
35
+ rhs.each do |item|
36
+ case item
37
+ when Symbol
38
+ output += grammatical_construct(grammar,item)
39
+ when String
40
+ output += item
41
+ when Hash
42
+ output += grammatical_construct(item)
43
+ else
44
+ raise "#{item.inspect} must be a symbol or string or Hash"
45
+ end
46
+ end
47
+ when Hash
48
+ output+= grammatical_construct(rhs)
49
+ when Symbol
50
+ output += grammatical_construct(rhs)
51
+ when String
52
+ output += rhs
53
+ else
54
+ raise "#{rhs.inspect} must be a symbol, string, Array or Hash"
55
+ end
56
+ end
57
+ return output
58
+ end
59
+
60
+ end
61
+ end
@@ -9,8 +9,9 @@ module RandomData
9
9
  Linden Maple Oak Pine Rose Walnut Willow)
10
10
  people = %w( Adams Franklin Jackson Jefferson Lincoln
11
11
  Madison Washington Wilson)
12
+ people_uk = %w( Churchill Tyndale Latimer Cranmer )
12
13
  places = %w( Highland Hill Park Woodland Sunset Virginia)
13
- numbers = %w( 1st 2nd 4th 5th )
14
+ numbers = %w( 1st 2nd 4th 5th 34th 42nd )
14
15
  @@streetnames = trees + people + places + numbers
15
16
 
16
17
  @@street_types = %w(St Ave Rd Blvd Trl Ter Rdg Pl Pkwy Ct Circle)
@@ -25,6 +26,8 @@ module RandomData
25
26
  "#{rand(40000)} #{@@streetnames.rand} #{@@street_types.rand}"
26
27
  end
27
28
 
29
+ alias :us_address_line_1 :address_line_1
30
+
28
31
  @@line2types = ["Apt", "Bsmt", "Bldg", "Dept", "Fl", "Frnt", "Hngr", "Lbby", "Lot", "Lowr", "Ofc", "Ph", "Pier", "Rear", "Rm", "Side", "Slip", "Spc", "Stop", "Ste", "Trlr", "Unit", "Uppr"]
29
32
 
30
33
  # Returns the first line of a US maiiling address (street number, street name, street type)
@@ -38,8 +41,25 @@ module RandomData
38
41
  end
39
42
 
40
43
  # Returns a random 5-digit string, not guaranteed to be a legitimate zip code.
44
+ # Legal zip codes can have leading zeroes and thus they need to be strings.
45
+
41
46
  def zipcode
42
- rand(89999) + 10000
47
+ "%05d" % rand(99999)
48
+ end
49
+
50
+
51
+ # Returns a string providing something in the general form of a UK post code. Like the zip codes, this might
52
+ # not actually be valid. Doesn't cover London whose codes are like "SE1".
53
+
54
+ def uk_post_code
55
+ post_towns = %w(BM CB CV LE LI LS KT MK NE OX PL YO)
56
+ # Can't remember any othes at the moment
57
+ number_1 = rand(100).to_s
58
+ number_2 = rand(100).to_s
59
+ # Easier way to do this?
60
+ letters = ("AA".."ZZ").to_a.rand
61
+
62
+ return "#{post_towns.rand}#{number_1} #{number_2}#{letters}"
43
63
  end
44
64
 
45
65
  # from technoweenie: http://svn.techno-weenie.net/projects/plugins/us_states/lib/us_states.rb
@@ -56,7 +76,7 @@ module RandomData
56
76
  ["Washington", "WA"], ["Wisconsin", "WI"], ["West Virginia", "WV"], ["Wyoming", "WY"]]
57
77
 
58
78
  # Returns a state 2-character abbreviation
59
- #Random.state = "IL"
79
+ # Random.state = "IL"
60
80
 
61
81
  def state
62
82
  @@us_states.rand[1]
@@ -5,9 +5,27 @@ module RandomData
5
5
  #returns a random number within the range
6
6
  # Examples
7
7
  #
8
- #
8
+ # >> Random.number(5)
9
+ # => 4
10
+ # >> Random.number(5)
11
+ # => 2
12
+ # >> Random.number(5)
13
+ # => 1
9
14
  def number(n)
10
15
  n.is_a?(Range) ? n.to_a.rand : rand(n)
11
16
  end
17
+
18
+ # return a random bit, 0 or 1.
19
+ def bit
20
+ rand(2)
21
+ end
22
+
23
+ # return an array of n random bits.
24
+ def bits(n)
25
+ x = []
26
+ n.times {x << bit}
27
+ x
28
+ end
29
+
12
30
  end
13
- end
31
+ end
@@ -4,7 +4,6 @@ module RandomData
4
4
 
5
5
  # Methods to create random strings and paragraphs.
6
6
 
7
-
8
7
  # Returns a string of random upper- and lowercase alphanumeric characters. Accepts a size parameters, defaults to 16 characters.
9
8
  #
10
9
  # >> Random.alphanumeric
@@ -1,8 +1,8 @@
1
1
  module RandomData #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
- MINOR = 2
5
- TINY = 1
4
+ MINOR = 3
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/lib/random_data.rb CHANGED
@@ -8,18 +8,32 @@ require 'random_data/locations'
8
8
  require 'random_data/names'
9
9
  require 'random_data/numbers'
10
10
  require 'random_data/text'
11
+ require 'random_data/grammar'
11
12
 
12
13
  class Random
13
14
  extend RandomData::Booleans
14
15
  extend RandomData::ContactInfo
15
16
  extend RandomData::Dates
17
+ extend RandomData::Grammar
16
18
  extend RandomData::Locations
17
19
  extend RandomData::Names
18
20
  extend RandomData::Numbers
19
21
  extend RandomData::Text
20
22
 
21
- # Looks for a file with the name methodname.dat, reads the lines from that file, then gives you a random line from that file
22
- # Will also search your load path for the file. Raises an error if it can't find the file.
23
+ # Looks for a file in the load path with the name methodname.dat, reads the lines from that file, then gives you a random line from that file.
24
+ # Raises an error if it can't find the file. For example, given a file named "horse.dat" in your load path:
25
+ # >> Random.horse
26
+ # => "Stallion"
27
+ # >> Random.horse
28
+ # => "Pony"
29
+ # >> Random.horse
30
+ # => "Mare"
31
+ # >> Random.horse
32
+ # => "Clydesdale"
33
+ # >> Random.horse
34
+ # => "Stallion"
35
+ # >> Random.horse
36
+ # => "Mare"
23
37
 
24
38
  def self.method_missing(methodname)
25
39
  thing = "#{methodname}.dat"
data/script/destroy CHANGED
File without changes
data/script/generate CHANGED
File without changes
data/script/txt2html CHANGED
File without changes
@@ -38,7 +38,11 @@ class TestRandomData < Test::Unit::TestCase
38
38
  end
39
39
 
40
40
  def test_should_return_random_zipcode
41
- assert_equal 48408, Random.zipcode
41
+ assert_equal "38408", Random.zipcode
42
+ end
43
+
44
+ def test_should_return_random_uk_post_code
45
+ assert_equal "BM8 24DB", Random.uk_post_code
42
46
  end
43
47
 
44
48
  def test_should_return_random_state
@@ -119,9 +123,26 @@ class TestRandomData < Test::Unit::TestCase
119
123
  assert_equal 8, Random.number(9)
120
124
  end
121
125
 
126
+ def test_should_return_random_bit
127
+ assert_equal 0, Random.bit
128
+ end
129
+
130
+ def test_should_return_random_bits
131
+ assert_equal 10, Random.bits(10).size
132
+ assert_equal [0, 1, 0, 0, 0, 0, 1, 0, 0, 1], Random.bits(10)
133
+ end
134
+
122
135
  def test_should_return_random_number_within_range
123
136
  assert_equal 13, Random.number(5..15)
124
137
  end
138
+
139
+ def test_should_return_random_grammatical_construct
140
+ assert_equal "Bob bites Rover",
141
+ Random::grammatical_construct({:story => [:man, " bites ", :dog],
142
+ :man => { :bob => "Bob"},
143
+ :dog => {:a =>"Rex", :b =>"Rover"}},
144
+ :story)
145
+ end
125
146
 
126
147
  end
127
148
 
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
5
- platform: ""
4
+ version: 1.3.0
5
+ platform: ruby
6
6
  authors:
7
7
  - Mike Subelsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2007-11-29 00:00:00 -05:00
12
+ date: 2008-05-25 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -38,6 +38,7 @@ files:
38
38
  - lib/random_data/booleans.rb
39
39
  - lib/random_data/contact_info.rb
40
40
  - lib/random_data/dates.rb
41
+ - lib/random_data/grammar.rb
41
42
  - lib/random_data/locations.rb
42
43
  - lib/random_data/names.rb
43
44
  - lib/random_data/numbers.rb
@@ -81,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  requirements: []
82
83
 
83
84
  rubyforge_project: random-data
84
- rubygems_version: 0.9.5
85
+ rubygems_version: 1.1.1
85
86
  signing_key:
86
87
  specification_version: 2
87
88
  summary: A Ruby gem that provides a Random singleton class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text.