sometimes 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccb9ae4d24901c0c3e8c281b0a472757886b960114efd2522a98e541eaf62979
4
- data.tar.gz: 31f5ea8578f47a910f01053ac0ac5d7173a19875253e5ce6470b63a1c4b70b02
3
+ metadata.gz: 60c3688a8dd7b7b16bf79a3f1949e52b11b87331553d94189ad070297f042433
4
+ data.tar.gz: e07d362391db192f46557c5c2ce5328c770f9a1a442781aa8ccae2e29979cdbf
5
5
  SHA512:
6
- metadata.gz: de1851cdd5e43008141669e83f67d7d9a288c50831534e27091f9c05263eba54388601cb14ba842abd863610603d2e56388d0785150f24e93dbb11f322de221e
7
- data.tar.gz: 736c27a6b843706b1aef4cfeeb06ebb6e982db39f35cbb4c977abe7aea4fbe4721601e49014a5b50143450b8df98e2ec15fca38791509ac4f8a91de7f5317013
6
+ metadata.gz: 2ae125a0a5c25cb46dfbfb2e54e086c061a28bce0bbda46b08fdcb073c612b84ee85a5461402a1fc6389583efc19f941640a48b05b2ef88632ed78a02744461b
7
+ data.tar.gz: 397b6f0b55bf1c340d88bcd93389179b87edd40c992ba56f474f3b3b470d805fa3ee0a95f724e80f9bbdb90ef164b1a629999173dd225dc15aef27fc779a1102
data/README.md CHANGED
@@ -1,17 +1,11 @@
1
1
  # Sometimes
2
2
 
