random_fu 0.0.5 → 0.0.6

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.
@@ -1,6 +1,8 @@
1
1
  require_relative 'random_fu/version'
2
2
  require_relative 'random_fu/string'
3
3
  require_relative 'random_fu/hash'
4
+ require_relative 'random_fu/time'
4
5
 
5
6
  String.send :include, RandomFu::StringInstanceMethods
6
7
  Hash.send :include, RandomFu::HashInstanceMethods
8
+ Time.send :include, RandomFu::TimeInstanceMethods
@@ -0,0 +1,21 @@
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 Hash
10
+ module HashInstanceMethods
11
+
12
+ # Returns the value from a random key.
13
+ def random
14
+ return nil if self.empty?
15
+ keys = self.keys
16
+ self[keys[rand(keys.size)]]
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,26 @@
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 Time
10
+ module TimeInstanceMethods
11
+
12
+ # Returns a new Time object which is a random time
13
+ # between self and other_time
14
+ def random_between(other_time)
15
+ if !other_time.is_a?(Time) then raise(ArgumentError,"Time expected") end
16
+ return self if self==other_time
17
+
18
+ # for large spans between time, to_i is not efficeint
19
+ time = self.dup
20
+ rnd = rand(self.to_i - other_time.to_i)
21
+ time = self.dup + ((self < other_time) ? rnd : -rnd)
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -1,3 +1,3 @@
1
1
  module RandomFu
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe "random_fu's Hash#random" do
4
+
5
+ let(:hash) { { fred: 'foo', tom: 'bar', jen: 'baz' } }
6
+
7
+ describe "when called on an empty hash" do
8
+ it "should return nil" do
9
+ {}.random.should be_nil
10
+ end
11
+ end
12
+
13
+ describe "with called" do
14
+ it "should return one value from a random key" do
15
+ value = hash.random
16
+ hash.value?(value).should be_true
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe "random_fu's Time#random_between" do
4
+
5
+ let(:time_now) { Time.now }
6
+ let(:second_diffs) { [ 0, 1, 60, 100, 86_400, 2_592_000 ] }
7
+ let(:multipliers) { [ 1, 10, 100, 1000, 10_000, 100_000 ] }
8
+
9
+ describe "should return a random time between self and parameter" do
10
+
11
+ it "when self is less than the paramter" do
12
+ multipliers.each do |multiplier|
13
+ second_diffs.each do |n|
14
+ other_time = time_now + n*multiplier
15
+ rnd_time = time_now.random_between(other_time)
16
+ rnd_time.between?(time_now,other_time).should be_true
17
+ end
18
+ end
19
+ end
20
+
21
+ it "when self is greater than the paramter" do
22
+ multipliers.each do |multiplier|
23
+ second_diffs.each do |n|
24
+ other_time = time_now - n*multiplier
25
+ rnd_time = time_now.random_between(other_time)
26
+ rnd_time.between?(other_time,time_now).should be_true
27
+ end
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ describe "should be the same time" do
34
+ it "when self is equal to the paramter" do
35
+ time_now.random_between(time_now).should == time_now
36
+ end
37
+ end
38
+
39
+ describe "when called incorrectly" do
40
+ describe "without a parameter" do
41
+ it "should raise an argument exception error" do
42
+ lambda { time_now.random_between }.should raise_error(ArgumentError)
43
+ end
44
+ end
45
+ describe "with a non Time object" do
46
+ it "should raise an argument exception error" do
47
+ lambda { time_now.random_between(1) }.should raise_error(ArgumentError, "Time expected")
48
+ end
49
+ end
50
+ end
51
+
52
+ end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe "random_fu's version" do
4
4
  it "should be current" do
5
- RandomFu::VERSION.should == '0.0.5'
5
+ RandomFu::VERSION.should == '0.0.6'
6
6
  end
7
7
  end
8
8
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: random_fu
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.5
5
+ version: 0.0.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Brian Ledsworth of Ledsworth Consluting LLC
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-06-21 00:00:00 Z
13
+ date: 2012-06-27 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Incorprate random helpers into your app
@@ -27,12 +27,16 @@ files:
27
27
  - MIT-LICENSE
28
28
  - README.md
29
29
  - lib/random_fu.rb
30
+ - lib/random_fu/hash.rb
30
31
  - lib/random_fu/string.rb
32
+ - lib/random_fu/time.rb
31
33
  - lib/random_fu/version.rb
32
34
  - random_fu.gemspec
35
+ - spec/hash_random_spec.rb
33
36
  - spec/spec_helper.rb
34
37
  - spec/string_random_order_spec.rb
35
38
  - spec/string_random_spec.rb
39
+ - spec/time_random_spec.rb
36
40
  - spec/version_spec.rb
37
41
  homepage: https://github.com/AgileMantis/random_fu
38
42
  licenses: []
@@ -62,8 +66,10 @@ signing_key:
62
66
  specification_version: 3
63
67
  summary: Random helpers
64
68
  test_files:
69
+ - spec/hash_random_spec.rb
65
70
  - spec/spec_helper.rb
66
71
  - spec/string_random_order_spec.rb
67
72
  - spec/string_random_spec.rb
73
+ - spec/time_random_spec.rb
68
74
  - spec/version_spec.rb
69
75
  has_rdoc: