mmb-seasonal 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +34 -0
- data/lib/seasonal.rb +67 -0
- data/seasonal.gemspec +20 -0
- data/test/seasonal_test.rb +106 -0
- metadata +63 -0
data/README
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
Create a calendar of date and time events and execute code based on which
|
2
|
+
events are currently happening.
|
3
|
+
|
4
|
+
Example (print holiday greetings):
|
5
|
+
|
6
|
+
require 'seasonal'
|
7
|
+
|
8
|
+
holidays = Seasonal::Calendar.new
|
9
|
+
|
10
|
+
holidays.push(Seasonal::Event.new(
|
11
|
+
'Merry Christmas', 'America/New_York', 'dec 25'))
|
12
|
+
holidays.push(Seasonal::Event.new(
|
13
|
+
"April Fool's Day", 'America/New_York', 'apr 1'))
|
14
|
+
|
15
|
+
holidays.going_on { |e| puts e.payload }
|
16
|
+
|
17
|
+
Example (make text red between 9:00 and 10:00 am):
|
18
|
+
|
19
|
+
require 'seasonal'
|
20
|
+
|
21
|
+
colors = Seasonal::Calendar.new
|
22
|
+
|
23
|
+
colors.push(Seasonal::Event.new(
|
24
|
+
'#ff0000', 'America/New_York', nil, '9:00am', nil, '10:00am'))
|
25
|
+
|
26
|
+
color = '#000000'
|
27
|
+
colors.going_on { |e| color = e.payload }
|
28
|
+
|
29
|
+
puts "<p style=\"background-color : #{color}\">test</p>"
|
30
|
+
|
31
|
+
Each event has an attached payload which can be any object.
|
32
|
+
|
33
|
+
Uses the flexible time parsing of Time.parse to be easy to use and the
|
34
|
+
timezone and DST handling of tzinfo.
|
data/lib/seasonal.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
require 'tzinfo'
|
5
|
+
|
6
|
+
module Seasonal
|
7
|
+
|
8
|
+
class Event
|
9
|
+
attr_accessor :payload
|
10
|
+
attr_accessor :zone
|
11
|
+
attr_accessor :start_date
|
12
|
+
attr_accessor :start_time
|
13
|
+
attr_accessor :end_date
|
14
|
+
attr_accessor :end_time
|
15
|
+
|
16
|
+
def initialize(payload, zone, start_date, start_time=nil, end_date=nil,
|
17
|
+
end_time=nil)
|
18
|
+
if end_time.nil?
|
19
|
+
if start_time.nil?
|
20
|
+
end_time = '23:59:59'
|
21
|
+
else
|
22
|
+
if end_date.nil?
|
23
|
+
end_time = start_time
|
24
|
+
else
|
25
|
+
end_time = '23:59:59'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
start_time = '00:00:00' if start_time.nil?
|
31
|
+
|
32
|
+
end_date = start_date if end_date.nil?
|
33
|
+
|
34
|
+
@payload = payload
|
35
|
+
@zone = zone
|
36
|
+
@start_date = start_date
|
37
|
+
@end_date = end_date
|
38
|
+
@start_time = start_time
|
39
|
+
@end_time = end_time
|
40
|
+
end
|
41
|
+
|
42
|
+
def start
|
43
|
+
tz = TZInfo::Timezone.get(zone)
|
44
|
+
tz.local_to_utc(Time.parse("#{start_date} #{start_time}"))
|
45
|
+
end
|
46
|
+
|
47
|
+
def ennd
|
48
|
+
tz = TZInfo::Timezone.get(zone)
|
49
|
+
tz.local_to_utc(Time.parse("#{end_date} #{end_time}"))
|
50
|
+
end
|
51
|
+
|
52
|
+
def going_on?(test_time=Time.now)
|
53
|
+
# puts "#{start} - #{ennd}"
|
54
|
+
test_time >= start and test_time <= ennd
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
class Calendar < Array
|
60
|
+
|
61
|
+
def going_on(test_time=Time.now)
|
62
|
+
each { |event| yield event if event.going_on?(test_time) }
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
data/seasonal.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "seasonal"
|
3
|
+
s.version = "0.0.2"
|
4
|
+
s.date = "2008-11-07"
|
5
|
+
s.summary = "create logic based on defined date and time events"
|
6
|
+
s.email = "matthewm@boedicker.org"
|
7
|
+
s.homepage = "http://github.com/mmb/seasonal"
|
8
|
+
s.description = "Create a calendar of date and time events and execute code based on which events are currently happening."
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.authors = ["Matthew M. Boedicker"]
|
11
|
+
s.files = [
|
12
|
+
"lib/seasonal.rb",
|
13
|
+
"README",
|
14
|
+
"seasonal.gemspec",
|
15
|
+
]
|
16
|
+
s.test_files = [
|
17
|
+
"test/seasonal_test.rb",
|
18
|
+
]
|
19
|
+
s.add_dependency("tzinfo", ["> 0.0.0"])
|
20
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
require 'seasonal'
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
class SeasonalTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def test_start_date
|
10
|
+
e = Seasonal::Event.new(nil, 'America/New_York', 'jan 1')
|
11
|
+
|
12
|
+
assert_equal(false,
|
13
|
+
e.going_on?(Time.utc(Time.now.utc.year - 1, 12, 30, 23, 59, 59)))
|
14
|
+
|
15
|
+
assert(e.going_on?(Time.utc(Time.now.utc.year, 1, 1, 17, 0, 0)))
|
16
|
+
|
17
|
+
assert_equal(false, e.going_on?(Time.utc(Time.now.utc.year, 1, 2, 5, 0, 0)))
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_start_date_end_time
|
21
|
+
e = Seasonal::Event.new(nil,
|
22
|
+
'America/New_York', 'feb 22', nil, nil, '5:00pm')
|
23
|
+
|
24
|
+
assert_equal(false,
|
25
|
+
e.going_on?(Time.utc(Time.now.utc.year, 2, 22, 4, 59, 59)))
|
26
|
+
|
27
|
+
assert(e.going_on?(Time.utc(Time.now.utc.year, 2, 22, 13, 0, 0)))
|
28
|
+
|
29
|
+
assert_equal(false,
|
30
|
+
e.going_on?(Time.utc(Time.now.utc.year, 2, 22, 22, 0, 1)))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_start_date_end_date
|
34
|
+
e = Seasonal::Event.new(nil,
|
35
|
+
'America/New_York', 'mar 3 2003', nil, 'mar 5 2003')
|
36
|
+
|
37
|
+
assert_equal(false, e.going_on?(Time.utc(2003, 3, 3, 4, 59, 59)))
|
38
|
+
|
39
|
+
assert(e.going_on?(Time.utc(2003, 3, 4, 0, 0, 0)))
|
40
|
+
|
41
|
+
assert_equal(false, e.going_on?(Time.utc(2003, 3, 6, 5, 0, 0)))
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_start_date_end_date_end_time
|
45
|
+
e = Seasonal::Event.new(nil,
|
46
|
+
'America/New_York', 'apr 20', nil, 'may 1', '00:00')
|
47
|
+
|
48
|
+
assert_equal(false,
|
49
|
+
e.going_on?(Time.utc(Time.now.utc.year, 4, 20, 3, 59, 59)))
|
50
|
+
|
51
|
+
assert(e.going_on?(Time.utc(Time.now.utc.year, 5, 1, 0, 0, 0)))
|
52
|
+
|
53
|
+
assert_equal(false, e.going_on?(Time.utc(Time.now.utc.year, 5, 2, 0, 0, 0)))
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_start_date_start_time
|
57
|
+
e = Seasonal::Event.new(nil, 'America/New_York', 'may 10', '10:00am')
|
58
|
+
|
59
|
+
assert_equal(false,
|
60
|
+
e.going_on?(Time.utc(Time.now.utc.year, 5, 10, 13, 59, 59)))
|
61
|
+
|
62
|
+
assert(e.going_on?(Time.utc(Time.now.utc.year, 5, 10, 14, 0, 0)))
|
63
|
+
|
64
|
+
assert_equal(false,
|
65
|
+
e.going_on?(Time.utc(Time.now.utc.year, 5, 10, 14, 0, 1)))
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_start_date_start_time_end_time
|
69
|
+
e = Seasonal::Event.new(nil,
|
70
|
+
'America/New_York', 'june 22', '10:00am', nil, '10:30am')
|
71
|
+
|
72
|
+
assert_equal(false,
|
73
|
+
e.going_on?(Time.utc(Time.now.utc.year, 6, 22, 13, 59, 59)))
|
74
|
+
|
75
|
+
assert(e.going_on?(Time.utc(Time.now.utc.year, 6, 22, 14, 15, 0)))
|
76
|
+
|
77
|
+
assert_equal(false,
|
78
|
+
e.going_on?(Time.utc(Time.now.utc.year, 6, 22, 14, 30, 1)))
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_start_date_start_time_end_date
|
82
|
+
e = Seasonal::Event.new(nil,
|
83
|
+
'America/New_York', 'july 4', '11:00pm', 'jul 5')
|
84
|
+
|
85
|
+
assert_equal(false,
|
86
|
+
e.going_on?(Time.utc(Time.now.utc.year, 7, 5, 2, 59, 59)))
|
87
|
+
|
88
|
+
assert(e.going_on?(Time.utc(Time.now.utc.year, 7, 5, 15, 0, 0)))
|
89
|
+
|
90
|
+
assert_equal(false, e.going_on?(Time.utc(Time.now.utc.year, 7, 6, 4, 0, 0)))
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_start_date_start_time_end_date_end_time
|
94
|
+
e = Seasonal::Event.new(nil,
|
95
|
+
'America/New_York', 'aug 19', '1:00pm', 'aug 21', '2:00pm')
|
96
|
+
|
97
|
+
assert_equal(false,
|
98
|
+
e.going_on?(Time.utc(Time.now.utc.year, 8, 19, 16, 59, 59)))
|
99
|
+
|
100
|
+
assert(e.going_on?(Time.utc(Time.now.utc.year, 8, 20, 15, 0, 0)))
|
101
|
+
|
102
|
+
assert_equal(false,
|
103
|
+
e.going_on?(Time.utc(Time.now.utc.year, 8, 21, 18, 0, 1)))
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mmb-seasonal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew M. Boedicker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-07 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: tzinfo
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.0.0
|
23
|
+
version:
|
24
|
+
description: Create a calendar of date and time events and execute code based on which events are currently happening.
|
25
|
+
email: matthewm@boedicker.org
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- lib/seasonal.rb
|
34
|
+
- README
|
35
|
+
- seasonal.gemspec
|
36
|
+
has_rdoc: false
|
37
|
+
homepage: http://github.com/mmb/seasonal
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: create logic based on defined date and time events
|
62
|
+
test_files:
|
63
|
+
- test/seasonal_test.rb
|