sleep 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +40 -4
  2. data/bin/bedtime +90 -0
  3. data/lib/sleep.rb +21 -14
  4. metadata +19 -6
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Sleep
2
2
  =====
3
3
 
4
- A little Ruby library to calculate 6 sleep-cycle times for good night's sleep.
4
+ A basic bedtime calculator to find 6 sleep-cycle times for good night's sleep
5
5
 
6
6
 
7
7
  ## Install
@@ -16,6 +16,38 @@ A little Ruby library to calculate 6 sleep-cycle times for good night's sleep.
16
16
  gem install sleep
17
17
 
18
18
 
19
+ ## Command Line Interface
20
+
21
+ List of commands
22
+
23
+ > bedtime help
24
+
25
+ Sleep now
26
+
27
+ > bedtime now
28
+
29
+ Wake up at one of the following times (Set your alarm clock):
30
+
31
+ 10:28 PM (1.5h)
32
+ 11:58 PM (3.0h)
33
+ 1:28 AM (4.5h)
34
+ 2:58 AM (6.0h)
35
+ 4:28 AM (7.5h)
36
+ 5:58 AM (9.0h)
37
+
38
+ Wake up at
39
+
40
+ > bedtime wakeup_at 8:30 AM
41
+
42
+ Sleep at one of the following times:
43
+
44
+ 7:00 AM (1.5h)
45
+ 5:30 AM (3.0h)
46
+ 4:00 AM (4.5h)
47
+ 2:30 AM (6.0h)
48
+ 1:00 AM (7.5h)
49
+ 11:30 PM (9.0h)
50
+
19
51
  ## Usage
20
52
 
21
53
  require 'sleep'
@@ -29,7 +61,7 @@ A little Ruby library to calculate 6 sleep-cycle times for good night's sleep.
29
61
  sleep.sleep_delay = 14 # minutes
30
62
 
31
63
 
32
- #### Sleep right now
64
+ #### Sleep now
33
65
 
34
66
  p sleep.now
35
67
 
@@ -56,10 +88,14 @@ A little Ruby library to calculate 6 sleep-cycle times for good night's sleep.
56
88
  p sleep.sleep_at("in 15 minutes")
57
89
 
58
90
 
59
- ## Credits
91
+ ## References
60
92
 