3
- [![Build Status](https://travis-ci.org/rubytune/sometimes.svg?branch=master)](https://travis-ci.org/rubytune/sometimes)
4
-
3
+ [![Build Status](https://github.com/sudara/sometimes/workflows/rubyCI/badge.svg
4
+ )](https://github.com/sudara/sometimes/actions)
5
5
 
6
6
  Stop being so black and white. Mix things up a bit and execute code *sometimes*.
7
7
 
8
- A (very) simple collection of lil' helpers polluting namespaces because...why the hell not?
9
-
10
- ## Versions
11
-
12
- Works with ruby 2.2 and up as of version 0.0.3.
13
-
14
- Use version 0.0.2 if you want to support earlier rubies.
8
+ A (very) simple collection of lil' helpers polluting Object namespaces since 2013 because...why the hell not?
15
9
 
16
10
  ## Installation
17
11
 
@@ -38,24 +32,29 @@ require 'sometimes'
38
32
  Say something every other time
39
33
 
40
34
  ```ruby
35
+ # executes warm fuzzies 50% of the time
41
36
  sometimes do
42
- puts "Hey, you are awesome. You really are." # executes warm fuzzies 50% of the time
37
+ puts "Hey, you are awesome. You really are."
43
38
  end
44
39
  ```
45
40
 
41
+
46
42
  Maybe you want to do something several times, but not always the exact same number of times
47
43
 
48
44
  ```ruby
45
+ # between 4 and 10 boogers made, it's unpredictable!
49
46
  (4..10).times do
50
- pick_nose # between 4 and 10 boogers made, it's unpredictable!
47
+ pick_nose
51
48
  end
52
49
  ```
53
50
 
51
+
54
52
  Maybe you want to remind someone of something, but not toooo often (It gets annoying!)
55
53
 
56
54
  ```ruby
55
+ # be annoying, but only 15% of the time
57
56
  15.percent_of_the_time do
58
- puts "Howdy, Don't forget to register!" # be annoying, but only 15% of the time
57
+ puts "Howdy, Don't forget to register!"
59
58
  end
60
59
 
61
60
  33.percent_of_the_time do
@@ -66,8 +65,9 @@ end
66
65
  Share a rare moment with your user
67
66
 
68
67
  ```ruby
68
+ # be really annoying about 5% of the time
69
69
  rarely do
70
- puts "How would you like some spammy spam spam!" # be really annoying about 5% of the time
70
+ puts "How would you like some spammy spam spam!"
71
71
  end
72
72
 
73
73
  1.percent_of_the_time do
@@ -90,12 +90,43 @@ always do
90
90
  puts "Thanks for sharing!"
91
91
  end
92
92
  ```
93
+
94
+ ### Otherwise
95
+
96
+ Counting precisely is overrated.
97
+
98
+ ```ruby
99
+ # Sometimes increment, sometimes don't
100
+ i += sometimes(1)
101
+
102
+ # 5% of the time add 10, otherwise increment
103
+ i += rarely(10, otherwise:1)
104
+ ```
105
+
106
+ Model real life state, like your calendar
107
+ ```ruby
108
+ # 95% of the time you are in meetings
109
+ status = mostly ? "busy" : "free"
110
+
111
+ # same as above
112
+ status = mostly("busy", otherwise: "free")
113
+
114
+ ```
115
+
93
116
  ## Why?
94
117
 
95
118
  This gem was made so [alonetone](http://github.com/sudara/alonetone) could bit more fun when displaying notices and communicating to our users.
96
119
 
97
120
  We are human, and have personality. Shouldn't our applications reflect this? Be predictable where it counts. But toss in some spice here and there — it is always a good thing.
98
121
 
122
+
123
+ ## Versions
124
+
125
+ Works with ruby 2.7 and higher.
126
+
127
+ Use version 1.0.0 to support ruby < 2.7
128
+ Use version 0.0.2 to support ruby < 2.2
129
+
99
130
  ## Updating and Releasing this gem
100
131
 
101
132
  * Update lib/sometimes/version.rb
data/lib/sometimes.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "sometimes/version"
2
2
 
3
- # -*- encoding : utf-8 -*-
4
3
  # OH SO FUN HELPERS!
5
4
 
6
5
  # RANDOMLY EXECUTES A BLOCK X percent OF THE TIME
@@ -17,14 +16,45 @@ require "sometimes/version"
17
16
  #
18
17
  #
19
18
  # 40.percent_of_the_time do
19
+ # some_task
20
+ # end
21
+ #
22
+ # 40.percent_of_the_time(true, otherwise: false)
23
+ #
24
+ # 40.percent_of_the_time ? something : something_else
20
25
  class Integer
21
- def percent_of_the_time(&block)
26
+ def percent_of_the_time(*arg, otherwise: nil)
22
27
  return if self == 0
23
28
 
24
- if self < 0 || self > 100
25
- raise(ArgumentError, 'Integer should be between 0 and 100 to be used with the percent_of_the_time method')
29
+ raise(ArgumentError, 'Integer should be between 0 and 100 to be used
30
+ with the percent_of_the_time method') if self < 0 || self > 100
31
+
32
+ if Kernel.rand(1..100) <= self
33
+ if block_given?
34
+ yield
35
+ elsif arg.empty?
36
+ true
37
+ else
38
+ arg.first
39
+ end
40
+ else
41
+ format_otherwise(arg, otherwise)
42
+ end
43
+ end
44
+
45
+ protected
46
+
47
+ def format_otherwise(arg, otherwise)
48
+ return false if arg.empty?
49
+
50
+ if arg.first.is_a? Integer
51
+ otherwise.to_i
52
+ elsif arg.first.is_a? Float
53
+ otherwise.to_f
54
+ elsif arg.first.respond_to? :to_str
55
+ otherwise.to_s
26
56
  else
27
- yield block if Kernel.rand(1..100) <= self
57
+ otherwise
28
58
  end
29
59
  end
30
60
  end
@@ -39,20 +69,20 @@ end
39
69
  # half_the_time do
40
70
  # sometimes do
41
71
  class Object
42
- def half_the_time(&block)
43
- 50.percent_of_the_time(&block)
72
+ def half_the_time(...)
73
+ 50.percent_of_the_time(...)
44
74
  end
45
75
  alias sometimes half_the_time
46
76
 
47
- def rarely(&block)
48
- 5.percent_of_the_time(&block)
77
+ def rarely(...)
78
+ 5.percent_of_the_time(...)
49
79
  end
50
80
 
51
- def mostly(&block)
52
- 95.percent_of_the_time(&block)
81
+ def mostly(...)
82
+ 95.percent_of_the_time(...)
53
83
  end
54
84
 
55
- def never(&block); end
85
+ def never(...); end
56
86
 
57
87
  def always
58
88
  yield
@@ -1,3 +1,3 @@
1
1
  module Sometimes
2
- VERSION = "1.0.0".freeze
2
+ VERSION = "1.1.0".freeze
3
3
  end
data/sometimes.gemspec CHANGED
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'sometimes/version'
@@ -0,0 +1,72 @@
1
+ require 'test/unit'
2
+ require 'sometimes'
3
+
4
+ class OtherwiseTest < Test::Unit::TestCase
5
+ def test_percent_with_implicit_integer_otherwise
6
+ i = 0
7
+ 100000.times do
8
+ i += 50.percent_of_the_time(1)
9
+ end
10
+ assert_in_delta(i, 50000, 500)
11
+ end
12
+
13
+ def test_sometimes_with_implicit_integer_otherwise
14
+ i = 0
15
+ 100000.times do
16
+ i += sometimes(1)
17
+ end
18
+ assert_in_delta(i, 50000, 500)
19
+ end
20
+
21
+ def test_percent_with_explicit_integer_otherwise
22
+ i = 0
23
+ 100000.times do
24
+ i += 50.percent_of_the_time(1, otherwise: 1)
25
+ end
26
+ assert_equal i, 100000
27
+ end
28
+
29
+ def test_percent_with_explicit_integer_otherwise_2
30
+ options = []
31
+ 10.times do
32
+ options << 50.percent_of_the_time(1, otherwise: 2)
33
+ end
34
+ assert_equal options.uniq.sort, [1, 2]
35
+ end
36
+
37
+ def test_percent_with_implicit_string_otherwise
38
+ options = []
39
+ 10.times do
40
+ options << 50.percent_of_the_time("1")
41
+ end
42
+ assert_equal options.uniq.sort, ["", "1"]
43
+ end
44
+
45
+ def test_percent_with_explicit_string_otherwise
46
+ options = []
47
+ 10.times do
48
+ options << 50.percent_of_the_time("1", otherwise: "2")
49
+ end
50
+ assert_equal options.uniq.sort, ["1", "2"]
51
+ end
52
+
53
+ def test_percent_with_implicit_boolean_otherwise
54
+ options = []
55
+ 10.times do
56
+ options << 50.percent_of_the_time
57
+ end
58
+
59
+ assert_equal options.uniq - [true], [false]
60
+
61
+ # only contains true and false values
62
+ assert options.uniq.difference([true, false]).empty?
63
+ end
64
+
65
+ def test_sometimes_with_implicit_boolean
66
+ i = 0
67
+ 100000.times do
68
+ i += sometimes ? 1 : 0
69
+ end
70
+ assert_in_delta(i, 50000, 500)
71
+ end
72
+ end
@@ -0,0 +1,45 @@
1
+ require 'test/unit'
2
+ require 'sometimes'
3
+
4
+ class PercentOfTheTimeTest < Test::Unit::TestCase
5
+ def test_percent
6
+ i = 0
7
+ 100000.times do
8
+ 75.percent_of_the_time do
9
+ i += 1
10
+ end
11
+ end
12
+ assert i > 73000
13
+ assert i < 76000
14
+ end
15
+
16
+ def test_99
17
+ i = 0
18
+ 100000.times do
19
+ 99.percent_of_the_time do
20
+ i += 1
21
+ end
22
+ end
23
+ assert i > 97000
24
+ end
25
+
26
+ def test_1
27
+ i = 0
28
+ 100000.times do
29
+ 1.percent_of_the_time do
30
+ i += 1
31
+ end
32
+ end
33
+ assert i < 1100
34
+ end
35
+
36
+ def test_0
37
+ bool = true
38
+ 100000.times do
39
+ 0.percent_of_the_time do
40
+ bool = false
41
+ end
42
+ end
43
+ assert_equal true, bool
44
+ end
45
+ end
@@ -1,16 +1,14 @@
1
1
  require 'test/unit'
2
2
  require 'sometimes'
3
- require 'helper'
4
3
 
5
4
  class SometimesTest < Test::Unit::TestCase
6
- def test_sometimes
5
+ def test_sometimes_with_block
7
6
  i = 0
8
7
  100000.times do
9
8
  sometimes do
10
9
  i += 1
11
10
  end
12
11
  end
13
- puts i
14
12
  assert (i < 51000) && (i > 49000)
15
13
  end
16
14
 
@@ -21,17 +19,6 @@ class SometimesTest < Test::Unit::TestCase
21
19
  assert i < 51
22
20
  end
23
21
 
24
- def test_percent
25
- i = 0
26
- 100000.times do
27
- 75.percent_of_the_time do
28
- i += 1
29
- end
30
- end
31
- assert i > 73000
32
- assert i < 76000
33
- end
34
-
35
22
  def test_rarely
36
23
  i = 0
37
24
  100000.times do
@@ -52,36 +39,6 @@ class SometimesTest < Test::Unit::TestCase
52
39
  assert i > 90000
53
40
  end
54
41
 
55
- def test_99
56
- i = 0
57
- 100000.times do
58
- 99.percent_of_the_time do
59
- i += 1
60
- end
61
- end
62
- assert i > 97000
63
- end
64
-
65
- def test_1
66
- i = 0
67
- 100000.times do
68
- 1.percent_of_the_time do
69
- i += 1
70
- end
71
- end
72
- assert i < 1100
73
- end
74
-
75
- def test_0
76
- bool = true
77
- 100000.times do
78
- 0.percent_of_the_time do
79
- bool = false
80
- end
81
- end
82
- assert_equal true, bool
83
- end
84
-
85
42
  def test_never
86
43
  bool = true
87
44
  never do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sometimes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sudara
@@ -55,6 +55,8 @@ files:
55
55
  - lib/sometimes/version.rb
56
56
  - sometimes.gemspec
57
57
  - test/helper.rb
58
+ - test/test_otherwise.rb
59
+ - test/test_percent_of_the_time.rb
58
60
  - test/test_sometimes.rb
59
61
  homepage: ''
60
62
  licenses: []
@@ -80,4 +82,6 @@ specification_version: 4
80
82
  summary: Be random. Be unpredictable. But only sometimes
81
83
  test_files:
82
84
  - test/helper.rb
85
+ - test/test_otherwise.rb
86
+ - test/test_percent_of_the_time.rb
83
87
  - test/test_sometimes.rb