sleep2 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3145e727d8fcda8a5a3ac2f5ea23fad3b15cf865
4
- data.tar.gz: 91cb4041208c854075f3f276b210f89d6b4b602c
3
+ metadata.gz: cfb0c06a9f091832dcfa13ce7996f27626335442
4
+ data.tar.gz: 792e923777e1e6f6eb682a02d25d7849c428acf3
5
5
  SHA512:
6
- metadata.gz: 30fbf6567c42df950d82f9c9eb06e94250faa6e2da8d8f95072e0cde0d8ffcb9993d4aa73362de7520a67ed7136bc758799f3957812ca5c759f97914f49ed9c2
7
- data.tar.gz: b64e98e06f5441748be779d1ac179afacec52930606b7a3bed70341c89c2e0b34069f4d8df9bb6fabc7b2dec711029c16be1e778cb948cae7ba24b3dadfa1d88
6
+ metadata.gz: b740c8560d8b65db38313d8880e9b85f1295cbada30f6ccc1ddf38cb0c27a10cacd4037bab141e8dfeb12c750c679e5bd02834b8c2ae5d728dc9226312c7c7b6
7
+ data.tar.gz: 02b67b43d475528d2dfbd922d95ffa8b855baea649371746fdcd88e00a4ed6eb1c3933c7c10e49cb12025a2d52ec10db4d525416744ac86eb14ab6462cdbf1c3
@@ -0,0 +1,10 @@
1
+ 0.2.0 / 2017-04-20
2
+ ==================
3
+
4
+ * Added new API
5
+ * Reimplemented sleeping method
6
+ * Added changelog
7
+
8
+ 0.1.0 / 2017-03-04
9
+
10
+ * Initial version
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.new(2) # no sleep when instantiating
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 # sleeps twice longer than it should
33
- #=> 4
34
- >> sleep / 2 # sleeps half the time
35
- #=> 1
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
- >> freeze = Sleep2.new(3) # no sleep when instantiating
45
+ >> delay = Sleep2[3] # no sleep when instantiating
46
46
  #=> #<Sleep2:0x2afc2eb138c0>
47
- >> freeze > sleep ? freeze : sleep # sleeps for 3 seconds
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.
@@ -3,34 +3,56 @@ require "sleep2/version"
3
3
  class Sleep2
4
4
  include Comparable
5
5
 
6
- attr_accessor :time, :time_of_instantiation
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 time
9
- @time_of_instantiation = Time.now
10
- @time = time.to_f
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
- # Avoid sleeping the first time it is called (the moment of instantiation)
15
- unless Time.now - time_of_instantiation <= 0.1
16
- sleep time
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
- # Make sleep instance can be calculated with integer
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.time if num.kind_of? self.class
24
- sleep time.public_send(m, num)
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
- # Make sleep instance comparable with another object of its type and integer
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.time <=> other.time
53
+ self.duration <=> other.duration
32
54
  elsif other.kind_of? Integer
33
- self.time <=> other
55
+ self.duration <=> other
34
56
  end
35
57
  end
36
58
  end
@@ -1,3 +1,3 @@
1
1
  class Sleep2
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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-03-04 00:00:00.000000000 Z
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.6.10
96
+ rubygems_version: 2.5.1
96
97
  signing_key:
97
98
  specification_version: 4
98
99
  summary: Ruby sleep with extra magical stuff.