93
+ * [The Power of the Sleep Cycle](http://www.glenrhodes.com/?p=132)
94
+
95
+
96
+ ## Credits
61
97
 
62
- [Chronic](https://github.com/mojombo/chronic) for amazing natural language date/time parser
98
+ * [Chronic](https://github.com/mojombo/chronic) for amazing natural language date/time parser
63
99
 
64
100
  ## License
65
101
 
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path("../../lib/sleep", __FILE__)
4
+ require 'thor'
5
+
6
+ class Bedtime < Thor
7
+
8
+ desc "now", "Sleep right now"
9
+ method_options :sleep_delay => :integer, :aliases => "-d"
10
+ def now
11
+ spaces do
12
+ puts "Wake up at one of the following times (Set your alarm clock):"
13
+ puts
14
+ update_sleep_delay!
15
+ sleep.now.each do |time|
16
+ puts time
17
+ end
18
+ end
19
+ end
20
+
21
+ desc "wakeup_at TIME", "Wake up at certain time"
22
+ method_options :sleep_delay => :integer, :aliases => "-d"
23
+ def wakeup_at(*time)
24
+ spaces do
25
+ puts "Sleep at one of the following times:"
26
+ puts
27
+ update_sleep_delay!
28
+ sleep.wakeup_at(time.join(" ")).each do |time|
29
+ puts time
30
+ end
31
+ end
32
+ end
33
+
34
+ desc "sleep_at TIME", "Sleep at certain time"
35
+ method_options :sleep_delay => :integer, :aliases => "-d"
36
+ def sleep_at(*time)
37
+ spaces do
38
+ puts "Wake up at one of the following times: (Set your alarm clock):"
39
+ puts
40
+ update_sleep_delay!
41
+ sleep.sleep_at(time.join(" ")).each do |time|
42
+ puts time
43
+ end
44
+ end
45
+ end
46
+
47
+ desc "next TIME", "next cycle for a wakeup time"
48
+ method_options :sleep_delay => :integer, :aliases => "-d"
49
+ def next(*time)
50
+ spaces do
51
+ puts "Your next wake up time:"
52
+ puts
53
+ update_sleep_delay!
54
+ puts sleep.next(time.join(" "))
55
+ end
56
+ end
57
+
58
+ desc "prev TIME", "previous cycle for a wakeup time"
59
+ method_options :sleep_delay => :integer, :aliases => "-d"
60
+ def prev(*time)
61
+ spaces do
62
+ puts "Your previous wake up time:"
63
+ puts
64
+ update_sleep_delay!
65
+ puts sleep.prev(time.join(" "))
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def update_sleep_delay!
72
+ if options[:sleep_delay]
73
+ sleep.sleep_delay = options[:sleep_delay].to_i
74
+ sleep.delay = true
75
+ end
76
+ end
77
+
78
+ def sleep
79
+ @sleep ||= Sleep.new
80
+ end
81
+
82
+ def spaces
83
+ puts
84
+ yield
85
+ puts
86
+ end
87
+
88
+ end
89
+
90
+ Bedtime.start
@@ -1,16 +1,24 @@
1
1
  require 'chronic'
2
- require 'ostruct'
3
2
 
3
+ # Sleep - A basic sleep-cycle calculator to get sleep cycle time for wake time or bed time.
4
+ #
5
+ # Example:
6
+ #
7
+ # require 'sleep'
8
+ #
9
+ # sleep = Sleep.new
10
+ #
11
+ # p sleep.now
12
+ # #=> [...]
13
+ #
4
14
  class Sleep
5
15
 
6
16
  attr_accessor :delay, :sleep_delay, :sleep_cycle
7
17
 
8
- def initialize(opts = {}, &blk)
9
- options = OpenStruct.new(opts)
10
- @delay = (options.delay || false)
11
- @sleep_delay = (options.sleep_delay || 14) # average sleep delay minutes
12
- @sleep_cycle = (options.sleep_cycle || 90) # sleep cycle minutes
13
- instance_eval(&blk) if block_given?
18
+ def initialize
19
+ @delay = false
20
+ @sleep_delay = 14 # average sleep delay minutes
21
+ @sleep_cycle = 90 # sleep cycle minutes
14
22
  end
15
23
 
16
24
  def now
@@ -24,7 +32,7 @@ class Sleep
24
32
 
25
33
  def wakeup_at(time)
26
34
  time = Chronic.parse(time)
27
- sleep_cycles(time, false)
35
+ sleep_cycles(time, true)
28
36
  end
29
37
 
30
38
  def next(time)
@@ -36,7 +44,8 @@ class Sleep
36
44
  time = time.is_a?(String) ? Chronic.parse(time) : time
37
45
  prev_cycle(time).strftime("%l:%M %p")
38
46
  end
39
- alias :prev :previous
47
+
48
+ private
40
49
 
41
50
  def next_cycle(time = Time.now)
42
51
  cycle = @delay ? @sleep_cycle + @sleep_delay : @sleep_cycle
@@ -48,19 +57,17 @@ class Sleep
48
57
  Chronic.parse("#{cycle} minutes ago", now: time)
49
58
  end
50
59
 
51
- private
52
-
53
60
  def time_distance(from_time, to_time)
54
61
  minutes = (to_time - from_time).abs/60
55
62
  (minutes / 60).round(1)
56
63
  end
57
64
 
58
- def sleep_cycles(time, forward = true)
65
+ def sleep_cycles(time, past = false)
59
66
  cycles = []
60
67
  cycle_time = time
61
68
  6.times do |i|
62
- cycle_time = forward ? next_cycle(cycle_time) : prev_cycle(cycle_time)
63
- distance = forward ? time_distance(time, cycle_time) : time_distance(cycle_time, time)
69
+ cycle_time = !past ? next_cycle(cycle_time) : prev_cycle(cycle_time)
70
+ distance = !past ? time_distance(time, cycle_time) : time_distance(cycle_time, time)
64
71
  cycles << cycle_time.strftime("%l:%M %p (#{distance}h)").gsub(/^\s+/, '')
65
72
  end
66
73
  cycles
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sleep
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-20 00:00:00.000000000 Z
12
+ date: 2012-04-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chronic
16
- requirement: &70129312462580 !ruby/object:Gem::Requirement
16
+ requirement: &70241740807260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,16 +21,29 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70129312462580
24
+ version_requirements: *70241740807260
25
+ - !ruby/object:Gem::Dependency
26
+ name: thor
27
+ requirement: &70241740806380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70241740806380
25
36
  description:
26
37
  email: bryann83@gmail.com
27
- executables: []
38
+ executables:
39
+ - bedtime
28
40
  extensions: []
29
41
  extra_rdoc_files: []
30
42
  files:
31
43
  - README.md
32
44
  - LICENSE
33
45
  - lib/sleep.rb
46
+ - bin/bedtime
34
47
  homepage: https://github.com/bry4n/sleep
35
48
  licenses: []
36
49
  post_install_message:
@@ -54,5 +67,5 @@ rubyforge_project:
54
67
  rubygems_version: 1.8.15
55
68
  signing_key:
56
69
  specification_version: 3
57
- summary: A little Ruby library to calculate 6 sleep-cycle times for good night's sleep
70
+ summary: A basic bedtime calculator to find 6 sleep-cycle times for good night's sleep.
58
71
  test_files: []