sleep2 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +22 -11
- data/lib/sleep2.rb +35 -13
- data/lib/sleep2/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfb0c06a9f091832dcfa13ce7996f27626335442
|
4
|
+
data.tar.gz: 792e923777e1e6f6eb682a02d25d7849c428acf3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b740c8560d8b65db38313d8880e9b85f1295cbada30f6ccc1ddf38cb0c27a10cacd4037bab141e8dfeb12c750c679e5bd02834b8c2ae5d728dc9226312c7c7b6
|
7
|
+
data.tar.gz: 02b67b43d475528d2dfbd922d95ffa8b855baea649371746fdcd88e00a4ed6eb1c3933c7c10e49cb12025a2d52ec10db4d525416744ac86eb14ab6462cdbf1c3
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Sleep2
|
2
2
|
|
3
|
-
Ruby sleep with extra magical stuff.
|
3
|
+
Ruby sleep with extra magical stuff. These extra stuff will be useful when you
|
4
|
+
are developing an algorithm with some kind of delay patterns such as in
|
5
|
+
animation.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -25,29 +27,38 @@ Don't forget to `require 'sleep2'` first.
|
|
25
27
|
### Do math with sleep.
|
26
28
|
|
27
29
|
```ruby
|
28
|
-
>> sleep = Sleep2
|
30
|
+
>> sleep = Sleep2[2] # no sleep when instantiating
|
29
31
|
#=> #<Sleep2:0x2afc2eaef970>
|
30
32
|
>> sleep # sleeps for 2 seconds
|
31
33
|
#=> #<Sleep2:0x2afc2eaef970>
|
32
|
-
>> sleep * 2 #
|
33
|
-
#=>
|
34
|
-
>>
|
35
|
-
#=>
|
36
|
-
>> sleep ** 2 # sleeps for 8 seconds
|
37
|
-
#=> 8
|
34
|
+
>> delay = sleep * 2 # instantiate new sleep object (therefore no sleeping yet)
|
35
|
+
#=> #<Sleep2:0x2afc217b09d0>
|
36
|
+
>> delay # sleeps twice longer than sleep
|
37
|
+
#=> #<Sleep2:0x2afc217b09d0>
|
38
38
|
```
|
39
39
|
|
40
40
|
### Compare sleep with integer (in seconds) or another of its type
|
41
41
|
|
42
42
|
```ruby
|
43
|
-
>> sleep if sleep > 3 # not sleeping at all
|
43
|
+
>> sleep if sleep > 3 # not sleeping at all since sleep duration is 2 seconds
|
44
44
|
#=> nil
|
45
|
-
>>
|
45
|
+
>> delay = Sleep2[3] # no sleep when instantiating
|
46
46
|
#=> #<Sleep2:0x2afc2eb138c0>
|
47
|
-
>>
|
47
|
+
>> delay > sleep ? delay : sleep # sleeps for 3 seconds
|
48
48
|
#=> #<Sleep2:0x2afc2eb138c0>
|
49
49
|
```
|
50
50
|
|
51
|
+
### Change current instance duration
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
>> freeze = Sleep2[3] # no sleep when instantiating
|
55
|
+
#=> #<Sleep2:0x2afc2262c2c0>
|
56
|
+
>> freeze[4] # no sleep when changing duration
|
57
|
+
#=> 4.0
|
58
|
+
>> freeze # sleeps for 4 seconds
|
59
|
+
#=> #<Sleep2:0x2afc2262c2c0>
|
60
|
+
```
|
61
|
+
|
51
62
|
## Contributing
|
52
63
|
|
53
64
|
Bug reports and pull requests are welcome on GitHub at https://github.com/styd/sleep2. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/lib/sleep2.rb
CHANGED
@@ -3,34 +3,56 @@ require "sleep2/version"
|
|
3
3
|
class Sleep2
|
4
4
|
include Comparable
|
5
5
|
|
6
|
-
|
6
|
+
def self.[] duration
|
7
|
+
new duration
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_accessor :duration, :sleep_proc
|
11
|
+
attr_writer :new
|
7
12
|
|
8
|
-
def initialize
|
9
|
-
|
10
|
-
|
13
|
+
def initialize duration
|
14
|
+
if duration.class.ancestors.include?(Numeric)
|
15
|
+
@duration = duration.to_f
|
16
|
+
elsif duration.class == self.class
|
17
|
+
@duration = duration.duration.to_f
|
18
|
+
else
|
19
|
+
raise TypeError, "can't convert #{duration.class} into time interval"
|
20
|
+
end
|
21
|
+
@new = true
|
22
|
+
end
|
23
|
+
|
24
|
+
def new?
|
25
|
+
@new
|
11
26
|
end
|
12
27
|
|
13
28
|
def inspect
|
14
|
-
#
|
15
|
-
unless
|
16
|
-
|
29
|
+
# Avoids sleeping the first time it is called (the moment of instantiation)
|
30
|
+
unless new?
|
31
|
+
self.sleep_proc.call
|
32
|
+
else
|
33
|
+
self.new = false
|
34
|
+
self.sleep_proc = proc { sleep duration }
|
17
35
|
end
|
18
36
|
end
|
19
37
|
|
20
|
-
|
38
|
+
def [] new_duration
|
39
|
+
self.duration = new_duration
|
40
|
+
end
|
41
|
+
|
42
|
+
# Creates new sleep instance by calculating it with integer
|
21
43
|
[:*, :/, :+, :-, :%, :**].each do |m|
|
22
44
|
define_method m do |num|
|
23
|
-
num = num.
|
24
|
-
|
45
|
+
num = num.duration if num.kind_of? self.class
|
46
|
+
self.class[ duration.public_send(m, num) ]
|
25
47
|
end
|
26
48
|
end
|
27
49
|
|
28
|
-
#
|
50
|
+
# Makes sleep instance comparable with another object of its type and integer
|
29
51
|
def <=>(other)
|
30
52
|
if other.kind_of? self.class
|
31
|
-
self.
|
53
|
+
self.duration <=> other.duration
|
32
54
|
elsif other.kind_of? Integer
|
33
|
-
self.
|
55
|
+
self.duration <=> other
|
34
56
|
end
|
35
57
|
end
|
36
58
|
end
|
data/lib/sleep2/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sleep2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrian Setyadi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- ".gitignore"
|
63
63
|
- ".rspec"
|
64
64
|
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
65
66
|
- CODE_OF_CONDUCT.md
|
66
67
|
- Gemfile
|
67
68
|
- LICENSE.txt
|
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
93
|
version: '0'
|
93
94
|
requirements: []
|
94
95
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.5.1
|
96
97
|
signing_key:
|
97
98
|
specification_version: 4
|
98
99
|
summary: Ruby sleep with extra magical stuff.
|